Sunday, 14 October 2012

Translate tool for websites using Google and Bing

Reactions: 
  This translate tool will essentially give you quite a few html links which when clicked upon will automatically submit the webpage page url to translate.google.com and microsofttranslator.com where you can view the translated version of the same page. The tool depends on the availability of both the services though :-). You need to set the source webpage language in the scripts correctly to ensure that to tool works properly.
 Once the source webpage language is set. The scripts have JavaScript arraylist(s) which contain language key-value pairs where the value is displayed in the webpage and the key gets submitted as the part of the submission URL to the translation websites. The for loop in the script essentially creates the translation links dynamically by iterating through the language key value pairs appending them to the translator urls.
 The Version of the scripts from this blog which opens the pages in new window or tab and has style sheet applied to the links are further down

Google Translate(Simple)

<script type="text/javascript">
//store page url
var pageUrl=document.location.href;
//Store href beginning string
var hrefBeg='<a href=\'';
//Store href ending string
var hrefEnd='\'>';
//store anchor tag closure
var hrefClosure='</a>';
//set your webpage language here.It should be one from the below arraylists.It //cannot be "mandarin".Use  zh-CN
var myLang='en';
//Google translator url.Source page url is set as English
var transHelpPage='http://translate.google.com/translate?sl='+myLang+'&tl=';
//store the url param to indicate page url
var urlBeg='&u=';
//Store space between languages
var spacer='&nbsp;';
//Two dimensional JavaScript array to Store language label and label code
var arr = [['af','Afrikaans'],['sq','Albanian'],['ar','Arabic'],['hy','Armenian'],['az','Azerbaijani'],['eu','Basque'],['be','Belarusian'],['bn','Bengali'],['bg','Bulgarian'],['ca','Catalan'],['zh-CN','Chinese'],['hr','Croatian'],['cs','Czech'],['da','Danish'],['nl','Dutch'],['en','English'],['eo','Esperanto'],['et','Estonian'],['tl','Filipino'],['fi','Finnish'],['fr','French'],['gl','Galician'],['ka','Georgian'],['de','German'],['el','Greek'],['gu','Gujarati'],['ht','HaitianCreole'],['iw','Hebrew'],['hi','Hindi'],['hu','Hungarian'],['is','Icelandic'],['id','Indonesian'],['ga','Irish'],['it','Italian'],['ja','Japanese'],['kn','Kannada'],['ko','Korean'],['lo','Lao'],['la','Latin'],['lv','Latvian'],['lt','Lithuanian'],['mk','Macedonian'],['ms','Malay'],['mt','Maltese'],['no','Norwegian'],['fa','Persian'],['pl','Polish'],['pt','Portuguese'],['ro','Romanian'],['ru','Russian'],['sr','Serbian'],['sk','Slovak'],['sl','Slovenian'],['es','Spanish'],['sw','Swahili'],['sv','Swedish'],['ta','Tamil'],['te','Telugu'],['th','Thai'],['tr','Turkish'],['uk','Ukrainian'],['ur','Urdu'],['vi','Vietnamese'],['cy','Welsh'],['yi','Yiddish']];
//For loop to iterate over the two dimensional javascript array
for (var i=0;i<arr.length; i++) {
document.write(spacer+hrefBeg+transHelpPage+arr[i][0]+urlBeg+pageUrl+hrefEnd+arr[i][1]+hrefClosure+spacer);
  }
</script>

Microsoft Translate(Simple)

<script type="text/javascript">
//store page url
var pageUrl=document.location.href;
//Store href beginning string
var hrefBeg='<a href=\'';
//Store href ending string
var hrefEnd='\'>';
//store anchor tag closure
var hrefClosure='</a>';
//set your webpage lanuage here.It should be one from the below arraylists.It //cannot be "mandarin".Use either zh-CHS or zh-CHT
var myLang='en';
//Bing translator url.Source page url is set as english
var transHelpPage='http://www.microsofttranslator.com/bv.aspx?from='+myLang+'&to=';
//store the url param to indicate page url
var urlBeg='&a=';
//Store space between languages
var spacer='&nbsp;';
//Two sets of single dimensional javascript arrays to Store language label and label code
var LangPair_ToDDL_keys=["ar","bg","ca","zh-CHS","zh-CHT","cs","da","nl","en","et","fi","fr","de","el","ht","he","hi","mww","hu","id","it","ja","ko","lv","lt","no","fa","pl","pt","ro","ru","sk","sl","es","sv","th","tr","uk","vi"];
var LangPair_ToDDL_values=["Arabic","Bulgarian","Catalan","Chinese Simplified","ChineseTraditional","Czech","Danish","Dutch","English","Estonian","Finnish","French","German","Greek","HaitianCreole","Hebrew","Hindi","HmongDaw","Hungarian","Indonesian","Italian","Japanese","Korean","Latvian","Lithuanian","Norwegian","Persian","Polish","Portuguese","Romanian","Russian","Slovak","Slovenian","Spanish","Swedish","Thai","Turkish","Ukrainian","Vietnamese"];
//For loop to iterate over the two dimensional javascript array
for (var i=0;i<LangPair_ToDDL_keys.length; i++) {
document.write(spacer+hrefBeg+transHelpPage+LangPair_ToDDL_keys[i]+urlBeg+pageUrl+hrefEnd+LangPair_ToDDL_values[i]+hrefClosure+spacer);
  }
</script>

 Google Translator with stylesheet and open in new window / tab customisation

<style type="text/css">
a.pastoral:link {
COLOR: #0000FF;
}
a.pastoral:visited {
COLOR: #800080;
}
a.pastoral:hover {
COLOR: #FF0000;
}
a.pastoral:active {
COLOR: #00FF00;
}
</style>
<script type="text/javascript">
//store page url
var pageUrl=document.location.href;
//Store href beginning string
var hrefBeg='<a href=\'';
//Store href ending string
var hrefEnd='>';
//Store rel param string
var hrefHoldRel='\'rel=\'external\'';
//Store the style param
var styleHolder= 'class=\'pastoral\'';
//store anchor tag closure
var hrefClosure='</a>';
//set your webpage language here.It should be one from the below arraylists.It //cannot be "mandarin"
var myLang='en';
//Google translator url.Source page url is set as English
var transHelpPage='http://translate.google.com/translate?sl='+myLang+'&tl=';
//store the url param to indicate page url
var urlBeg='&u=';
//Store space between languages
var spacer='&nbsp;';
//Two dimensional javascript array to Store language label and label code
var arr = [['af','Afrikaans'],['sq','Albanian'],['ar','Arabic'],['hy','Armenian'],['az','Azerbaijani'],['eu','Basque'],['be','Belarusian'],['bn','Bengali'],['bg','Bulgarian'],['ca','Catalan'],['zh-CN','Chinese'],['hr','Croatian'],['cs','Czech'],['da','Danish'],['nl','Dutch'],['en','English'],['eo','Esperanto'],['et','Estonian'],['tl','Filipino'],['fi','Finnish'],['fr','French'],['gl','Galician'],['ka','Georgian'],['de','German'],['el','Greek'],['gu','Gujarati'],['ht','HaitianCreole'],['iw','Hebrew'],['hi','Hindi'],['hu','Hungarian'],['is','Icelandic'],['id','Indonesian'],['ga','Irish'],['it','Italian'],['ja','Japanese'],['kn','Kannada'],['ko','Korean'],['lo','Lao'],['la','Latin'],['lv','Latvian'],['lt','Lithuanian'],['mk','Macedonian'],['ms','Malay'],['mt','Maltese'],['no','Norwegian'],['fa','Persian'],['pl','Polish'],['pt','Portuguese'],['ro','Romanian'],['ru','Russian'],['sr','Serbian'],['sk','Slovak'],['sl','Slovenian'],['es','Spanish'],['sw','Swahili'],['sv','Swedish'],['ta','Tamil'],['te','Telugu'],['th','Thai'],['tr','Turkish'],['uk','Ukrainian'],['ur','Urdu'],['vi','Vietnamese'],['cy','Welsh'],['yi','Yiddish']];
//For loop to iterate over the two dimensional javascript array
for (var i=0;i<arr.length; i++) {
document.write(spacer+hrefBeg+transHelpPage+arr[i][0]+urlBeg+pageUrl+hrefHoldRel+styleHolder+hrefEnd+arr[i][1]+hrefClosure+spacer);
  }
//This function can be used to add a target="_blank" attribute to all anchor tags //with a rel="external" attribute
function externalLinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i=0; i<anchors.length; i++) { var anchor = anchors[i]; if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; } } window.onload = externalLinks;
</script>

 Bing / Microsoft Translator with stylesheet and open in new window / tab customisation

<style type="text/css">
a.microBing:link {
COLOR: #0000FF;
}
a.microBing:visited {
COLOR: #800080;
}
a.microBing:hover {
COLOR: #FF0000;
}
a.microBing:active {
COLOR: #00FF00;
}
</style>
<script type="text/javascript">
//store page url
var pageUrl=document.location.href;
//Store href beginning string
var hrefBeg='<a href=\'';
//Store href ending string
var hrefEnd='>';
//Store rel param string
var hrefHoldRel='\'rel=\'external\'';
//Store the style param
var styleHolder= 'class=\'microBing\'';
//store anchor tag closure
var hrefClosure='</a>';
//set your webpage lanuage here.It should be one from the below arraylists.It //cannot be "mandarin".Use either zh-CHS or zh-CHT
var myLang='en';
//Bing translator url.Source page url is set as English
var transHelpPage='http://www.microsofttranslator.com/bv.aspx?from='+myLang+'&to=';
//store the url param to indicate page url
var urlBeg='&a=';
//Store space between languages
var spacer='&nbsp;';
//Two sets of single dimensional javascript arrays to Store language label and label code
var LangPair_ToDDL_keys=["ar","bg","ca","zh-CHS","zh-CHT","cs","da","nl","en","et","fi","fr","de","el","ht","he","hi","mww","hu","id","it","ja","ko","lv","lt","no","fa","pl","pt","ro","ru","sk","sl","es","sv","th","tr","uk","vi"];
var LangPair_ToDDL_values=["Arabic","Bulgarian","Catalan","ChineseSimplified","ChineseTraditional","Czech","Danish","Dutch","English","Estonian","Finnish","French","German","Greek","HaitianCreole","Hebrew","Hindi","HmongDaw","Hungarian","Indonesian","Italian","Japanese","Korean","Latvian","Lithuanian","Norwegian","Persian","Polish","Portuguese","Romanian","Russian","Slovak","Slovenian","Spanish","Swedish","Thai","Turkish","Ukrainian","Vietnamese"];
//For loop to iterate over the two javascript arrays
for (var i=0;i<LangPair_ToDDL_keys.length; i++) {
document.write(spacer+hrefBeg+transHelpPage+LangPair_ToDDL_keys[i]+urlBeg+pageUrl+hrefHoldRel+styleHolder+hrefEnd+LangPair_ToDDL_values[i]+hrefClosure+spacer);
  }
//This function can be used to add a target="_blank" attribute to all anchor tags //with a rel="external" attribute
function externalLinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i=0; i<anchors.length; i++) { var anchor = anchors[i]; if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; } } window.onload = externalLinks;
</script>
Note:-open links in new window/tab is from this site

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...