- /* Realtime Language Translator for Chatzilla
- ** Version: 0.1
- ** Author : Jeremy Stark <jeremy@deximer.com>
- **
- ** See LICENSE.txt for licensing information (hint, it's GPL).
- ** This is just a bare bones get something working release.
- ** See TODO.txt for where this is currently headed.
- ** Direct any comments, ideas, bugs or code to the above email.
- ** There is also a bug# for this addon at:
- ** https://bugzilla.mozilla.org/show_bug.cgi?id=195670
- **
- ** Thanks to all the Chatzilla devs for making a great IRC client
- */
- plugin.id = "translator";
- plugin.init =
- function init(glob)
- {
- plugin.major = 0;
- plugin.minor = 1;
- plugin.version = plugin.major + "." + plugin.minor;
- plugin.description = "Translate languages.";
- plugin.prefary = plugin.prefary.concat([["translator_site", "google", ""]]);
- plugin.prefary = plugin.prefary.concat([["translator_from", "en", ""]]);
- plugin.prefary = plugin.prefary.concat([["translator_to", "it", ""]]);
- client.prefManager.addPrefs(plugin.prefary);
- }
- plugin.disable =
- function disable()
- {
- client.munger.delRule("translator");
- display(plugin.id + ' - plugin disabled');
- return true;
- }
- plugin.enable =
- function enable()
- {
- client.munger.addRule("translator",
- /(.*)/,
- doTranslation);
- display(plugin.id + ' - plugin enabled');
- return true;
- }
- function doTranslation (matchText, containerTag, eventData)
- {
- if(!client.translator)
- client.translator = plugin.prefs['translator_site'];
- switch(client.translator) {
- case 'google':
- doGoogleTranslation(matchText, containerTag, eventData);
- break;
- case 'babelfish':
- doFishTranslation(matchText, containerTag, eventData);
- break;
- default:
- doGoogleTranslation(matchText, containerTag, eventData);
- }
- }
- // http://babelfish.altavista.com/
- function doFishTranslation (matchText, containerTag, eventData)
- {
- var fish;
- function onLoad(e)
- {
- var translation =
- fish.responseText.match( /name=\"q\" value=\"(.[^\"]+)/ );
- if(translation[1] == '')
- messageText = 'Translation Error';
- else
- messageText = translation[1];
- var text = document.createTextNode( ' [{' + client.translator + '} ' + messageText + ' ]');
- fish.CTAG.appendChild (text);
- }
- try
- {
- fish = new XMLHttpRequest();
- fish.onload = onLoad;
- containerTag.appendChild(document.createTextNode(matchText));
- fish.CTAG = containerTag;
- var lang = plugin.prefs['translator_from'] + '_' + plugin.prefs['translator_to'];
- fish.open("GET", "http://babelfish.altavista.com/tr?trtext=" +
- matchText + "&lp=" + lang +
- "&tt=urltext&ie=ASCII&oe=ASCII&submit=Translate", true);
- fish.send(null);
- }
- catch (e)
- {
- return;
- }
- }
- // http://translate.google.com/
- function doGoogleTranslation (matchText, containerTag, eventData)
- {
- var google;
- function onLoad(e)
- {
- var translation =
- google.responseText.match( /PHYSICAL>(.[^<]+)<\/textarea>/ );
- if(translation[1] == '')
- messageText = 'Translation Error';
- else
- messageText = translation[1];
- var text = document.createTextNode( ' [{' + client.translator + ':' + google.lang + '} ' + messageText + ' ]');
- google.CTAG.appendChild (text);
- }
- try
- {
- google = new XMLHttpRequest();
- google.onload = onLoad;
- containerTag.appendChild(document.createTextNode(matchText));
- google.CTAG = containerTag;
- google.lang = plugin.prefs['translator_from'] + '|' + plugin.prefs['translator_to'];
- google.open("GET", "http://translate.google.com/translate_t?text=" +
- matchText + "&langpair=" + google.lang +
- "&hl=en&ie=ASCII&oe=ASCII&submit=Translate", true);
- google.send(null);
- }
- catch (e)
- {
- return;
- }
- }
