MediaWiki:Gadget-SidebarTranslate.js: Difference between revisions

From Seeds of the Word, the encyclopedia of the influence of the Gospel on culture
(implement bugfix per talk page edit request. Not sure why this was made lazy to begin with)
 
m (1 revision imported)
 
(No difference)

Latest revision as of 20:55, August 30, 2020

/*  _____________________________________________________________________________
 * |                                                                             |
 * |                    === WARNING: GLOBAL GADGET FILE ===                      |
 * |                  Changes to this page affect many users.                    |
 * | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
 * |_____________________________________________________________________________|
 *
 * Imported from [[User:Equazcion/SidebarTranslate.js]] on 2013-11-27
 * See [[User:Equazcion/SidebarTranslate]]
 * Note that when syncing to disable the importStylesheet call.
 */

$( document ).ready( function () {
	'use strict';
	
	var $plang, $plangUL, $interwikis,
		langReg, gtPre, hardLangs, googleSupported, sortByText;

	$plang = $( '#p-lang' );
	$plangUL = $plang.find( 'ul' );
	$interwikis = $plangUL.find( 'li.interlanguage-link' );

	if ( $interwikis.length === 0 ) {
		return;
	}

	$plang.hide();
	$interwikis.detach();

	langReg = /(.* – )?(.*)/;
	gtPre = 'http://translate.google.com/translate?hl=en&sl=auto&tl=en&u=';
	hardLangs = {
		'lumbaart': 'Lombard', 
		'tarandíne': 'Tarantino', 
		'vèneto': 'Venetian',
		'беларуская (тарашкевіца)\u200E': 'Belarusian (Taraškievica)',
		'буряад': 'Buryat', 
		'лакку': 'Lak',
		'भोजपुरी': 'Bhojpuri',
		'تۆرکجه': 'Azerbaijani (South)'
	};
	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.interlanguage-link-target' ).each( function() {
		var $linkTarget, origLangName, langMatches,
			newLangName, hrefs, origLangPre, $langName,
			unsup, $googleLink;
		
		$linkTarget = $( this );
		langMatches = langReg.exec( $linkTarget.attr( 'title' ) );

		if ( langMatches === null ) {
			return;
		}

		origLangName = $linkTarget.text();
		newLangName = ( langMatches[2] in hardLangs ) ? hardLangs[ langMatches[2] ] : langMatches[2];

		hrefs = $linkTarget.attr( 'href' );
		origLangPre = ( langMatches[1] === undefined ) ? '' : langMatches[1];
		$langName = $( '<span>' )
			.attr( 'lang', 'en' )
			.text( newLangName );

		$linkTarget	
			.text( '' )
			.append( $langName )
			.attr( 'title', origLangPre + origLangName );
		unsup = ( googleSupported.indexOf( langMatches[2]) < 0 ) ? '#FA8540' : '#4085FA';
		
		$googleLink = $( '<a>', {
			'text': 'G',
			'class': 'external gTrans',
			'target': '_blank',
			'href': gtPre + hrefs,
			'title': 'Google Translate: ' + newLangName + ' to English',
			'style': 'color:' + unsup + ';',
			'lang': 'en'
		} );
		
		$googleLink
			.insertAfter( $linkTarget )
			.before( ' ' );

		if ( $langName.text().length > 17 ) {
			$langName.html( $langName.html().replace( '-', '- ' ).replace( ' ', '<br />' ) );
		}

		$linkTarget.siblings( '.gTrans' ).hover(
			function() {
				$( this ).css( { 'background-color': unsup, 'color': 'white' } );
			},
			function() {
				$( this ).css( { 'background-color': 'transparent', 'color': unsup } );
			}
		);

		$linkTarget.parent( 'li' ).hover(
			function() {
				$( this ).find( '.gTrans' ).fadeIn( 200 ).css( 'margin-left', '0' );
			},
			function() {
				$( this ).find( '.gTrans' ).fadeOut( 100 ).css( 'margin-left', '5px' );
			}
		);
	} );

	sortByText = function ( a, b ) {
		return ( $( b ).text() ) < ( $( a ).text() ) ? 1 : -1;
	};

	$plangUL.prepend( $interwikis.sort( sortByText ) );
	
	// If gray links added by Content Translation exist,
	// move them back to the top
	$plangUL.find( '.cx-new-interlanguage-link' )
		.detach()
		.prependTo( $plangUL );

	$plang.show();
} );