jenkins-bot has submitted this change and it was merged.

Change subject: Structured language overlay: prioritize available general 
language ...
......................................................................


Structured language overlay: prioritize available general language ...

... if user's specific client side variant isn't available. For example,
if the device language is 'es-lx' and the article is not avialable in this
variant, but available in 'es', then add 'es' to the list of preferred
languages.

Also fix a typo in one of the tests.

Bug: T126868
Change-Id: I18164925563ab88367f738dae4c97af0bffc468f
---
M resources/mobile.languages.structured/util.js
M tests/qunit/mobile.languages.structured/test_LanguageOverlay.js
M tests/qunit/mobile.languages.structured/test_util.js
3 files changed, 72 insertions(+), 8 deletions(-)

Approvals:
  Jdlrobson: Looks good to me, approved
  Bmansurov: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/resources/mobile.languages.structured/util.js 
b/resources/mobile.languages.structured/util.js
index 23e5eb4..662f8ab 100644
--- a/resources/mobile.languages.structured/util.js
+++ b/resources/mobile.languages.structured/util.js
@@ -8,6 +8,48 @@
        var util = {};
 
        /**
+        * Return the device language if it's in the list of article languages.
+        * If the language is a variant of a general language, and if the 
article
+        * is not available in that language, then return the general language
+        * if article is available in it. For example, if the device language is
+        * 'en-gb', and the article is only available in 'en', then return 'en'.
+        *
+        * @ignore
+        * @param {Object[]} languages list of language objects as returned by 
the API
+        * @param {String|undefined} deviceLanguage the device's primary 
language
+        * @returns {String|undefined} Return undefined if the article is not 
available in
+        *  the (general or variant) device language
+        */
+       function getDeviceLanguageOrParent( languages, deviceLanguage ) {
+               var parentLanguage, index,
+                       deviceLanguagesWithVariants = {};
+
+               if ( !deviceLanguage ) {
+                       return;
+               }
+
+               // Are we dealing with a variant?
+               index = deviceLanguage.indexOf( '-' );
+               if ( index !== -1 ) {
+                       parentLanguage = deviceLanguage.slice( 0, index );
+               }
+
+               $.each( languages, function ( i, language ) {
+                       if ( language.lang === parentLanguage || language.lang 
=== deviceLanguage ) {
+                               deviceLanguagesWithVariants[ language.lang ] = 
true;
+                       }
+               } );
+
+               if ( deviceLanguagesWithVariants.hasOwnProperty( deviceLanguage 
) ) {
+                       // the device language is one of the available languages
+                       return deviceLanguage;
+               } else if ( deviceLanguagesWithVariants.hasOwnProperty( 
parentLanguage ) ) {
+                       // no device language, but the parent language is one 
of the available languages
+                       return parentLanguage;
+               }
+       }
+
+       /**
         * Return two sets of languages: preferred and all (everything else)
         *
         * Preferred languages are the ones that the user has used before. This 
also
@@ -26,16 +68,23 @@
         * @return {Object[]}
         */
        util.getStructuredLanguages = function ( languages, 
frequentlyUsedLanguages, deviceLanguage ) {
-               var index, lang, variant, allLanguages,
+               var index, lang, variant, allLanguages, maxFrequency = 0,
                        parentsMap = {},
                        variantsMap = {},
                        preferredLanguages = [],
                        preferredLanguagesMap = {};
 
-               // User's device language will be included to the frequently 
used languages
-               // map and gets the top priority
+               $.each( frequentlyUsedLanguages, function ( language, frequency 
) {
+                       maxFrequency = maxFrequency < frequency ? frequency : 
maxFrequency;
+               } );
+
+               // Is the article available in the user's device language?
+               deviceLanguage = getDeviceLanguageOrParent( languages, 
deviceLanguage );
+
                if ( deviceLanguage ) {
-                       frequentlyUsedLanguages[ deviceLanguage ] = 101;
+                       // Make the device language the most frequently used 
one so that
+                       // it appears at the top of the list when sorted by 
frequency.
+                       frequentlyUsedLanguages[ deviceLanguage ] = 
maxFrequency + 1;
                }
 
                // Separate languages into preferred, parent, and variants. 
Parent
diff --git a/tests/qunit/mobile.languages.structured/test_LanguageOverlay.js 
b/tests/qunit/mobile.languages.structured/test_LanguageOverlay.js
index 34d84b8..6f1c648 100644
--- a/tests/qunit/mobile.languages.structured/test_LanguageOverlay.js
+++ b/tests/qunit/mobile.languages.structured/test_LanguageOverlay.js
@@ -111,7 +111,7 @@
                assert.ok(
                        this.languageOverlay.$( '.site-link-list 
a:not(.hidden)' ).length === 1 &&
                        this.languageOverlay.$( '.site-link-list 
a:not(.hidden)' ).hasClass( 'be-x-old' ),
-                       'One language (be-x-old) matches "old" and only that 
language is visible.'
+                       'One language (be-x-old) matches "ol" and only that 
language is visible.'
                );
 
                this.languageOverlay.filterLanguages( '' );
diff --git a/tests/qunit/mobile.languages.structured/test_util.js 
b/tests/qunit/mobile.languages.structured/test_util.js
index bf38d93..752c70e 100644
--- a/tests/qunit/mobile.languages.structured/test_util.js
+++ b/tests/qunit/mobile.languages.structured/test_util.js
@@ -24,6 +24,11 @@
                                        title: 'Барак Абама',
                                        langname: 'беларуская (тарашкевіца)'
                                }, {
+                                       lang: 'es',
+                                       url: 
'https://en.wikipedia.org/wiki/Barack_Obama',
+                                       title: 'Barack Obama',
+                                       langname: 'Spanish'
+                               }, {
                                        lang: 'ko',
                                        url: 
'https://ko.wikipedia.org/wiki/%EB%B2%84%EB%9D%BD_%EC%98%A4%EB%B0%94%EB%A7%88',
                                        title: '버락 오바마',
@@ -66,7 +71,6 @@
                        this.frequentlyUsedLanguages = {
                                'zh-min-nan': 1,
                                zh: 2,
-                               en: 10,
                                ko: 1
                        };
 
@@ -113,6 +117,11 @@
                                                hasVariants: true,
                                                variantsHeader: 'беларуская'
                                        }, {
+                                               lang: 'es',
+                                               url: 
'https://en.wikipedia.org/wiki/Barack_Obama',
+                                               title: 'Barack Obama',
+                                               langname: 'Spanish'
+                                       }, {
                                                lang: 'ru',
                                                url: 
'https://ru.wikipedia.org/wiki/%D0%9E%D0%B1%D0%B0%D0%BC%D0%B0,_%D0%91%D0%B0%D1%80%D0%B0%D0%BA',
                                                title: 'Обама, Барак',
@@ -143,14 +152,13 @@
                }
        } );
 
-       QUnit.test( 'test utility functions', 3, function ( assert ) {
+       QUnit.test( 'test utility functions', 4, function ( assert ) {
                assert.deepEqual( util.getFrequentlyUsedLanguages(), 
this.frequentlyUsedLanguages, 'Frequently used languages is correct.' );
 
                util.saveLanguageUsageCount( 'ko', 
util.getFrequentlyUsedLanguages() );
                assert.ok( this.saveSpy.calledWith( {
                        'zh-min-nan': 1,
                        zh: 2,
-                       en: 10,
                        ko: 2
                } ), 'Frequently used language is correctly saved.' );
 
@@ -159,6 +167,13 @@
                        this.structuredLanguages,
                        'Structured languages are correct.'
                );
+
+               // device language is a variant and only the parent language is 
available
+               assert.equal(
+                       util.getStructuredLanguages( this.apiLanguages, {}, 
'es-lx' ).preferred[0].lang,
+                       'es',
+                       '"es" is correctly selected as a preferred language 
even though the device language is "es-lx".'
+               );
        } );
 
 } )( mw.mobileFrontend );

-- 
To view, visit https://gerrit.wikimedia.org/r/270882
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I18164925563ab88367f738dae4c97af0bffc468f
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Bmansurov <bmansu...@wikimedia.org>
Gerrit-Reviewer: Bmansurov <bmansu...@wikimedia.org>
Gerrit-Reviewer: Jdlrobson <jrob...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to