Utente:Reza1615/ArticleTranslator.js
Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.
Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti (per le pagine globali è comunque necessario attendere qualche minuto). Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Chrome: premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menù Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.
// <nowiki>
/*jslint regexp: true, indent: 4 */
/*global $: false, wgNamespaceNumber: false, autoStart: false, wgAction: false,
homeWiki: true, translatorFormat: true, translatorKeepLinksLables: true */
if (typeof homeWiki === "undefined") {
var homeWiki = "en";
}
if (typeof translatorFormat === "undefined") {
var translatorFormat = "($1: $2)";
}
if (typeof translatorKeepLinksLables === "undefined") {
var translatorKeepLinksLables = false;
}
function Translator() {
"use strict";
var translationTextArea;
function queryTranslationFromData(data) {
var XMLDocument = $(data),
XMLQuery = 'll[lang="' + homeWiki + '"]';
return XMLDocument.find(XMLQuery).text();
}
function commitOnNode(node, translatedTitle) {
var injectionString = translatorFormat.replace("$1", homeWiki).replace("$2", translatedTitle);
node.append(injectionString);
}
function commitOnTranslationTextarea(title, translatedTitle) {
translationTextArea.val(translationTextArea
.val()
.replace(
new RegExp("(\\[\\[)" + title + "(\\|?.*?)(\\]\\])"),
"$1" + translatedTitle + (translatorKeepLinksLables ? "$2" : "") + "$3"
)
);
}
function commit(data, node, title) {
var translatedTitle = queryTranslationFromData(data);
if (translatedTitle !== "") {
if (node === null) {
commitOnTranslationTextarea(title, translatedTitle);
} else {
commitOnNode(node, translatedTitle);
}
}
}
function translateFromLanguageLink(title, node) {
$.get(wgScriptPath + "/api.php?action=query&prop=langlinks&titles=" + title + "&redirects=&format=xml&lllimit=500",
function (data) { commit(data, node, title); });
}
this.run = function () {
if (wgAction === "view" || wgAction === "purge" || wgAction === "historysubmit") {
$("#bodyContent a").each(function () {
var iter = $(this),
title = iter.attr("title");
if (title !== undefined) {
translateFromLanguageLink(title, iter);
}
});
} else if (wgAction === "edit" || wgAction === "submit") {
translationTextArea = $("#wpTextbox1")
.clone()
.attr({"id": "wpTextbox2"})
.css({"background-color": "#CCCEFF"});
$("#wpTextbox1").before(translationTextArea);
var matched = translationTextArea.val().match(/\[\[.*?\]\]/g),
i,
title;
for (i = 0; i < matched.length; i = i + 1) { // i don't like use for in, because old IE can not support it
title = matched[i].replace(/\[\[([^\]\|]*)\|?.*?\]\]/g, "$1");
translateFromLanguageLink(title, null);
}
}
};
}
var translator = new Translator();
$(function () {
"use strict";
if (typeof autoStart !== "undefined") {
if (autoStart === true) {
translator.run();
}
} else {
$("h1").append(' <a style="font-size:40%" href="javascript:translator.run()">(translate links to ' + homeWiki + '!)</a>');
}
});