Google Translate for Blog Articles

This entry is at request of my friend Jackie. He found there is translation of many of my articles on my blog: Franch, Chinese, German… He understood it is translated using Google Translate API, but don’t know how to do it exactly. Here is the trick (sorry for posting for too late).

Below is the script behind the scene. I am away from code for many years. The last time I touched PHP code was in 2007 (ops. The last time I touched ASP, or .NET code was in 2004).


function file_get_contents_curl($url) {

   $ch = curl_init();

   curl_setopt($ch, CURLOPT_HEADER, 0);

   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   //Set curl to return the data instead of printing it to the browser.

   curl_setopt($ch, CURLOPT_URL, $url);                  $data = curl_exec($ch);


   return $data;



function translate_term($term, $fromlang, $tolang) {

   $apistr = “$fromlang%7C$tolang&q=” . urlencode($term);

   $dest = file_get_contents_curl($apistr);

   $json = json_decode($dest);

   return $json->responseData->translatedText;


function translate($src, $fromlang, $tolang) {

   $lines = split(“<br \/>”, $src);

   $rlines = array();

   foreach($lines as $line) {

      $terms = split(“\. “, $line);

      $rterms = array();

      foreach($terms as $term)

         $rterms[] = translate_term($term, $fromlang, $tolang);

      $rlines[] = implode(“. “, $rterms);


   return implode(“<br />”, $rlines);



function get_body($url)   {

   $html = file_get_contents($url);

   if(preg_match(“/(<title>.*<\/title>).*<p><p>(.*)<\/p><\/p>/smi”, $html, $matches))

      return $matches[1] . $matches[2];

   return “”;



The code above is very easy to read. Basically, the idea is to break long sentences into small chunks that Google Translator (AJAX version) can handle, and then implode them back. This is a workaround but so far I haven’t seen significant problems.

How to use it?

The function translate() can translate whatever length of text from one language, to another. A typical call would be like:

echo(translate(get_body(“../archives/$file”), “en”, $lang));

Good luck, Jackie!

3 thoughts on “Google Translate for Blog Articles

  1. One place to try translation codes is the Tripadvisor reviews that have a Google translate link on the site, or using Google translate on the reviews on 地图. Although the translations are often incomprehensible because they don’t take large enough phrases ( 的话 ends up being something like “of speech”) at least the Chinese to English is no worse than the French to English.

    As an aside, I was watching a beautiful BBC video called “Wild China” where it seemed the announcer mangled the Chinese pronunciation horribly, and then I remember that you, Wang Jianshuo, have complained about the mis-translations to English on public signs in China, but we from native English speaking countries find them charming and funny and even wonder if they are done deliberately to entertain us (and disarm us into thinking that Chinese are not fierce business competitors because the English is so cute). So maybe Chinese find the mispronunciation of Chinese names and places by native English speakers, especially television and movie commentors, just as funny and silly as we do the written translations in the other direction. Or are they as offensive and rude as it seems to me. I’d love to know how you and others feel.

Leave a Reply

Your email address will not be published. Required fields are marked *