User:DavRosen/SidebarReorderTranslate.js
//
// Changes sidebar language links to use Google Translate, rearranges them in a specific order, & displays their English names.
//
// The order was chosen based roughly on how many translation engines (of three I knew) support the language, then
// translatability/relatedness to English (Romance ahead of Germanic due to difficulty of verb grammar differences),
// and then some quirky measures based on avg edits per article, active users, etc.
// No doubt you would choose a different order. If you wonder why some small wikipedias are so high on the list,
// remember that it only shows up on a page when that language *does* have an article; how many *other* articles it has is
// less important than the likelihood that this one is good. A WP with very few articles will seldom have a link at all,
// no matter how high it may be on the overall list here.
// (Originally from DavRosen's bookmarklet WpSortLangLinks+LinksToGoogTranslV2draft)
//
// Bugs:
// o Removes the special bullets that identify languages having particularly good articles
// o When clicking on the edit link of a specific section, it has you editing the *next* section instead.
// o On articles with no language links, this script messes up the "Add links" link.
// o On certain pages such as the main page, it doesn't behave as expected...
//
addOnloadHook // for use as bookmarklet, comment out this line and uncomment the next
// javascript:
(function (){
var i, anchors;
var ul = document.querySelectorAll('div#p-lang')[0].querySelectorAll('ul')[0];
ul.insertAdjacentHTML('afterbegin', "
anchors=ul.querySelectorAll('a');
for (i=anchors.length; --i >= 0;){
var x=anchors[i];
var hsplit=x.hostname.split(".");
var lang=hsplit[0];
if (!x.title && !x.href) x.parentNode.style.display="none";
if ( hsplit.length==3 && hsplit[1]=="wikipedia" && hsplit[2]=="org"){
if (a=document.getElementById('SRTL-'+lang)){
var hr=x.href;
var ti=x.title+' ('+(x.textContent||'')+', '+lang+')';
x.removeAttribute('href');
x.parentNode.style.display="none";
a.href=hr;
if(!(a.title&&a.title!=''))a.title=ti;
a.parentNode.setAttribute('class','interwiki-'+lang);
a.parentNode.style.display=null;
if(!(a.parentNode.getAttribute('style')&&a.parentNode.getAttribute('style')!='')) a.parentNode.removeAttribute('style');}}}
/*javascript:*/
/*wpLinksToMsTransl*/(function(){
var i;for (i=document.links.length; --i >= 0;) {
var x=document.links[i], hsplit=x.hostname.split("."), lang=hsplit[0];
if (hsplit.length==3 && hsplit[1]=="wikipedia" && hsplit[2]=="org" && lang!="en" && lang!='simple' && x.hostname!=window.location.hostname) {
var e=(document.charset||document.characterSet);
/* x.appendChild(document.createTextNode(" ("+lang+")")); */ /*append parenthesized language code*/
x.setAttribute('href','http://translate.google.com/translate?u='+encodeURI(
'http://'+x.host+x.pathname+x.hash+x.search)+'&hl=en&langpair=auto|en&tbb=1&ie='+e);}}})()
});
//();
// uncomment line above only for bookmarklet