From 76551ed4a7fccbaf87cd850674406ae316f4f956 Mon Sep 17 00:00:00 2001 From: Amir Sarabadani Date: Mon, 15 Jul 2019 16:11:33 +0200 Subject: [PATCH] Return target of redirect languages in mw.uls.getFrequentLanguageList When mw.uls.getFrequentLanguageList() is requested sometimes the language that is a redirect returned causing other codebases (like Wikibase) to show an invalid language to the user. Bug: T217770 Change-Id: I49c802d584081aa5992dd0ba76144059bcac56c8 --- resources/js/ext.uls.common.js | 7 ++++++- tests/qunit/ext.uls.tests.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/resources/js/ext.uls.common.js b/resources/js/ext.uls.common.js index 3b1a6e37..c12c2a01 100644 --- a/resources/js/ext.uls.common.js +++ b/resources/js/ext.uls.common.js @@ -180,7 +180,7 @@ * @return {Array} List of language codes without duplicates. */ mw.uls.getFrequentLanguageList = function ( countryCode ) { - var i, j, lang, + var i, j, lang, target, ret = [], lists = [ [ @@ -200,6 +200,11 @@ for ( i = 0; i < lists.length; i++ ) { for ( j = 0; j < lists[ i ].length; j++ ) { lang = lists[ i ][ j ]; + target = $.uls.data.isRedirect( lang ); + if ( target ) { + lang = target; + } + // Make flat, make unique, and ignore unknown/unsupported languages if ( ret.indexOf( lang ) === -1 && $.uls.data.getAutonym( lang ) !== lang ) { ret.push( lang ); diff --git a/tests/qunit/ext.uls.tests.js b/tests/qunit/ext.uls.tests.js index 4d7e428c..8598fafe 100644 --- a/tests/qunit/ext.uls.tests.js +++ b/tests/qunit/ext.uls.tests.js @@ -90,4 +90,34 @@ 'Tagalog is one of the languages presented to users in the Philippines.' ); } ); + + QUnit.test( 'Add redirect target', function ( assert ) { + var i, foundTagalog, languages, foundFil; + + foundTagalog = false; + foundFil = false; + mw.uls.getBrowserLanguage = function () { + return 'fil'; + }; + + languages = mw.uls.getFrequentLanguageList(); + + for ( i = 0; i < languages.length; i++ ) { + if ( languages[ i ] === 'tl' ) { + foundTagalog = true; + } + + if ( languages[ i ] === 'fil' ) { + foundFil = true; + } + } + assert.ok( + foundTagalog, + 'Tagalog is one of the languages presented to users when "fil" language is requested' + ); + assert.notOk( + foundFil, + '"fil" language is redirected to Tagalog' + ); + } ); }() );