User:Amire80/Gadget-SidebarTranslate.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Amire80/Gadget-SidebarTranslate. This user script seems to have an accompanying .css page at User:Amire80/Gadget-SidebarTranslate.css. |
/* _____________________________________________________________________________
* | |
* | === 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();
mw.hook( 'mw.uls.compactlinks.initialized' ).add( function ( triggerRendered ) {
debugger;
} );
} );