Share Pastebin
Guest
Public paste!

yoda

By: a guest | Feb 9th, 2010 | Syntax: None | Size: 4.12 KB | Hits: 26 | Expires: Never
Copy text to clipboard
  1. /* Realtime Language Translator for Chatzilla
  2. ** Version: 0.1
  3. ** Author : Jeremy Stark <jeremy@deximer.com>
  4. **
  5. ** See LICENSE.txt for licensing information (hint, it's GPL).
  6. ** This is just a bare bones get something working release.
  7. ** See TODO.txt for where this is currently headed.
  8. ** Direct any comments, ideas, bugs or code to the above email.
  9. ** There is also a bug# for this addon at:
  10. ** https://bugzilla.mozilla.org/show_bug.cgi?id=195670
  11. **
  12. ** Thanks to all the Chatzilla devs for making a great IRC client
  13. */
  14.  
  15. plugin.id = "translator";
  16.  
  17. plugin.init =
  18. function init(glob)
  19. {
  20.     plugin.major = 0;
  21.     plugin.minor = 1;
  22.     plugin.version = plugin.major + "." + plugin.minor;
  23.     plugin.description = "Translate languages.";
  24.     plugin.prefary = plugin.prefary.concat([["translator_site", "google", ""]]);
  25.     plugin.prefary = plugin.prefary.concat([["translator_from", "en", ""]]);
  26.     plugin.prefary = plugin.prefary.concat([["translator_to", "it", ""]]);
  27.     client.prefManager.addPrefs(plugin.prefary);
  28. }
  29.  
  30. plugin.disable =
  31. function disable()
  32. {
  33.     client.munger.delRule("translator");
  34.  
  35.     display(plugin.id + ' - plugin disabled');
  36.  
  37.     return true;
  38. }
  39.  
  40. plugin.enable =
  41. function enable()
  42. {
  43.     client.munger.addRule("translator",
  44.                           /(.*)/,
  45.                           doTranslation);
  46.  
  47.     display(plugin.id + ' - plugin enabled');
  48.  
  49.     return true;
  50. }
  51.  
  52. function doTranslation (matchText, containerTag, eventData)
  53.  {
  54.     if(!client.translator)
  55.         client.translator = plugin.prefs['translator_site'];
  56.  
  57.     switch(client.translator) {
  58.         case 'google':
  59.             doGoogleTranslation(matchText, containerTag, eventData);
  60.             break;
  61.         case 'babelfish':
  62.             doFishTranslation(matchText, containerTag, eventData);
  63.             break;
  64.         default:
  65.             doGoogleTranslation(matchText, containerTag, eventData);
  66.     }
  67. }
  68.  
  69. // http://babelfish.altavista.com/
  70.  
  71. function doFishTranslation (matchText, containerTag, eventData)
  72. {
  73.     var fish;
  74.  
  75.     function onLoad(e)
  76.      {
  77.         var translation =
  78.              fish.responseText.match( /name=\"q\" value=\"(.[^\"]+)/ );
  79.         if(translation[1] == '')
  80.             messageText = 'Translation Error';
  81.         else
  82.             messageText = translation[1];
  83.         var text = document.createTextNode( ' [{' + client.translator + '} ' + messageText + ' ]');
  84.         fish.CTAG.appendChild (text);
  85.     }
  86.  
  87.      try
  88.      {
  89.          fish = new XMLHttpRequest();
  90.          fish.onload = onLoad;
  91.          containerTag.appendChild(document.createTextNode(matchText));
  92.          fish.CTAG = containerTag;
  93.          var lang = plugin.prefs['translator_from'] + '_' + plugin.prefs['translator_to'];
  94.          fish.open("GET", "http://babelfish.altavista.com/tr?trtext=" +
  95.              matchText + "&lp=" + lang +
  96.             "&tt=urltext&ie=ASCII&oe=ASCII&submit=Translate", true);
  97.          fish.send(null);
  98.      }
  99.      catch (e)
  100.      {
  101.          return;
  102.      }
  103. }
  104.  
  105. // http://translate.google.com/
  106.  
  107. function doGoogleTranslation (matchText, containerTag, eventData)
  108. {
  109.     var google;
  110.     function onLoad(e)
  111.      {
  112.         var translation =
  113.              google.responseText.match( /PHYSICAL>(.[^<]+)<\/textarea>/ );
  114.         if(translation[1] == '')
  115.             messageText = 'Translation Error';
  116.         else
  117.             messageText = translation[1];
  118.         var text = document.createTextNode( ' [{' + client.translator + ':' + google.lang + '} ' + messageText + ' ]');
  119.         google.CTAG.appendChild (text);
  120.     }
  121.  
  122.      try
  123.      {
  124.          google = new XMLHttpRequest();
  125.          google.onload = onLoad;
  126.          containerTag.appendChild(document.createTextNode(matchText));
  127.          google.CTAG = containerTag;
  128.          google.lang = plugin.prefs['translator_from'] + '|' + plugin.prefs['translator_to'];
  129.          google.open("GET", "http://translate.google.com/translate_t?text=" +
  130.              matchText + "&langpair=" + google.lang +
  131.             "&hl=en&ie=ASCII&oe=ASCII&submit=Translate", true);
  132.          google.send(null);
  133.      }
  134.      catch (e)
  135.      {
  136.          return;
  137.      }
  138. }