Roller Weblogger language translation with Google using JavaScript and Velocity

So here’s the latest version of my Google Translation code for Roller Weblogger, as used to host blogs.sun.com (or ‘bsc’ as we in Sun call it).

It’s much improved over the original and Google versions.

First off it checks for JavaScript, if there then it uses it to make sure the page hasn’t been translated before as well as get the current URL to translate (Google generates translation glitches if it tries to translate pre-translated pages).

If not it still generates the language translation by using Velocity / Roller JSP code.

If your on bsc then $baseURL needs the ‘eclectic’ text replaced with whatever your bsc site is called, otherwise $baseURL needs to be your blog URL.

If your not on bsc you’ll also need to change $iconURL to where you are hosting the flag images (which I got from FamFamFam), unless of course you leech off mine.

So far this code is being used on Bill Vass’ blog and Glenn Brunette’s Security Weblog (as well as this one of course).

Here’s the code for you to have a look at and cut and paste if you’d like to use it. It needs to be embedded in your Roller template code, probably best to put it in the sidebar. You can also use it to translate from other source languages other than English, more on this at the bottom of the page.



<div style="margin:0px; padding:1px;"> <!-- BEGIN _MOD_TRANSLATE --> ## Current Language
#set ($langCur="en") ## Lang Text
#set ($langText_ar="Arabic")
#set ($langText_bg="Bulgarian")
#set ($langText_zh-CN="Chinese (Simplified)")
#set ($langText_zh-TW="Chinese (Traditional)")
#set ($langText_hr="Croatian")
#set ($langText_cs="Czech")
#set ($langText_da="Danish")
#set ($langText_nl="Dutch")
#set ($langText_en="English")
#set ($langText_fi="Finnish")
#set ($langText_fr="French")
#set ($langText_de="German")
#set ($langText_el="Greek")
#set ($langText_hi="Hindi")
#set ($langText_it="Italian")
#set ($langText_ja="Japanese")
#set ($langText_ko="Korean")
#set ($langText_no="Norwegian")
#set ($langText_pl="Polish")
#set ($langText_pt="Portuguese")
#set ($langText_ro="Romanian")
#set ($langText_ru="Russian")
#set ($langText_es="Spanish")
#set ($langText_sv="Swedish") ## Lang Code
#set ($langCode_ar="ar")
#set ($langCode_bg="bg")
#set ($langCode_zh-CN="zh-CN")
#set ($langCode_zh-TW="zh-TW")
#set ($langCode_hr="hr")
#set ($langCode_cs="cs")
#set ($langCode_da="da")
#set ($langCode_nl="nl")
#set ($langCode_en="en")
#set ($langCode_fi="fi")
#set ($langCode_fr="fr")
#set ($langCode_de="de")
#set ($langCode_el="el")
#set ($langCode_hi="hi")
#set ($langCode_it="it")
#set ($langCode_ja="ja")
#set ($langCode_ko="ko")
#set ($langCode_no="no")
#set ($langCode_pl="pl")
#set ($langCode_pt="pt")
#set ($langCode_ro="ro")
#set ($langCode_ru="ru")
#set ($langCode_es="es")
#set ($langCode_sv="sv") ## Other variables
#set ($tranText01="<a style="padding:0px" title='") #set ($tranText02="' href="%22)%20%20%20%0A#set%20(%24tranServer=%22http://66.102.9.104%22)%20%20%20%0A#set%20(%24tranCommand=%22/translate_c?hl=%22)%20%20%20%0A#set%20(%24tranText03=%22&#038;langpair=%22)%20%20%20%0A#set%20(%24tranText04=%22%7C%22)%20%20%20%0A#set%20(%24tranText05=%22&#038;u=%22)%20%20%20%0A#set%20(%24tranText06=%22"><img decoding="async" alt='") #set ($tranText07="' style="border:0px; padding:0px 8px 8px 0px;" src="/web/20090226230923im_/http://blogs.sun.com/eclectic/entry/%22)%20%20%20#set%20(%24iconURL=%22http://blogs.sun.com/eclectic/resource/%22)%20%20%20#set%20(%24iconTLA=%22.png"></a>" ) ## specific blog entry ? #if ($model.permalink) #set ($baseurl="http://blogs.sun.com/BVass/entry/$utilities.encode($model.weblogEntry.anchor)" ) #else #set ($baseurl="http://blogs.sun.com/BVass/" ) #end <script language="JavaScript"><br/><!--<br/>{ /* Allocate memory */<br/>strLangPage = new String (location.href); /* Catch pages translated externally and re-assign */<br/>if ((strLangPage.indexOf("/translate_c?")!=-1)) { /* Catch pages with the new google tracking code and strip */ if ((strLangPage.indexOf("&usg=")!=-1)) { strLangPage = strLangPage.slice((strLangPage.lastIndexOf("u=") + 2),(strLangPage.lastIndexOf("&usg=") )); } else { strLangPage = strLangPage.slice((strLangPage.lastIndexOf("u=") + 2)); } } document.write("<div style='margin:0px; padding:0px;'>");<br/>document.write("$tranText01$langText_ar$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ar$tranText05",strLangPage,"$tranText06$langCode_ar$tranText07$iconURL$langCode_ar$iconTLA$tranText08");<br/>document.write("$tranText01$langText_bg$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_bg$tranText05",strLangPage,"$tranText06$langCode_bg$tranText07$iconURL$langCode_bg$iconTLA$tranText08");<br/>document.write("$tranText01$langText_zh-CN$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_zh-CN$tranText05",<br/>strLangPage,<br/>"$tranText06$langCode_zh-CN$tranText07$iconURL$langCode_zh-CN$iconTLA$tranText08");<br/>document.write("$tranText01$langText_zh-TW$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_zh-TW$tranText05",<br/>strLangPage,<br/>"$tranText06$langCode_zh-TW$tranText07$iconURL$langCode_zh-TW$iconTLA$tranText08");<br/>document.write("$tranText01$langText_hr$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_hr$tranText05",strLangPage,"$tranText06$langCode_hr$tranText07$iconURL$langCode_hr$iconTLA$tranText08");<br/>document.write("$tranText01$langText_cs$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_cs$tranText05",strLangPage,"$tranText06$langCode_cs$tranText07$iconURL$langCode_cs$iconTLA$tranText08");<br/>document.write("$tranText01$langText_da$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_da$tranText05",strLangPage,"$tranText06$langCode_da$tranText07$iconURL$langCode_da$iconTLA$tranText08");<br/>document.write("$tranText01$langText_nl$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_nl$tranText05",strLangPage,"$tranText06$langCode_nl$tranText07$iconURL$langCode_nl$iconTLA$tranText08");<br/>document.write("$tranText01$langText_en$tranText02",strLangPage,"$tranText06$langCode_en$tranText07$iconURL$langCode_en$iconTLA$tranText08");<br/>document.write("$tranText01$langText_fi$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_fi$tranText05",strLangPage,"$tranText06$langCode_fi$tranText07$iconURL$langCode_fi$iconTLA$tranText08");<br/>document.write("$tranText01$langText_fr$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_fr$tranText05",strLangPage,"$tranText06$langCode_fr$tranText07$iconURL$langCode_fr$iconTLA$tranText08");<br/>document.write("$tranText01$langText_de$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_de$tranText05",strLangPage,"$tranText06$langCode_de$tranText07$iconURL$langCode_de$iconTLA$tranText08");<br/>document.write("$tranText01$langText_el$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_el$tranText05",strLangPage,"$tranText06$langCode_el$tranText07$iconURL$langCode_el$iconTLA$tranText08");<br/>document.write("$tranText01$langText_hi$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_hi$tranText05",strLangPage,"$tranText06$langCode_hi$tranText07$iconURL$langCode_hi$iconTLA$tranText08");<br/>document.write("$tranText01$langText_it$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_it$tranText05",strLangPage,"$tranText06$langCode_it$tranText07$iconURL$langCode_it$iconTLA$tranText08");<br/>document.write("$tranText01$langText_ja$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ja$tranText05",strLangPage,"$tranText06$langCode_ja$tranText07$iconURL$langCode_ja$iconTLA$tranText08");<br/>document.write("$tranText01$langText_ko$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ko$tranText05",strLangPage,"$tranText06$langCode_ko$tranText07$iconURL$langCode_ko$iconTLA$tranText08");<br/>document.write("$tranText01$langText_no$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_no$tranText05",strLangPage,"$tranText06$langCode_no$tranText07$iconURL$langCode_no$iconTLA$tranText08");<br/>document.write("$tranText01$langText_pl$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_pl$tranText05",strLangPage,"$tranText06$langCode_pl$tranText07$iconURL$langCode_pl$iconTLA$tranText08");<br/>document.write("$tranText01$langText_pt$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_pt$tranText05",strLangPage,"$tranText06$langCode_pt$tranText07$iconURL$langCode_pt$iconTLA$tranText08");<br/>document.write("$tranText01$langText_ro$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ro$tranText05",strLangPage,"$tranText06$langCode_ro$tranText07$iconURL$langCode_ro$iconTLA$tranText08");<br/>document.write("$tranText01$langText_ru$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ru$tranText05",strLangPage,"$tranText06$langCode_ru$tranText07$iconURL$langCode_ru$iconTLA$tranText08");<br/>document.write("$tranText01$langText_es$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_es$tranText05",strLangPage,"$tranText06$langCode_es$tranText07$iconURL$langCode_es$iconTLA$tranText08");<br/>document.write("$tranText01$langText_sv$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_sv$tranText05",strLangPage,"$tranText06$langCode_sv$tranText07$iconURL$langCode_sv$iconTLA$tranText08");<br/>document.write("</div>"); }<br/>// --><br/></script> <noscript>

<div style="margin:0px; padding:0px;">
$tranText01$langText_ar$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ar$tranText05$baseURL$tranText06$langCode_ar$tranText07$iconURL$langCode_ar$iconTLA$tranText08
$tranText01$langText_bg$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_bg$tranText05$baseURL$tranText06$langCode_bg$tranText07$iconURL$langCode_bg$iconTLA$tranText08
$tranText01$langText_zh-CN$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_zh-CN$tranText05$baseURL$langCode$tranText06$langCode_zh-CN$tranText07$iconURL$langCode_zh-CN$iconTLA$tranText08
$tranText01$langText_zh-TW$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_zh-TW$tranText05$baseURL$langCode$tranText06$langCode_zh-TW$tranText07$iconURL$langCode_zh-TW$iconTLA$tranText08
$tranText01$langText_hr$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_hr$tranText05$baseURL$tranText06$langCode_hr$tranText07$iconURL$langCode_hr$iconTLA$tranText08
$tranText01$langText_cs$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_cs$tranText05$baseURL$tranText06$langCode_cs$tranText07$iconURL$langCode_cs$iconTLA$tranText08
$tranText01$langText_da$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_da$tranText05$baseURL$tranText06$langCode_da$tranText07$iconURL$langCode_da$iconTLA$tranText08
$tranText01$langText_nl$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_nl$tranText05$baseURL$tranText06$langCode_nl$tranText07$iconURL$langCode_nl$iconTLA$tranText08
$tranText01$langText_en$tranText02$baseURL$langCode$tranText06$langCode_en$tranText07$iconURL$langCode_en$iconTLA$tranText08
$tranText01$langText_fi$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_fi$tranText05$baseURL$tranText06$langCode_fi$tranText07$iconURL$langCode_fi$iconTLA$tranText08
$tranText01$langText_fr$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_fr$tranText05$baseURL$tranText06$langCode_fr$tranText07$iconURL$langCode_fr$iconTLA$tranText08
$tranText01$langText_de$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_de$tranText05$baseURL$tranText06$langCode_de$tranText07$iconURL$langCode_de$iconTLA$tranText08
$tranText01$langText_el$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_el$tranText05$baseURL$tranText06$langCode_el$tranText07$iconURL$langCode_el$iconTLA$tranText08
$tranText01$langText_hi$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_hi$tranText05$baseURL$tranText06$langCode_hi$tranText07$iconURL$langCode_hi$iconTLA$tranText08
$tranText01$langText_it$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_it$tranText05$baseURL$tranText06$langCode_it$tranText07$iconURL$langCode_it$iconTLA$tranText08
$tranText01$langText_ja$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ja$tranText05$baseURL$tranText06$langCode_ja$tranText07$iconURL$langCode_ja$iconTLA$tranText08
$tranText01$langText_ko$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ko$tranText05$baseURL$tranText06$langCode_ko$tranText07$iconURL$langCode_ko$iconTLA$tranText08
$tranText01$langText_no$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_no$tranText05$baseURL$tranText06$langCode_no$tranText07$iconURL$langCode_no$iconTLA$tranText08
$tranText01$langText_pl$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_pl$tranText05$baseURL$tranText06$langCode_pl$tranText07$iconURL$langCode_pl$iconTLA$tranText08
$tranText01$langText_pt$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_pt$tranText05$baseURL$tranText06$langCode_pt$tranText07$iconURL$langCode_pt$iconTLA$tranText08
$tranText01$langText_ro$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ro$tranText05$baseURL$tranText06$langCode_ro$tranText07$iconURL$langCode_ro$iconTLA$tranText08
$tranText01$langText_ru$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_ru$tranText05$baseURL$tranText06$langCode_ru$tranText07$iconURL$langCode_ru$iconTLA$tranText08
$tranText01$langText_es$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_es$tranText05$baseURL$tranText06$langCode_es$tranText07$iconURL$langCode_es$iconTLA$tranText08
$tranText01$langText_sv$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_sv$tranText05$baseURL$tranText06$langCode_sv$tranText07$iconURL$langCode_sv$iconTLA$tranText08
</div>

</noscript> <!-- END _MOD_TRANSLATE --></div>

As I mentioned above it can also be used to translate from other source languages other than English.

By changing #set ($langCur=”en”) it’ll translate from other source languages. For instance set $langCur to ‘fr’ to use French as a the source language. You’ll also need to transpose the ‘en’ output strings with that of the source language you want to use, because Google doesn’t like trying to translate a page to and from the same language! For instance, still using French as an example:


$tranText01$langText_en$tranText02$baseURL$langCode$tranText06$langCode_en$tranText07$iconURL$langCode_en$iconTLA$tranText08
needs to become
$tranText01$langText_fr$tranText02$baseURL$langCode$tranText06$langCode_fr$tranText07$iconURL$langCode_fr$iconTLA$tranText08
and reciprocally
$tranText01$langText_fr$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_fr$tranText05$baseURL$tranText06$langCode_fr$tranText07$iconURL$langCode_fr$iconTLA$tranText08
needs to become
$tranText01$langText_en$tranText02$tranServer$tranCommand$langCur$tranText03$langCur$tranText04$langCode_en$tranText05$baseURL$tranText06$langCode_en$tranText07$iconURL$langCode_en$iconTLA$tranText08

Remember that there are two versions of this string though, one encapsulated in JavaScript (using ‘document.write’) and the other free standing in the ‘NOSCRIPT’ element, just in case JavaScript isn’t used.