Code for translating content using Google Translate
Often I use machine translated content when I am developing so that I have content in various language before I get the official translation. I’m not too concerned that the translation is correct, just as long as it is representative of content in that language. I’ve found that creating a website from the beginning in at least two different languages helps me avoid coding myself in a box and making assumptions that are only valid in a single-language website, particularly in my queries for content in the DB and in how the web page layout handles strings of different lengths.
I usually create an initial resource bundle of some sort in English and then use Google Translate to translate the strings in the bundle into various target languages. I automate this so that I just push a button or have the script execute in a trigger so that when I change or add to the resource bundle in English I can re-translate it easily.
Here is the type of script I use to make the call to Google Translate:
declare function local:get-google-translation($text, $source-lang, $target-lang) {
let $url := fn:concat("http://translate.google.com/translate_a/t?client=t&text=", xdmp:url-encode($text), "&hl=", $source-lang, "&tl=", $target-lang, "&multires=1&sc=1")
let $response := xdmp:http-get($url,
<options xmlns="xdmp:http-get">
<format xmlns="xdmp:document-get">text</format>
</options>
)
return fn:tokenize($response[2], '"')[2]
};
let $text := "Rome (CNN) -- Transcripts published Tuesday capture the dramatic conversations between port officials and a cruise ship captain, who a judge ruled can be held under house arrest while Italian authorities investigate his role in last week's disaster."
let $source-lang := "en"
let $target-lang := "es"
return local:get-google-translation($text, $source-lang, $target-lang)
=> ROMA (CNN) - Las transcripciones publicadas el martes la captura de las conversaciones entre los funcionarios del puerto espectacular y un capitán de barco de crucero , que puede ser un juez dictaminó bajo arresto domiciliario mientras las autoridades italianas investigar su papel en el desastre de la semana pasada .