From 1a4203a8c608c1ee9e13d4196f1ee0c6cac6886e Mon Sep 17 00:00:00 2001 From: "S.C. Chen" Date: Sat, 9 Feb 2019 03:36:37 +0800 Subject: [PATCH] Fix locale codes contains underscore will never be preferred (#6116) --- app/scripts/lib/get-first-preferred-lang-code.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/scripts/lib/get-first-preferred-lang-code.js b/app/scripts/lib/get-first-preferred-lang-code.js index 170d508c1..469235357 100644 --- a/app/scripts/lib/get-first-preferred-lang-code.js +++ b/app/scripts/lib/get-first-preferred-lang-code.js @@ -7,7 +7,13 @@ const getPreferredLocales = extension.i18n ? promisify( { errorFirst: false } ) : async () => [] -const existingLocaleCodes = allLocales.map(locale => locale.code.toLowerCase().replace('_', '-')) +// mapping some browsers return hyphen instead underscore in locale codes (e.g. zh_TW -> zh-tw) +const existingLocaleCodes = {} +allLocales.forEach(locale => { + if (locale && locale.code) { + existingLocaleCodes[locale.code.toLowerCase().replace('_', '-')] = locale.code + } +}) /** * Returns a preferred language code, based on settings within the user's browser. If we have no translations for the @@ -33,9 +39,10 @@ async function getFirstPreferredLangCode () { } const firstPreferredLangCode = userPreferredLocaleCodes - .map(code => code.toLowerCase()) - .find(code => existingLocaleCodes.includes(code)) - return firstPreferredLangCode || 'en' + .map(code => code.toLowerCase().replace('_', '-')) + .find(code => existingLocaleCodes.hasOwnProperty(code)) + + return existingLocaleCodes[firstPreferredLangCode] || 'en' } module.exports = getFirstPreferredLangCode