User:Proteins/translateinterwikicodes.js: Difference between revisions

Content deleted Content added
Proteins (talk | contribs)
allow for more than forty interwiki links to be displayed
m Maintenance: Fixing deprecated call to addPortletLink (mw:ResourceLoader/Migration_guide_(users)#addPortletLink)
 
(25 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 382 ⟶ 389:
var English_translation;
 
var num_interwiki_links_translated = 0;
var interwiki_link_string = "interwiki link";
 
// Check whether we're on the Main Page
Line 394 ⟶ 403:
 
alert_string = "";
num_lines_printed = 0;
 
num_hyperlinks = 0;
interwiki_portlet = document.getElementById("p-lang");
Line 406 ⟶ 417:
}
}
interwiki_link_string = "interwiki link";
alert_string += "This page has " + num_hyperlinks + " potential interwiki links.\n\n";
if (num_hyperlinks != 1) { interwiki_link_string += "s"; };
alert_string += "This page has " + num_hyperlinks + " potential interwiki" links+ interwiki_link_string + ".\n\n";
num_lines_printed += 2;
// window.alert(alert_string); alert_string = "";
 
Line 412 ⟶ 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); }
window.alert( alert_string) = "";
num_lines_printed = 0;
}
 
parent_node = temp_hyperlink.parentNode;
if (!parent_node) {
alert_string += "No parent node in hyperlink " + hyperlink_index + "\n";
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;
}
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;
}
Line 436 ⟶ 462:
 
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
alert_stringnum_interwiki_links_translated += "\nTranslated " + hyperlink_counter + " interwiki links into English.";
window.alert(alert_string);
 
interwiki_link_string = "interwiki link";
// Translate other interwiki links, but only on the Main Page
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,; butremoved onlyrestriction onto the Main Page
alert_string = "";
translate_all_extiw_hyperlinks = true;
if (on_main_page) {
if ((translate_all_extiw_hyperlinks) || (on_main_page)) {
num_hyperlinks = 0;
 
wikipedia_languages = document.getElementById("mp-lang");
hyperlinks = document.getElementsByTagName("A"); // earlier code was restricted to mp-lang on Main Page
if (!wikipedia_languages) {
if (!hyperlinks) {
alert_string += "TheDocument Wikipediahyperlinks languages section of the Main Page isare undefined.\n";
} else {
num_hyperlinks = hyperlinks.length; // number of potential extiw hyperlinks
hyperlinks = wikipedia_languages.getElementsByTagName("A");
if (!hyperlinks) {
alert_string += "Interwiki hyperlinks are undefined.\n";
} else {
num_hyperlinks = hyperlinks.length;
}
}
alert_string += "The bottom of the Main Page has " + num_hyperlinks + " potential interwiki links.\n\n";
// window.alert(alert_string); alert_string = "";
 
hyperlink_counter = 0;
Line 468 ⟶ 493:
 
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) {
Line 477 ⟶ 504:
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
alert_stringnum_interwiki_links_translated += "\nTranslated " + hyperlink_counter; +// "Increment morethe interwikinumber links into English.";translated
 
window.alert(alert_string);
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); 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', '');
});