|
|
|
@ -2,8 +2,7 @@ const extension = require('extensionizer') |
|
|
|
|
const promisify = require('pify') |
|
|
|
|
const allLocales = require('../../_locales/index.json') |
|
|
|
|
|
|
|
|
|
const isSupported = extension.i18n && extension.i18n.getAcceptLanguages |
|
|
|
|
const getPreferredLocales = isSupported ? promisify( |
|
|
|
|
const getPreferredLocales = extension.i18n ? promisify( |
|
|
|
|
extension.i18n.getAcceptLanguages, |
|
|
|
|
{ errorFirst: false } |
|
|
|
|
) : async () => [] |
|
|
|
@ -18,7 +17,21 @@ const existingLocaleCodes = allLocales.map(locale => locale.code.toLowerCase().r |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
async function getFirstPreferredLangCode () { |
|
|
|
|
const userPreferredLocaleCodes = await getPreferredLocales() |
|
|
|
|
let userPreferredLocaleCodes |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
userPreferredLocaleCodes = await getPreferredLocales() |
|
|
|
|
} catch (e) { |
|
|
|
|
// Brave currently throws when calling getAcceptLanguages, so this handles that.
|
|
|
|
|
userPreferredLocaleCodes = [] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// safeguard for Brave Browser until they implement chrome.i18n.getAcceptLanguages
|
|
|
|
|
// https://github.com/MetaMask/metamask-extension/issues/4270
|
|
|
|
|
if (!userPreferredLocaleCodes){ |
|
|
|
|
userPreferredLocaleCodes = [] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const firstPreferredLangCode = userPreferredLocaleCodes |
|
|
|
|
.map(code => code.toLowerCase()) |
|
|
|
|
.find(code => existingLocaleCodes.includes(code)) |
|
|
|
@ -26,3 +39,4 @@ async function getFirstPreferredLangCode () { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
module.exports = getFirstPreferredLangCode |
|
|
|
|
|
|
|
|
|