MediaWiki:Gadget-google-translate.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          */
/*******************************************************************/
 
importScriptURI('http://www.google.com/jsapi');
 
addOnloadHook(function () {
  var editbox;
  if (!document.editform || !(editbox = document.editform.wpTextbox1 )) return;
  if (editbox.value) return;

  //the seven lines below are courtesy of Sparkla. We have to use this instead of
  //getElemetById() because the message definition element doesn't have an ID
  var def = getElementsByClassName(document,'fieldset','mw-sp-translate-edit-definition');
  if(def.length != 1) return;
  def = def[0];
  var code = def.getElementsByTagName('code');
  if(code.length == 0) return;
  code = code[0];
  var text = getInnerText(code);

  google.load("language", "1");
  function initialize() {
    google.language.translate(text, "", wgUserLanguage, function(result) {
      if (!result.error) editbox.value = '!!FUZZY!!' + result.translation;
      else alert(result.error);
    });
  }
  google.setOnLoadCallback(initialize);

  //add "var gTransAutoSum = true;" to your .js page if you wish to auto-fill the summary field.
  if(window.gTransAutoSum)
    document.editform.wpSummary.value = 'translated with assistance of the Google-translate gadget';
});
/* </pre> */