ext.uls.compactlinks: consistently normalize language codes

Create a function for this purpose and use it everywhere where
language code is read from an attribute.

Bug: T143867
Change-Id: I01b6f04dd9185a9f4ab565e96ca5a6a34e4ea14a
This commit is contained in:
Niklas Laxström
2016-08-25 09:27:58 +02:00
committed by Nikerabbit
parent 694f3719a8
commit 2144019c50

View File

@@ -35,6 +35,20 @@
} ) ); } ) );
} }
/**
* Normalize a language code for ULS usage.
*
* MediaWiki language codes (especially on WMF sites) are inconsistent
* with ULS codes. We need to use ULS codes to access the proper data.
*
* @param {string} code
* @return {string} Normalized language code
*/
function convertMediaWikiLanguageCodeToULS( code ) {
code = code.toLowerCase();
return $.uls.data.isRedirect( code ) || code;
}
/** /**
* @class * @class
*/ */
@@ -357,7 +371,7 @@
var languagesInText = []; var languagesInText = [];
$( '#mw-content-text [lang]' ).each( function ( i, el ) { $( '#mw-content-text [lang]' ).each( function ( i, el ) {
var lang = $( el ).attr( 'lang' ); var lang = convertMediaWikiLanguageCodeToULS( $( el ).attr( 'lang' ) );
if ( $.inArray( lang, languagesInText ) === -1 && $.inArray( lang, languages ) >= 0 ) { if ( $.inArray( lang, languagesInText ) === -1 && $.inArray( lang, languages ) >= 0 ) {
languagesInText.push( lang ); languagesInText.push( lang );
} }
@@ -378,8 +392,8 @@
*/ */
CompactInterlanguageList.prototype.filterByBadges = function () { CompactInterlanguageList.prototype.filterByBadges = function () {
return $( '#p-lang' ).find( '[class*="badge"]' ).map( function ( i, el ) { return $( '#p-lang' ).find( '[class*="badge"]' ).map( function ( i, el ) {
return $( el ).find( 'a' ).attr( 'lang' ).toLowerCase(); } return convertMediaWikiLanguageCodeToULS( $( el ).find( 'a' ).attr( 'lang' ) );
).toArray(); } ).toArray();
}; };
/** /**
@@ -392,10 +406,8 @@
var interlanguageList = {}; var interlanguageList = {};
this.$interlanguageList.find( 'li.interlanguage-link > a' ).each( function () { this.$interlanguageList.find( 'li.interlanguage-link > a' ).each( function () {
var langCode = this.getAttribute( 'lang' ).toLowerCase(); var langCode = convertMediaWikiLanguageCodeToULS( this.getAttribute( 'lang' ) );
// We keep interlanguageList with redirect resolved language codes as keys.
langCode = $.uls.data.isRedirect( langCode ) || langCode;
interlanguageList[ langCode ] = { interlanguageList[ langCode ] = {
href: this.getAttribute( 'href' ), href: this.getAttribute( 'href' ),
autonym: $( this ).text(), autonym: $( this ).text(),