Tuesday, March 15, 2011

Realtime translation with jquery

For those who might be trying to follow the news in japan on ustream, you might notice all the comments are in Japanese. Here is some javascript to translate real time


var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'http://code.jquery.com/jquery-1.5.1.min.js';
head.appendChild(script);

var script2= document.createElement('script');
script2.type= 'text/javascript';
script2.src= 'http://jquery-translate.googlecode.com/files/jquery.translate-1.4.7-debug-all.js';
head.appendChild(script2);

tr = function(){
$('iframe').contents().find('*[data-template="ssMessageItem"]').translate('jp','en');
setTimeout("tr()", 100);
}
tr();




If you care what it does. It loads up jQuery, then loads up a jQuery plugin to do google translation. Sets up a timer to translate all the elements that look like messages '*[data-template="ssMessageItem"]' from jp to en.