User:Mr. Ibrahem/ArticleTranslator1.js
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)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**************************************************************مترجم الوصلات و صناديق المعلومات****************************************************************/
// 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*title/, "| عنوان").replace(/\|\s*image/,"| صورة").replace(/\|\s*caption/, "| عنوان الغلاف").replace(/\|\s*developer/, "| مطور").replace(/\|\s*publisher/, "| ناشر").replace(/\|\s*distributor/, "| موزع").replace(/\|\s*producer/, "| منتج").replace(/\|\s*designer/, "| مصمم").replace(/\|\s*composer/, "| مؤلف الموسيقى").replace(/\|\s*series/, "| سلسلة").replace(/\|\s*writer/, "| كاتب").replace(/\|\s*engine/, "| محرك").replace(/\|\s*released/, "| إصدر").replace(/\|\s*genre/, "| نوع").replace(/\|\s*modes/, "| نمط").replace(/\|\s*ratings/, "| تقدير").replace(/\|\s*platforms/, "| أنظمة").replace(/\|\s*media/, "| وسائط").replace(/\|\s*version/, "| نسخة").replace(/\|\s*artist/, "| فنان").replace(/\|\s*requirements/, "| متطلبات").replace(/\|\s*director/, "| مخرج").replace(/\=\s*\[\[[File]+.+\]\]/, "=").replace("<small>(creative director)</small>", "<small>(المخرج الإبداعي)</small>").replace("<small>(lead designer)</small>", "<small>(قائد المصممين)</small>").replace("small>(lead developer)</small>", "small>(قائد المطورين)</small>").replace("PlayStation Network", "بلاي ستيشن نيتورك").replace("(Japan)", "(اليابان)")
//ترجمة صندوق تصفح
.replace(/\{\{Navbox/,"{{صندوق تصفح عام").replace(/\|\s*name/,"| اسم").replace(/\|\s*group(\d)\s*\=/g, "| مجموعة$1 =").replace(/\|\s*list(\d)\s*\=/g, "| قائمة$1 =").replace(/\*\*\<[small]+\>.+\<\/[small]+\>/g, "")
//صندوق معلومات تلفاز
//.replace(/\|\s*show_name/, "|اسم البرنامج").replace(/\|\s*show_name_2/, "|أيضاً معروف باسم").replace(/\|\s*format/, "|صيغة").replace(/\|\s*creator/, "|صناعة").replace(/\|\s*creative_director/, "|المخرج الإبداعي").replace(/\|\s*presenter/, "|قام بعرضه").replace(/\|\s*starring/, "|بطولة").replace(/\|\s*judges/, "|حكام").replace(/\|\s*voices/, "|أصوات").replace(/\|\s*narrated/, "|تأليف").replace(/\|\s*theme_music_composer/, "|مؤلف موسيقى البرنامج").replace(/\|\s*opentheme/, "|الموسيقى الإفتتاحية").replace(/\|\s*endtheme/, "|الموسيقى الختامية").replace(/\|\s*country/, "| بلد الأصل").replace(/\|\s*language\s*\=\s*English/, "|اللغة= الإنجليزية").replace(/\|\s*language/, "|اللغة").replace(/\|\s*num_seasons/, "|عدد المواسم").replace(/\|\s*num_episodes/, "|عدد الحلقات").replace(/\|\s*list_episodes/, "|قائمة الحلقات").replace(/\|\s*executive_producer/, "|المنتج المنفذ").replace(/\|\s*editor/, "|المحرر").replace(/\|\s*cinematography/, "|الإخراج السينمائي").replace(/\|\s*location/, "|الموقع").replace(/\|\s*camera/, "|الكاميرا").replace(/\|\s*runtime/, "|مدة العرض").replace(/\|\s*company /, "|شركة الإنتاج").replace(/\|\s*channel/, "|القناة").replace(/\|\s*picture_format/, "|صيغة الصورة").replace(/\|\s*audio_format/, "|صيغة الصوت").replace(/\|\s*first_run /, "|عرض لأول مرة في").replace(/\|\s*network/, "|الشبكة").replace(/\|\s*first_aired/, "|بث لأول مرة في").replace(/\|\s*last_aired/, "|بث لآخر مرة في").replace(/\|\s*preceded_by/, "|سبقه").replace(/\|\s*followed_by/, "|تبعه").replace(/\|\s*related/, "|متعلق بـ").replace(/\|\s*website/, "|الموقع الرسمي").replace(/\|\s*production_website/, "|الموقع الرسمي للإنتاج").replace(/(\d\d)\s*minutes/g, "$1 دقيقة").replace(/(\d\d)\s*episodes/g, "$1 حلقة").replace(/\(DVD\)/g, "(دي في دي)").replace(/\(Blu-ray\)/g, "(بلوراي)").replace(/United\sStates/g, "الولايات المتحدة").replace(/present/, "الوقت الحاضر")
//شهور السنة
.replace(/January/g, "يناير").replace(/February/g, "فبراير").replace(/March/g, "مارس").replace(/April/g, "أبريل").replace(/May/g, "مايو").replace(/June/g, "يونيو").replace(/July/g, "يوليو").replace(/August/g, "أغسطس").replace(/September/g, "سبتمبر").replace(/October/g, "أكتوبر").replace(/November/g, "نوفمبر").replace(/December/g, "ديسمبر")
//صندوق معلومات شخصية
//.replace(/\|\s*birth_name/, "| الاسم عند الولادة").replace(/\|\s*birth_date/, "| تاريخ الولادة").replace(/\|\s*birth_place/, "| مكان الولادة").replace(/\|\s*death_date/, "| تاريخ الوفاة").replace(/\|\s*death_place/, "| مكان الوفاة").replace(/\|\s*nationality/, "| الجنسية").replace(/\|\s*known_for/, "| اشتهر بـ").replace(/\|\s*occupation/, "| المهنة").replace(/\|\s*education/, "| التعليم").replace(/\|\s*religion/, "| الديانة").replace(/\|\s*residence/, "| الإقامة").replace(/\|\s*party/, "| الحزب").replace(/\|\s*signature/, "| التوقيع").replace(/\|\s*signature_size/, "| حجم التوقيع")
//صندوق فنان موسيقي
//.replace(/\|\s*name/, "| الاسم").replace(/\|\s*image_size/, "| حجم الصورة").replace(/\|\s*years_active/, "| سنوات النشاط").replace(/\|\s*label/, "| منتج موسيقي").replace("solo_singer", "مغني منفرد").replace(/\|\s*instrument/, "| ألات").replace(/\|\s*past_members/, "| أعضاء سابقون").replace(/\|\s*current_members/, "| أعضاء حاليون").replace(/\|\s*background/, "| خلفية")
//ترجمة صندوق فيلم
//.replace(/\|\s*music/, "| موسيقى").replace(/\|\s*editing/, "| مونتاج").replace(/\|\s*studio/, "| استديو").replace(/\|\s*budget/, "| ميزانية").replace(/\|\s*gross/, "| الإيرادات")
//صندوق مشخصية لعبة فيديو
//.replace(/\|\s*firstgame/, "| أول لعبة").replace(/\|\s*voiceactor/, "| الأداء الصوتي").replace(/\|\s*japanactor/, "| الأداء الصوتي الياباني").replace(/\|\s*motionactor/, "| ممثل الحركات الواقعية").replace(/\|\s*liveactor/, "| التمثيل الحي")
//معلومات شركة
.replace(/\|\s*company_logo/, "| شعار_الشركة").replace(/\|\s*company_type/, "| نوع_الشركة").replace(/\|\s*foundation/, "| تاريخ_التأسيس").replace(/\|\s*key_people/, "| أهم_الشخصيات").replace(/\|\s*industry/, "| الصناعة").replace(/\|\s*products/, "| المنتجات").replace(/\|\s*num_employees/, "| عدد_الموظفين").replace(/\|\s*homepage/, "| الموقع_الإلكتروني").replace(/\|\s*company_name/, "| اسم_الشركة").replace(/\|\s*area_served/, "| مناطق_الخدمة").replace(/\|\s*revenue/, "| العائدات").replace(/\|\s*operating_income/, "| الدخل التشغيلي").replace(/\|\s*parent/, "| الشركة_الأم").replace(/\|\s*divisions/, "| الشركات_التابعة").replace(/\|\s*fate/, "| مصير").replace(/\|\s*predecessor/, "| سابقا").replace(/\|\s*successor/, "| لاحقا").replace(/\|\s*owner/, "| المالك").replace(/\[\[Subsidiary\]\]\sof/, "| [[شركة تابعة]] لـ");
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;
});
}
});