From df70f941b226310d2b1b7e6441be67a032f8ede7 Mon Sep 17 00:00:00 2001 From: Santhosh Thottingal Date: Mon, 11 Jul 2016 14:33:24 +0530 Subject: [PATCH] Show languages that appear in the page's text This is just a simple search for elements with lang attribute. This will be executed only when previous languages, geo-IP based languages, browser, UI, content languages etc. are not filling the defined compact size. Bug: T70077 Change-Id: I77d1efa5e29deeaaf595753fb5519c8b70c7f2b5 --- resources/js/ext.uls.compactlinks.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/resources/js/ext.uls.compactlinks.js b/resources/js/ext.uls.compactlinks.js index 5d04decc..5a738913 100644 --- a/resources/js/ext.uls.compactlinks.js +++ b/resources/js/ext.uls.compactlinks.js @@ -229,6 +229,8 @@ // Add all common languages to the beginning of array. // These are the most probable languages predicted by ULS. this.getCommonLanguages, + // Add languages that are present in the article content. + this.filterByLangsInText, // Some global fallbacks to avoid showing languages in the beginning of the alphabet getExtraCommonLanguages, // Finally add the whole languages array too. @@ -325,6 +327,30 @@ return []; } + /** + * Filter the language list by languages that appear in + * the page's text. This is done by looking for HTML elements with + * a "lang" attribute—they are likely to appear in a foreign name, + * for example. + * + * The reader doesn't necessarily know this language, but it + * appears relevant to the page. + * + * @return {Array} List of language codes supported by the article + */ + CompactInterlanguageList.prototype.filterByLangsInText = function ( languages ) { + var languagesInText = []; + + $( '#mw-content-text [lang]' ).each( function ( i, el ) { + var lang = $( el ).attr( 'lang' ); + if ( $.inArray( lang, languagesInText ) === -1 && $.inArray( lang, languages ) >= 0 ) { + languagesInText.push( lang ); + } + } ); + + return languagesInText; + }; + /** * Find out the existing languages supported * by the article and fetch their href.