From b9309f689be7f55fde0a32a6e576784aa9fc1061 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 22 Mar 2018 12:39:16 -0230 Subject: Use extension api to get initial locale. --- app/scripts/background.js | 10 ++++++---- app/scripts/controllers/preferences.js | 2 +- app/scripts/lib/get-first-preferred-lang-code.js | 18 ++++++++++++++++++ app/scripts/metamask-controller.js | 2 +- package.json | 1 + 5 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 app/scripts/lib/get-first-preferred-lang-code.js diff --git a/app/scripts/background.js b/app/scripts/background.js index 8bd7766ad..c8fe1cd63 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -19,7 +19,7 @@ const setupRaven = require('./lib/setupRaven') const reportFailedTxToSentry = require('./lib/reportFailedTxToSentry') const setupMetamaskMeshMetrics = require('./lib/setupMetamaskMeshMetrics') const EdgeEncryptor = require('./edge-encryptor') - +const getFirstPreferredLangCode = require('./lib/get-first-preferred-lang-code') const STORAGE_KEY = 'metamask-config' const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG' @@ -58,7 +58,8 @@ setupMetamaskMeshMetrics() async function initialize () { const initState = await loadStateFromPersistence() - await setupController(initState) + const initLangCode = await getFirstPreferredLangCode() + await setupController(initState, initLangCode) log.debug('MetaMask initialization complete.') } @@ -90,11 +91,10 @@ async function loadStateFromPersistence () { return versionedData.data } -function setupController (initState) { +function setupController (initState, initLangCode) { // // MetaMask Controller // - const controller = new MetamaskController({ // User confirmation callbacks: showUnconfirmedMessage: triggerUi, @@ -102,6 +102,8 @@ function setupController (initState) { showUnapprovedTx: triggerUi, // initial state initState, + // initial locale code + initLangCode, // platform specific api platform, encryptor: isEdge ? new EdgeEncryptor() : undefined, diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js index dc7da90d0..b4819d951 100644 --- a/app/scripts/controllers/preferences.js +++ b/app/scripts/controllers/preferences.js @@ -11,7 +11,7 @@ class PreferencesController { tokens: [], useBlockie: false, featureFlags: {}, - currentLocale: 'ja', + currentLocale: opts.initLangCode, }, opts.initState) this.store = new ObservableStore(initState) } diff --git a/app/scripts/lib/get-first-preferred-lang-code.js b/app/scripts/lib/get-first-preferred-lang-code.js new file mode 100644 index 000000000..74dfe246c --- /dev/null +++ b/app/scripts/lib/get-first-preferred-lang-code.js @@ -0,0 +1,18 @@ +const fs = require('fs') +const path = require('path') +const extension = require('extensionizer') +const promisify = require('pify') + +const existingLocaleCodes = fs.readdirSync(path.join(__dirname, '..', '..', '_locales')) + +async function getFirstPreferredLangCode () { + const userPreferredLocaleCodes = await promisify( + extension.i18n.getAcceptLanguages, + { errorFirst: false } + )().catch(err => console.log('err123', err)) + const firstPreferredLangCode = userPreferredLocaleCodes.find(code => existingLocaleCodes.includes(code)) + // const firstPreferredLangCode = userPreferredLocaleCodes[0] + return firstPreferredLangCode || 'en' +} + +module.exports = getFirstPreferredLangCode diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index bd092cea0..6e82f6011 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -57,7 +57,6 @@ module.exports = class MetamaskController extends EventEmitter { this.defaultMaxListeners = 20 this.sendUpdate = debounce(this.privateSendUpdate.bind(this), 200) - this.opts = opts const initState = opts.initState || {} this.recordFirstTimeInfo(initState) @@ -82,6 +81,7 @@ module.exports = class MetamaskController extends EventEmitter { // preferences controller this.preferencesController = new PreferencesController({ initState: initState.PreferencesController, + initLangCode: opts.initLangCode, }) // currency controller diff --git a/package.json b/package.json index 1aae1092e..f10491fd1 100644 --- a/package.json +++ b/package.json @@ -141,6 +141,7 @@ "promise-filter": "^1.1.0", "promise-to-callback": "^1.0.0", "pump": "^3.0.0", + "pify": "^3.0.0", "pumpify": "^1.3.4", "qrcode-npm": "0.0.3", "ramda": "^0.24.1", -- cgit v1.2.3