مستخدم:Nordin far/translator.js
يمكن توفير توثيق لسكربت المستخدم هذا في الصفحة : مستخدم:Nordin far/translator. |
ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.
/**************************************************************مترجم الوصلات و صناديق المعلومات****************************************************************/
// en:User:Ebraminio/ArticleTranslator.js, see ويكيبيديا:الميدان/تقنية/05/2012#مترجم
/*jslint regexp: true, indent: 4 */
/*global $: false, wgNamespaceNumber: false, autoStart: false, wgAction: false,
wgScriptPath: false, wgPageContentLanguage: false, window: false,
homeWiki: true, linkTranslatorFormat: true, removeLinksAliases: true, removeLinksAliasesText: true, doneText: true,
enableTemplateTranslation: true, fromLang: true, translatorBarFormat: true, templateTranslatorText: true */
if (typeof fromLang === "undefined") {
var fromLang = "en";
}
if (typeof homeWiki === "undefined") {
var homeWiki = "ar";
}
if (typeof linkTranslatorFormat === "undefined") {
var linkTranslatorFormat = " <small class=nowrap>($2)</small>";
}
if (typeof enableTemplateTranslation === "undefined") {
var enableTemplateTranslation = true;
}
if (typeof fromLang === "undefined") {
var fromLang = wgPageContentLanguage;
}
if (typeof translatorBarFormat === "undefined") {
var translatorBarFormat = "$1ترجم$2 الوصلات من <b>$3</b> إلى <b>$4</b>";
}
if (typeof templateTranslatorText === "undefined") {
var templateTranslatorText = "ترجم القوالب";
}
if (typeof removeLinksAliases === "undefined") {
var removeLinksAliases = true;
}
if (typeof removeLinksAliasesText === "undefined") {
var removeLinksAliasesText = "أخف عناوين الوصلات";
}
if (typeof doneText === "undefined") {
var doneText = "تم";
}
var ajaxUrl = null;
function setAjaxUrl() {
"use strict";
ajaxUrl = window.location.protocol + "//" + window.location.host.replace(wgPageContentLanguage, fromLang) + ":" + window.location.port + wgScriptPath + "/api.php?action=query&prop=langlinks&redirects=&format=json&lllimit=500&titles=";
}
setAjaxUrl();
// Regexp.escape() from: http://80.68.89.23/2006/Jan/20/escape/
RegExp.escape = function (text) {
"use strict";
return text.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
};
function Translator() {
"use strict";
var translationTextArea,
progressCount,
itemsCount;
function increaseProgressCount() {
progressCount = progressCount + 1;
$("#translatorProgress").attr("value", progressCount);
if (progressCount === itemsCount) {
$("#translatorProgress").hide("slow");
$("#translatorStatus").css("font-weight", "bold").text(doneText).show("slow");
}
}
function queryTranslationFromData(data) {
var languageLinks,
homeWikiLanguageLink;
/*jslint unparam: true*/ // I don't want use key in this closure!
$.each(data.query.pages, function (key, value) { // for retrieving first object index
languageLinks = value.langlinks;
});
/*jslint unparam: false*/
if (typeof languageLinks !== "undefined") {
homeWikiLanguageLink = languageLinks.filter(function (x) { return x.lang === homeWiki; });
if (homeWikiLanguageLink.length === 1) {
return homeWikiLanguageLink[0]["*"];
}
}
return "";
}
function commonAjaxRunner(title, translatorFunction) {
$.ajax({
url: ajaxUrl + encodeURIComponent(title),
success: function (data) {
increaseProgressCount();
var translation = queryTranslationFromData(data);
if (translation !== "") {
translatorFunction(translation);
}
},
dataType: "jsonp"
});
}
function addTranslationToNode(node, translation) {
var injectionString = linkTranslatorFormat.replace("$1", homeWiki).replace("$2", translation);
node.append(injectionString);
}
function translateFromLanguageLinkNode(title, node) {
commonAjaxRunner(title, function (translation) {
addTranslationToNode(node, translation);
});
}
// for Links in textareas
function addTranslationToTextareaLink(title, translation) {
translationTextArea.val(translationTextArea.val().replace(
new RegExp("(\\[\\[:?)" + RegExp.escape(title) + "(\\|?.*?)(\\]\\])"),
"$1" + translation + (removeLinksAliases ? "" : "$2") + "$3"
));
}
function translateFromLanguageLinks(title) {
commonAjaxRunner(title, function (translation) {
addTranslationToTextareaLink(title, translation);
});
}
// for {{TemplateLink}}s in textareas
function addTranslationToTextareaTemplateLink(title, translation) {
translationTextArea.val(translationTextArea.val().replace(
new RegExp("(\\{\\{\\s*(?:[Tt]emplate:)?)" + RegExp.escape(title) + "([\\n\\|\\}])"),
"$1" + translation + "$2"
));
}
function translateFromLanguageTemplateLinks(title) {
commonAjaxRunner("Template:" + title, function (translation) {
addTranslationToTextareaTemplateLink(title, translation.replace(/^.*?:/, ""));
});
}
this.run = function () {
progressCount = 0;
itemsCount = 0;
$("#translatorStatus").hide(0);
$("#translatorProgress").removeAttr("max").removeAttr("value");
if (wgAction === "view" || wgAction === "purge" || wgAction === "historysubmit") {
$("#bodyContent a").each(function () {
var iter = $(this),
title = iter.attr("title");
if (title !== undefined) {
itemsCount = itemsCount + 1;
translateFromLanguageLinkNode(title, iter);
}
});
$("#translatorProgress").show().attr("max", itemsCount);
} else if (wgAction === "edit" || wgAction === "submit") {
$("#wpTextbox2").remove(); // remove translation textarea if exists
if (fromLang === wgPageContentLanguage) {
translationTextArea = $("#wpTextbox1").clone().attr({
"id": "wpTextbox2"
}).css({ // new color for translation textarea
"background-color": "whitesmoke"
}).val($("#wpTextbox1").val()); // this something that clone must do
$("#wpTextbox1").before(translationTextArea); // put translation textarea before old
} else {
translationTextArea = $("#wpTextbox1");
}
// for links
var links = translationTextArea.val().match(/\[\[.*?\]\]/g),
templates = translationTextArea.val().match(/\{\{.*?[\n\|\}]/g),
i,
title;
itemsCount = -1;
if (links !== null) {
for (i = 0; i < links.length; i = i + 1) { // equals with <code>for (i in matched)</code>
title = links[i].replace(/\[\[:?([^\]\|]*)\|?.*?\]\]/g, "$1");
translateFromLanguageLinks(title);
}
if (itemsCount === -1) {
itemsCount = 0;
}
itemsCount = itemsCount + links.length;
}
if (templates !== null && enableTemplateTranslation === true) {
for (i = 0; i < templates.length; i = i + 1) { // equals with <code>for (i in matched)</code>
title = templates[i].replace(/\{\{\s*(?:[Tt]emplate:)?(.*)\s*[\n\|\}]/g, "$1");
translateFromLanguageTemplateLinks(title);
}
if (itemsCount === -1) {
itemsCount = 0;
}
itemsCount = itemsCount + templates.length;
}
if (itemsCount !== -1) {
$("#translatorProgress").show().attr("max", itemsCount);
}
}
};
}
function initializeEditorFor(forEditable, inputForEditable, setVariableClosure) {
"use strict";
$(forEditable).click(function (event) {
event.preventDefault();
$(forEditable).hide();
$(inputForEditable).css("width", "2em").show().val($(forEditable).text());
});
$(inputForEditable).keyup(function (event) {
var selectedLanguage = $(this).val();
if (event.keyCode === 13) {
$(this).focusout(); // on enter
} else if (event.keyCode === 27) {
$(forEditable).show(); // on escape
$(inputForEditable).hide().val(selectedLanguage);
}
}).focusout(function () {
var selectedLanguage = $(this).val();
if (/...?/.test(selectedLanguage)) {
setVariableClosure(selectedLanguage);
$(forEditable).html(selectedLanguage);
}
$(forEditable).show();
$(inputForEditable).hide();
});
}
var translator = new Translator();
$(function () {
"use strict";
if (typeof autoStart !== "undefined") {
if (autoStart === true) {
translator.run();
}
} else {
$("#translatorBar").remove();
var html = '<span style="font-size: 50%; margin: 0 2em;" id="translatorBar">' + translatorBarFormat;
if (wgAction === "edit" || wgAction === "submit") {
html = html + ' <input type="checkbox" name="enableTemplateTranslation" id="enableTemplateTranslation"><label for="enableTemplateTranslation">' + templateTranslatorText + '</label>';
html = html + ' <input type="checkbox" name="removeLinksAliases" id="removeLinksAliases"><label for="removeLinksAliases">' + removeLinksAliasesText + '</label>';
}
html = html + ' <span id="translatorStatus" /><progress id="translatorProgress">يتم</progress></span>';
html = html.replace('$1', '<a id="translator-button" href="#">');
html = html.replace('$2', '</a>');
html = html.replace('$3', '<a id="translator-from" href="#">' + fromLang + '</a><input style="display: none" id="translator-from-input">');
html = html.replace('$4', '<a id="translator-to" href="#">' + homeWiki + '</a><input style="display: none" id="translator-to-input">');
$("h1").append(html);
$("#translatorProgress").hide(0);
$("#translator-button").click(function (event) {
event.preventDefault();
//.replace(/\<[ref]+\>.+\<\/[ref]+\>/g, "")
////ترجمة صندوق تصفح .replace(/\|\s*111/, "| 222")
var MyValue = document.getElementById("wpTextbox1").value;
document.getElementById("wpTextbox1").value = MyValue.replace(/\|\s*name/,"| اسم").replace(/\|\s*group(\d)\s*\=/g, "| مجموعة$1 =").replace
(/\|\s*list(\d)\s*\=/g, "| قائمة$1 =").replace(/\*\*<[small]+>.+<\/[small]+>/g, "");
translator.run();
});
initializeEditorFor("#translator-to", "#translator-to-input", function (value) {
homeWiki = value;
});
initializeEditorFor("#translator-from", "#translator-from-input", function (value) {
fromLang = value;
setAjaxUrl();
});
$("#enableTemplateTranslation").attr("checked", enableTemplateTranslation).click(function () {
enableTemplateTranslation = this.checked;
});
$("#removeLinksAliases").attr("checked", removeLinksAliases).click(function () {
removeLinksAliases = this.checked;
});
}
});