diff --git a/background.js b/background.js index ed142c7..f569bbb 100644 --- a/background.js +++ b/background.js @@ -13,8 +13,9 @@ import * as rwhCompose from './modules/compose.mjs'; import * as rwhTabs from './modules/tabs.mjs'; import * as rwhSettings from './modules/settings.mjs'; import * as rwhAccounts from './modules/accounts.mjs'; +import * as rwhI18n from './modules/headers-i18n.mjs'; -messenger.runtime.onInstalled.addListener(async function(details) { +messenger.runtime.onInstalled.addListener(async function (details) { // About 'details' argument // Refer here: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onInstalled rwhLogger.debug(details); @@ -22,18 +23,28 @@ messenger.runtime.onInstalled.addListener(async function(details) { rwhSettings.setAccountDefaults(accounts); }); -messenger.accounts.onCreated.addListener(async function(id, account){ +messenger.accounts.onCreated.addListener(async function (id, account) { rwhLogger.debug('onCreated', id, account); if (account.type === 'imap' || account.type === 'pop3') { rwhSettings.setDefault(`${id}.enabled`, true); } }); -messenger.accounts.onDeleted.addListener(async function(id){ +messenger.accounts.onDeleted.addListener(async function (id) { rwhLogger.debug('onDeleted', id); rwhSettings.remove(`${id}.enabled`); }); +async function detectLocaleAndSetAsDefault() { + let uiLocale = messenger.i18n.getUILanguage(); + let selected = rwhI18n.i18n.lang[uiLocale]; + let currentLocale = await rwhSettings.getHeaderLocale(); + rwhLogger.debug('currentLocale:', currentLocale, 'uiLocale:', uiLocale, 'selected:', selected); + if (selected !== 'undefined' && currentLocale !== uiLocale) { + await rwhSettings.set('header.locale', uiLocale); + } +} + async function init() { await rwhSettings.setDefaults(); @@ -43,11 +54,12 @@ async function init() { await rwhCompose.process(tab); }); + await detectLocaleAndSetAsDefault(); } try { init(); rwhLogger.info('Addon loaded successfully'); -} catch(e) { +} catch (e) { rwhLogger.error(e); }