Nothing Special   »   [go: up one dir, main page]

Jump to content

User:AnonEMouse/translators.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
/** Adds links to machine translations of external links near {{language icon}} tags. 
 * [[User:AnonEMouse]], January 2008. */

hookEvent("load", addTranslators);

function addTranslators()
{
  var spans = document.getElementsByTagName("span");
  for(var i=0;i<spans.length;++i){
    addTranslations(spans[i]);
  }
}

function googleTrans(link, language)
{ // Google works with:
  var langs = {
    "(Arabic)": "ar",
    "(Chinese)": "zh-CN",
    "(Traditional Chinese)": "zh-TW",
    "(Dutch)": "nl",
    "(French)": "fr",
    "(German)": "de",
    "(Greek)": "el",
    "(Italian)": "it",
    "(Japanese)": "ja",
    "(Korean)": "ko",
    "(Portuguese)": "pt",
    "(Russian)": "ru",
    "(Spanish)": "es",
  }
  var langCode = langs[language];
  // alert(link + language + langCode);
  if(langCode == null) return null;
  var transLink = document.createElement("a");
  transLink.href = 'http://translate.google.com/translate?u=' + encodeURI(link.href) + 
                   '&langpair=' + langCode + '%7Cen&hl=en&ie=UTF-8';
  transLink.innerHTML = 'GoogleTrans';
  transLink.target = "_blank";
  //alert("returning " + transLink.innerHTML);
  return transLink;
}

function babelfish(link, language)
{ // Babelfish (Systran) works with:
  var langs = {
    "(Chinese)": "zh",
    "(Traditional Chinese)": "zt",
    "(Dutch)": "nl",
    "(French)": "fr",
    "(German)": "de",
    "(Greek)": "el",
    "(Italian)": "it",
    "(Japanese)": "ja",
    "(Korean)": "ko",
    "(Portuguese)": "pt",
    "(Russian)": "ru",
    "(Spanish)": "es",
  } 

  var langCode = langs[language];
  // alert(link + language + langCode);
  if(langCode == null) return null;
  var transLink = document.createElement("a");
  transLink.href = 'http://babelfish.altavista.com/babelfish/trurl_pagecontent?lp=' + langCode + 
             '_en&url=' + encodeURI(link.href);
  transLink.target = "_blank";
  transLink.innerHTML = 'Babelfish';
  //alert("returning " + transLink.innerHTML);
  return transLink;
}

function alphaWorks(link, language)
{ // AlphaWorks (IBM) works with:
  var langs = {
    "(Chinese)": "cn",
    "(Traditional Chinese)": "tw",
    "(French)": "fr",
    "(German)": "de",
    "(Italian)": "it",
    "(Japanese)": "jp",
    "(Spanish)": "es",
  } 
  var langCode = langs[language];
  // alert(link + language + langCode);
  if(langCode == null) return null;
  var transLink = document.createElement("a");
  transLink.href = 'http://demowsmt.dfw.ibm.com/demand?mtlang=' + langCode + 'en&translate='
             + encodeURI(link.href);

  transLink.target = "_blank";
  transLink.innerHTML = 'AlphaWorks';
  //alert("returning " + transLink.innerHTML);
  return transLink;
}

function SDL(link, language)
{ // SDL (http://www.freetranslation.com/) works with:
  var langs = {
    "(Spanish)": "spanish",
    "(French)": "french",
    "(German)": "german",
    "(Italian)": "italian",
    "(Dutch)": "dutch",
    "(Portuguese)": "portuguese",
  } 
  var langCode = langs[language];
  // alert(link + language + langCode);
  if(langCode == null) return null;
  var transLink = document.createElement("a");
  transLink.href = 'http://fets5.freetranslation.com/?sequence=core&language=' + langCode 
             + '/English&url=' + encodeURI(link.href);
  transLink.target = "_blank";
  transLink.innerHTML = 'SDL';
  //alert("returning " + transLink.innerHTML);
  return transLink;
}


function gramTrans(link, language)
{ // GramTrans works with:
  var langs = {
    "(Danish)": "dan",
    "(Swedish)": "swe",
  } 
  var langCode = langs[language];
  // alert(link + language + langCode);
  if(langCode == null) return null;
  var transLink = document.createElement("a");
  transLink.href = 'http://gramtrans.com/gt/url/?url=' + encodeURI(link.href) + '&pair=' 
             + langCode +'2eng&x-form-id=translate_url';
  transLink.target = "_blank";
  transLink.innerHTML = 'GramTrans';
  //alert("returning " + transLink.href + transLink.text);
  return transLink;
} 

function apertium(link, language)
{// Apertium works with:
  var langs = {
    "(Catalan)": "ca",
  } 
  var langCode = langs[language];
  // alert(link + language + langCode);
  if(langCode == null) return null;
  var transLink = document.createElement("a");
  transLink.href = 'http://xixona.dlsi.ua.es/apertium/tradurl.php?marcar=&direccion=' + langCode +
             '-en&inurl=' + encodeURI(link.href);
  transLink.target = "_blank";
  transLink.innerHTML = 'Apertium';
  //alert("returning " + transLink.href + transLink.text);
  return transLink;
}

function windowsLive(link, language)
{// Windows Live Translator works with:
  var langs = {
    "(Arabic)": "ar",
    "(Chinese)": "zh-chs",
    "(Traditional Chinese)": "zh-cht",
    "(Dutch)": "nl",
    "(French)": "fr",
    "(German)": "de",
    "(Italian)": "it",
    "(Japanese)": "ja",
    "(Korean)": "kp",
    "(Portuguese)": "pt",
    "(Russian)": "ru",
    "(Spanish)": "es",
  } 
  var langCode = langs[language];
  // alert(link + language + langCode);
  if(langCode == null) return null;
  var transLink = document.createElement("a");
  transLink.href = 'http://www.windowslivetranslator.com/BV.aspx?MKT=en-US&lp=' + langCode +
             '_en&a=' + encodeURI(link.href);
  transLink.target = "_blank";
  transLink.innerHTML = 'WindowsLive';
  //alert("returning " + transLink.href + transLink.text);
  return transLink;
}

function gts(link, language)
{// GTS (Global Translations) Translator works with:
  var langs = {
    "(Arabic)": "43",
    "(Chinese)": "42",
    "(Dutch)": "37",
    "(French)": "35",
    "(German)": "22",
    "(Hebrew)": "57",
    "(Italian)": "36",
    "(Japanese)": "61",
    "(Korean)": "62",
    "(Persian)": "53",
    "(Polish)": "38",
    "(Portuguese)": "39",
    "(Russian)": "40",
    "(Spanish)": "34",
    "(Turkish)": "63",
    "(Ukrainian)": "41",
  } 

  var langCode = langs[language];
  // alert(link + language + langCode);
  if(langCode == null) return null;
  var transLink = document.createElement("a");
  transLink.href = 'http://www.gts-translation.com/translatewebsite.asp?language=' + langCode +
             '&url=' + encodeURI(link.href);
  transLink.target = "_blank";
  transLink.innerHTML = 'GTS';
  //alert("returning " + transLink.href + transLink.text);
  return transLink;
}

function addTranslations(span)
{
  var transFuncs = new Array();
  transFuncs[0] = googleTrans;
  transFuncs[1] = babelfish;
  transFuncs[2] = alphaWorks;
  transFuncs[3] = SDL;
  transFuncs[4] = gramTrans;
  transFuncs[5] = apertium;
  transFuncs[6] = windowsLive;
  transFuncs[7] = gts;
  // alert(transFuncs);

  var parent = span.parentNode;
  var lang = span.innerHTML;
  if(lang[0] != '(' || lang[lang.length -1] != ')') return;
  //alert(parent + ":" + siblings.length);

  // icon inside link text
  if(parent.nodeName == "A"){
    if(parent.href.indexOf("http") == 0 // it's a real link
       && parent.href.indexOf("http://en.wikipedia.org/") != 0){ // it's an external link
      var linkParent = parent.parentNode;
     
      // alert(node.innerHTML);
      for(j=0; j<transFuncs.length; ++j){
       // alert("transFuncs: " + transFuncs[j]);
        var translation = transFuncs[j](parent, lang);
        if(translation != null){
          //alert("adding " + translation);
          translation.innerHTML = ' <small>'+ translation.innerHTML + ';</small>';
          linkParent.insertBefore(translation, node.nextSibling);
        }
      }
    }    
  }
  else {
    // icon on same line with link
    var siblings = parent.childNodes;
    for(var i=0; i<siblings.length;++i){
      node = siblings[i];
      // alert(node.nodeName);
      if(node.nodeName == "A" 
         && node.href.indexOf("http") == 0 // it's a real link
         && node.href.indexOf("http://en.wikipedia.org/") != 0){ // it's an external link
        // alert(node.innerHTML);
        for(j=0; j<transFuncs.length; ++j){
          // alert("transFuncs: " + transFuncs[j]);
          var translation = transFuncs[j](node, lang);
          if(translation != null){
            //alert("adding " + translation);
            translation.innerHTML = ' <small>'+ translation.innerHTML + ';</small>';
            parent.insertBefore(translation, node.nextSibling);
            ++i;
          }
        }
      }
    }
  } 
}


// </nowiki>