Skip to content

Commit

Permalink
auto detect the current locale and set that as default value #103 (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm authored Jul 1, 2024
1 parent f70e382 commit b7763e9
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,38 @@ 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);
let accounts = await rwhAccounts.all();
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();

Expand All @@ -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);
}

0 comments on commit b7763e9

Please sign in to comment.