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).
<?php
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);
curl_close($ch);
return $data;
}
function translate_term($term, $fromlang, $tolang) {
$apistr = “http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=$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!
nice code. curl is great.
I will have a look of this php code and integrate it to MovableType blog, thank you very much for sharing.
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.