User:Waldyrious/autotranslate.js

From translatewiki.net

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* <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> */