User:Proteins/translateinterwikicodes.js: Difference between revisions

Content deleted Content added
Proteins (talk | contribs)
place alert_string restriction elsewhere
m Maintenance: Fixing deprecated call to addPortletLink (mw:ResourceLoader/Migration_guide_(users)#addPortletLink)
 
(33 intermediate revisions by 2 users not shown)
Line 1:
//<pre>
// Translate interwiki codes such as "simple" and "ar"
//
// To use this script, add "importScript('User:Proteins/translateinterwikicodes.js');" to your monobook.js subpage
// under your user page, as you can see at User:Proteins/monobook.js
 
var interwiki_English_names = {
Line 368 ⟶ 371:
function translateInterwikiCodes() {
var alert_string;
var num_lines_printed = 0;
var describe_results = false;
 
var on_main_page = false;
var translate_all_extiw_hyperlinks = true;
 
var parent_node;
Line 380 ⟶ 388:
var language_code;
var English_translation;
 
var num_interwiki_links_translated = 0;
var interwiki_link_string = "interwiki link";
 
// Check whether we're on the Main Page
 
on_main_page = false;
if (document.getElementById("mp-topbanner")) {
on_main_page = true;
// window.alert("We're reading the Main Page.");
}
 
// Loop over the interwiki links
 
alert_string = "";
num_lines_printed = 0;
 
num_hyperlinks = 0;
interwiki_portlet = document.getElementById("p-lang");
Line 396 ⟶ 417:
}
}
interwiki_link_string = "interwiki link";
alert_string += "This page has " + num_hyperlinks + " interwiki links.\n\n";
if (num_hyperlinks != 1) { interwiki_link_string += "s"; };
alert_string += "This page has " + num_hyperlinks + " potential " + interwiki_link_string + ".\n\n";
num_lines_printed += 2;
// window.alert(alert_string); alert_string = "";
 
Line 402 ⟶ 426:
for (hyperlink_index=0; hyperlink_index<num_hyperlinks; hyperlink_index++) {
temp_hyperlink = hyperlinks[hyperlink_index];
 
if (num_lines_printed>45) { // in cases of many error messages
if (describe_results) { window.alert(alert_string); }
alert_string = "";
num_lines_printed = 0;
}
 
parent_node = temp_hyperlink.parentNode;
if (!parent_node) { continue; }
alert_string += "No parent node in hyperlink " + hyperlink_index + "\n";
if ((parent_node.nodeName != "LI") || (!parent_node.className)) { continue; }
num_lines_printed++;
continue;
}
if (parent_node.nodeName != "LI") {
alert_string += "No LI parent node in hyperlink " + hyperlink_index + "\n";
num_lines_printed++;
continue;
}
if (!parent_node.className) {
// alert_string += "No className for parent node in hyperlink " + hyperlink_index + "\n";
num_lines_printed++;
continue;
}
hyperlink_counter++;
language_code = parent_node.className.replace(/interwiki-/, "");
language_code = language_code.replace(/\sFA/, "");
 
English_translation = interwiki_English_names[language_code];
if (!English_translation) {
alert_string += "No English translation for " + language_code + " in hyperlink " + hyperlink_index + "\n";
num_lines_printed++;
continue;
}
hyperlink_counter++;
 
if ((hyperlink_counter%40 <== 411) && (hyperlink_counter > 1)) {
if (describe_results) { window.alert(alert_string); }
alert_string += "Interwiki link " + hyperlink_counter + " : " + language_code + " => " + English_translation + "\n";
alert_string = "Continued...\n\n";
}
alert_string += "Interwiki link " + hyperlink_counter + " : " + language_code + " => " + English_translation + "\n";
temp_hyperlink.innerHTML = English_translation;
} // closes loop over interwiki links
alert_stringnum_interwiki_links_translated += "\nTranslated " + hyperlink_counter + " interwiki links into English.";
window.alert(alert_string);
 
interwiki_link_string = "interwiki link";
if (num_interwiki_links_translated != 1) { interwiki_link_string += "s"; };
alert_string += "\nTranslated " + num_interwiki_links_translated + " " + interwiki_link_string + " into English.";
if (describe_results) { window.alert(alert_string); }
 
// Translate other interwiki links; removed restriction to the Main Page
alert_string = "";
translate_all_extiw_hyperlinks = true;
if ((translate_all_extiw_hyperlinks) || (on_main_page)) {
num_hyperlinks = 0;
 
hyperlinks = document.getElementsByTagName("A"); // earlier code was restricted to mp-lang on Main Page
if (!hyperlinks) {
alert_string += "Document hyperlinks are undefined.\n";
} else {
num_hyperlinks = hyperlinks.length; // number of potential extiw hyperlinks
}
 
hyperlink_counter = 0;
for (hyperlink_index=0; hyperlink_index<num_hyperlinks; hyperlink_index++) {
temp_hyperlink = hyperlinks[hyperlink_index];
 
if (temp_hyperlink.className != "extiw") { continue; }
if (temp_hyperlink.title == "m:List of Wikipedias") { continue; }
 
language_code = temp_hyperlink.title.replace(/:/, "");
 
English_translation = interwiki_English_names[language_code];
if (!English_translation) {
alert_string += "No English translation for " + language_code + " in hyperlink " + hyperlink_index + "\n";
continue;
}
hyperlink_counter++;
if ((hyperlink_counter%40 == 1) && (hyperlink_counter > 1)) {
if (describe_results) { window.alert(alert_string); }
alert_string = "Continued...\n\n";
}
alert_string += "Interwiki link " + hyperlink_counter + " : " + language_code + " => " + English_translation + "\n";
temp_hyperlink.innerHTML = English_translation;
} // closes loop over interwiki links
num_interwiki_links_translated += hyperlink_counter; // Increment the number translated
 
interwiki_link_string = "interwiki link";
if (hyperlink_counter != 1) { interwiki_link_string += "s"; };
alert_string += "\nTranslated " + hyperlink_counter + " " + interwiki_link_string + " at the bottom of the Main Page into English.";
if (describe_results) { window.alert(alert_string); }
} // closes check for being on the Main Page
 
// Minimal acknowledgment message
if (!describe_results) {
interwiki_link_string = "interwiki link";
if (num_interwiki_links_translated != 1) { interwiki_link_string += "s"; };
alert_string = "\nTranslated " + num_interwiki_links_translated + " " + interwiki_link_string + " into English.";
window.alert(alert_string);
}
} // closes function translateInterwikiCodes()
addOnloadHook$(function () {
mw.util.addPortletLink('p-cactionstb', 'javascript:translateInterwikiCodes()', 'EnglishTranslate interwikisinterwiki links', 'ca-translate', 'Translates interwiki links into English', 'i', '');
});