Jump to content

User:Equazcion/SidebarTranslate.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
var plang = $('#p-lang').hide();

$(document).ready( function() {
	function sort( a, b ) {
		return ( $(b).text() ) < ( $(a).text() ) ? 1 : -1;
	}
	var plangUL = plang.find('ul');
	var interwikis = plangUL.find('li[class^="interlanguage-link"]').detach();
	if ( interwikis.length > 0 ) {
		importStylesheet('User:Equazcion/SidebarTranslate.css');
		var langReg = /(.*? – )?(.*)/;
		var gtPre = 'http://translate.google.com/translate?hl=en&sl=auto&tl=en&u=';
		var hardLangs = {
			'lumbaart': 'Lombard', 
			'tarandíne': 'Tarantino', 
			'vèneto': 'Venetian', 
			'Беларуская (тарашкевіца)': 'Belarusian (Taraškievica)',
			'буряад': 'Buryat', 
			'лакку': 'Lak'
		};
		var googleSupported = ['Afrikaans', 'Albanian', 'Arabic', 'Armenian', 'Azerbaijani', 'Basque', 'Belarusian', 'Bengali', 'Bulgarian',
			'Catalan', 'Chinese', 'Croatian', 'Czech', 'Danish', 'Dutch', 'English', 'Esperanto', 'Estonian', 'Filipino', 'Finnish',
			'French', 'Galician', 'Georgian', 'German', 'Greek', 'Haitian', 'Creole', 'Hebrew', 'Hindi', 'Hungarian', 'Icelandic',
			'Indonesian', 'Irish', 'Italian', 'Japanese', 'Javanese', 'Kannada', 'Korean', 'Khmer', 'Latin', 'Latvian', 'Lithuanian',
			'Macedonian', 'Malay', 'Maltese', 'Marathi', 'Norwegian', 'Norwegian (bokmål)', 'Norwegian Nynorsk', 'Persian', 'Polish',
			'Portuguese', 'Romanian', 'Russian', 'Serbian', 'Slovak', 'Slovenian', 'Spanish', 'Swahili', 'Swedish', 'Tamil',
			'Telugu', 'Thai', 'Turkish', 'Ukrainian', 'Urdu', 'Vietnamese', 'Welsh', 'Yiddish'];
		
		interwikis.find('a').each( function() {
			var $this = $(this);
			var origLangName = $this.text();
			var langMatches = langReg.exec( $this.attr('title') );
			if ( langMatches != null ) {
				var newLangName = (langMatches[2] in hardLangs) ? hardLangs[ langMatches[2] ] : langMatches[2];
					if ( $this.attr('hreflang') == 'be-x-old' ) newLangName = 'Belarusian (Taraškievica)'; // Doesn't match object key for some reason
				var hrefs = $this.attr('href');
				var origLangPre = (langMatches[1] == undefined) ? '' : langMatches[1];
				$this.text( newLangName ).removeAttr('lang style').attr( 'title', origLangPre + origLangName );
				var unsup = ( googleSupported.indexOf(langMatches[2]) < 0 ) ? '#FA8540' : '#4085FA';
				$('<a/>', {
					'text': 'G',
					'class': 'external gTrans',
					'target': '_blank',
					'href': gtPre + hrefs,
					'title': 'Google Translate: ' + newLangName + ' to English',
					'style': 'color:' + unsup + ';'
				}).insertAfter( $this ).before(' ');
				if ( $this.text().length > 17 )  $this.html( $this.html().replace('-','- ').replace(' ', '<br />') );
				$this.siblings('.gTrans').hover(
					function() { $(this).css( {'background-color': unsup, 'color': 'white'} ) },
					function() { $(this).css( {'background-color': 'transparent', 'color': unsup} ) }
				);
				$this.parent('li').hover(
					function() { $(this).find('.gTrans').fadeIn(200).css('margin-left','0') },
					function() { $(this).find('.gTrans').fadeOut(100).css('margin-left','5px') }
				);
			}
		});
		plangUL.prepend( interwikis.sort(sort) );
	}
	plang.show();
});