Fix locale codes contains underscore will never be preferred (#6116)

feature/default_network_editable
S.C. Chen 6 years ago committed by Whymarrh Whitby
parent 57d458233b
commit 1a4203a8c6
  1. 15
      app/scripts/lib/get-first-preferred-lang-code.js

@ -7,7 +7,13 @@ const getPreferredLocales = extension.i18n ? promisify(
{ errorFirst: false } { errorFirst: false }
) : async () => [] ) : 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 * 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 const firstPreferredLangCode = userPreferredLocaleCodes
.map(code => code.toLowerCase()) .map(code => code.toLowerCase().replace('_', '-'))
.find(code => existingLocaleCodes.includes(code)) .find(code => existingLocaleCodes.hasOwnProperty(code))
return firstPreferredLangCode || 'en'
return existingLocaleCodes[firstPreferredLangCode] || 'en'
} }
module.exports = getFirstPreferredLangCode module.exports = getFirstPreferredLangCode

Loading…
Cancel
Save