Jdrewniak has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/383339 )
Change subject: Use the browsers full language codes for translation ...................................................................... Use the browsers full language codes for translation Attempts to use the full language code to translate the page if a translation is available. If a translation for the full language code is unavailable, falls back to the Iso639 version of the code, then falls back to english. Bug: T171647 Change-Id: Iecc9a78a75f89be93e7965bfebaf8643aa220814 --- M dev/wikipedia.org/assets/js/page-localized.js M dev/wikipedia.org/assets/js/topten-localized.js M dev/wikipedia.org/assets/js/wm-test.js 3 files changed, 43 insertions(+), 11 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/wikimedia/portals refs/changes/39/383339/1 diff --git a/dev/wikipedia.org/assets/js/page-localized.js b/dev/wikipedia.org/assets/js/page-localized.js index fb1ba47..eb4b858 100644 --- a/dev/wikipedia.org/assets/js/page-localized.js +++ b/dev/wikipedia.org/assets/js/page-localized.js @@ -1,4 +1,4 @@ -/* global wmTest, translationsHash, mw, wmL10nVisible, rtlLangs, isTranslationAvailable */ +/* global wmTest, translationsHash, mw, wmL10nVisible, rtlLangs */ /** * This script localizes the page text. @@ -13,7 +13,7 @@ ( function ( wmTest, translationsHash, mw, rtlLangs ) { - var primaryLang = isTranslationAvailable( wmTest.userLangs[ 0 ] ), + var primaryLang = wmTest.userLangsFullCodes[ 0 ], storedTranslationHash, storedTranslations, l10nReq, diff --git a/dev/wikipedia.org/assets/js/topten-localized.js b/dev/wikipedia.org/assets/js/topten-localized.js index f67cf01..7468f4a 100644 --- a/dev/wikipedia.org/assets/js/topten-localized.js +++ b/dev/wikipedia.org/assets/js/topten-localized.js @@ -1,4 +1,4 @@ -/* global wmTest, translationsHash, wmL10nVisible, rtlLangs, isTranslationAvailable */ +/* global wmTest, translationsHash, wmL10nVisible, rtlLangs */ /** * This code was used to localize the top-ten language links * for the A/B test titled "A/B test: browser language detection" @@ -12,7 +12,7 @@ function localizeTopTen() { - var preferredLanguages = wmTest.userLangs, + var preferredLanguages = wmTest.userLangsFullCodes, i, topLinks = document.querySelectorAll( '.central-featured-lang' ), topLinksContainer = document.querySelector( '.central-featured' ), topLinkLangs, @@ -83,7 +83,7 @@ function mergeNewTopLinkLangs() { var pl, plIndex, plExists, plRightSpot; for ( i = 0; i < preferredLanguages.length; i++ ) { - pl = isTranslationAvailable( preferredLanguages[ i ] ); + pl = preferredLanguages[ i ]; plIndex = topLinkLangs.indexOf( pl ); plExists = plIndex >= 0; plRightSpot = plIndex === i; diff --git a/dev/wikipedia.org/assets/js/wm-test.js b/dev/wikipedia.org/assets/js/wm-test.js index f8fa6a5..47f6d49 100644 --- a/dev/wikipedia.org/assets/js/wm-test.js +++ b/dev/wikipedia.org/assets/js/wm-test.js @@ -1,4 +1,4 @@ -/* global eventLoggingLite, getIso639 */ +/* global eventLoggingLite, getIso639, isTranslationAvailable */ window.wmTest = ( function ( eventLoggingLite, mw ) { @@ -16,18 +16,22 @@ SESSION_ID: 'portal_session_id', EXPIRES: 'portal_test_group_expires' }, - preferredLangs, sessionId, group, testOnly; + preferredLangs, preferredLangsFullCodes, sessionId, group, testOnly; /** - * Created an array of preferred languages in ISO939 format. + * Created an array of preferred languages. + * By default formats the language codes to ISO939 format. + * With optional argument can return the full language code. * + * @param {Object} options - an object with prop fullCodes (boolean) * @return {Array} langs */ - function setPreferredLanguages() { - var langs = [], possibleLanguage, i; + function setPreferredLanguages( options ) { + var langs = [], possibleLanguage, i, + opt = options || {}; function appendLanguage( l ) { - var lang = getIso639( l ); + var lang = ( opt.fullCodes ) ? l : getIso639( l ); if ( lang && langs.indexOf( lang ) < 0 ) { langs.push( lang ); } @@ -53,7 +57,30 @@ return langs; } + /** + * For l10n purposes, de-dups and verifies that a translation file is + * available for the languages in the prefferedLangs. + * @param {array} langCodes - array of language codes + * @return {array} - arry of verified and de-duplicated language codes + */ + function verifyLanguageCodes( langCodes ) { + var i, availableLang, verifiedl10n = []; + if ( !langCodes.length ) { + // if language codes are empty, return 'en' as the default. + return [ 'en' ]; + } + + for ( i = 0; i < langCodes.length; i++ ) { + availableLang = isTranslationAvailable( langCodes[ i ] ); + if ( verifiedl10n.indexOf( availableLang ) === -1 ) { + verifiedl10n.push( availableLang ); + } + } + return verifiedl10n; + } + preferredLangs = setPreferredLanguages(); + preferredLangsFullCodes = verifyLanguageCodes( setPreferredLanguages( { fullCodes: true } ) ); /** * Determines whether the user is part of the population size. @@ -157,6 +184,11 @@ userLangs: preferredLangs, /** + * Users preferred languages with full language codes. + */ + userLangsFullCodes: preferredLangsFullCodes, + + /** * User population group * * @type {string} -- To view, visit https://gerrit.wikimedia.org/r/383339 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iecc9a78a75f89be93e7965bfebaf8643aa220814 Gerrit-PatchSet: 1 Gerrit-Project: wikimedia/portals Gerrit-Branch: master Gerrit-Owner: Jdrewniak <jdrewn...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits