User:Waldir/autotranslate.js

From translatewiki.net
Jump to: navigation, search
/* <pre> */
/*******************************************************************/
/* Prefills the edit field for new translations                    */
/* with the automatic Google translation of the message definition */
/* into the user's language as set in Special:Preferences          */
/*******************************************************************/

var translate=function(){
  
 google.load("language", "pt");
 google.setOnLoadCallback(initialize);

}

var initialize=function(){
  var editbox;
  if (!(editbox = document.editform.wpTextbox1 )) return;
  
  var text = getInnerText(getElementsByClassName(document,'fieldset','mw-sp-translate-edit-definition')[0].getElementsByTagName('code')[0]);
  
  google.language.translate(text, "", wgUserLanguage, function(result) {
    if (!result.error) {
      if (!editbox.value) editbox.value = '!!FUZZY!!' + result.translation;
      else editbox.value += '\n!!FUZZY!!' + result.translation;
    }
    else alert(result.error);
  });
}
 
window.AddToolbarButton=function(caption, href, imgsrc) {
  var tbar=document.getElementById('toolbar');
  if ( !tbar ) return;
  var ba = document.createElement('a');
  ba.href = href;
  var img = document.createElement('img');
  img.src = imgsrc;
  img.alt = caption;
  ba.appendChild(img);
  ba.onfocus = ba.blur;
  tbar.appendChild(ba);
}
 
var addAutoTranslateButton = function(){
  AddToolbarButton('Autotranslate (via Google)', 'javascript:translate();', 'http://upload.wikimedia.org/wikipedia/commons/7/72/Button_%C3%A9clair_author.png');
}

var setup = function(){
  importScriptURI('http://www.google.com/jsapi');
  google.load("language", "1");
  addAutoTranslateButton();
  var editbox = document.editform.wpTextbox1;
  if (editbox && !(editbox.value)) //if the edit box exists and is empty
    translate();
}

hookEvent('load', setup );

/* </pre> */