Code Snippet » Google Translater

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Code Snippet » Google Translater

Post by Jonah Bron »

Hello, world!

Here is a piece of code I was planning on putting into the Code Snippet forum, and was directed to post it here first.

It is a simple function that uses cURL to connect to the google translater. It simply sends the first argument to the translation page, under the same conditions as the real form, then returns the results. The second argument is the two character abreviation of the language you want produce. The third, (optional) is what language the input is.

So, what do you think?

Code: Select all

<?php
/****************************\
|Google Translation API      |
\****************************/
function google_translate($text, $lang_to, $lang_from='en'){
  $lang_to = strtolower($lang_to);
  $lang_from = strtolower($lang_from);
  $lang_pat = $lang_from .'|'. $lang_to;
  $langs = array('ar|en', 'zh|en', 'zh-CN|zh-TW', 'zh-TW|zh-CN', 'nl|en', 'en|ar', 'en|zh-CN', 'en|zh-TW', 'en|nl', 'en|fr', 'en|de', 'en|el', 'en|it', 'en|ja', 'en|ko', 'en|pt', 'en|ru', 'en|es', 'fr|en', 'fr|de', 'de|en', 'de|fr', 'el|en', 'it|en', 'ja|en', 'ko|en', 'pt|en', 'ru|en', 'es|en');
  $match = false;
  foreach ($langs as $lang){
    if ($lang==$lang_pat){
      $match = true;
      break;
    }
  }
  if ($match){
    $curl = curl_init();
    $url = 'http://translate.google.com/translate_t?langpair='. $lang_pat;
    $post = 'hl=en&ie=UTF8&text='. $text .'&langpair='. $lang_pat;
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $page = curl_exec($curl);
    curl_close($curl);
    preg_match('/<div id=result_box dir="ltr">(.*?)<\/div>/i', $page, $matches);
    $page = $matches[1];
    $page = str_replace('<br>', '
', $page);
    return $page;
  }else{
    echo 'Error: invalid language in str_translate()';
    return false;
  }
}
?>
Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Code Snippet » Google Translater

Post by Christopher »

One side of me hates to get into a design discussion about such a clean little solution ... but I can't help myself! ;) I want to give the caveat that everything I can think of is just adding bloat from one point of view.

My thoughts are really in two main direction, configurablity and compatibility. And generally dealing with both would probably turn this function into a class -- which cuts against the simplicity of the thing.

1. For configuration, there is currently no way except changing the code to add language patterns if Google adds them. They are hard coded. Is there a way to get whether a combo is valid from Google rather than having hard coded values at all? Or is there a way to add language pairs?

2. What about when cURL is not installed. You could use something like Snoopy, but there is no interface to provide an alternate connection. It may be easiest to just provide to separate functions. Or create a connection classes for cURL or Snoopy (or other?) that you can pass to this code.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Code Snippet » Google Translater

Post by Benjamin »

You could change

Code: Select all

 
  $match = false;
  foreach ($langs as $lang){
    if ($lang==$lang_pat){
      $match = true;
      break;
    }
  }
  if ($match){
 
to

Code: Select all

 
if (in_array($langs_pat, $langs)) {
 
But I'm a nut for using the least amount of code possible..

On second thought, you could get rid of the:

Code: Select all

 
$lang_pat = $lang_from .'|'. $lang_to;
 
and then change the in_array line to

Code: Select all

 
if (in_array("$lang_from|$lang_to", $langs)) {
 
Everything else looks good to me for such a simple bit of code. Ideally the curl and language code would be in seperate classes of functions.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Code Snippet » Google Translater

Post by Jonah Bron »

I see.

Thanks, arborint. I'll take that into account, and work on that. I would have done the language thing, but I couldn't picture it changing any time soom.

I'll also put those in, astions.

Thanks...Keep 'em comin' 8)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Code Snippet » Google Translater

Post by Christopher »

PHPyoungster wrote:Thanks, arborint. I'll take that into account, and work on that. I would have done the language thing, but I couldn't picture it changing any time soom.
If you don't think it will change, or you will track the changes with releases then your design might be the best. But it also might be good to somehow just do the call and get an error if the language combo was not supported. Then the code would automatically support everything that Google provides.
(#10850)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Code Snippet » Google Translater

Post by Jonah Bron »

Okay. I went ahead and did it. Wrote up the class with cURL and Snoopy, but haven't gotten around to debugging.
timmy01
Forum Newbie
Posts: 1
Joined: Wed May 28, 2008 11:35 pm

Re: Code Snippet » Google Translater

Post by timmy01 »

i tried the function but
if when i try to translate from english to chinese or japanese
i got a whole bunch of questions marks?

what do you guy got?
how do i fix this?
Post Reply