Skip to content

Commit b7763e9

Browse files
authored
auto detect the current locale and set that as default value #103 (#151)
1 parent f70e382 commit b7763e9

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

background.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,38 @@ import * as rwhCompose from './modules/compose.mjs';
1313
import * as rwhTabs from './modules/tabs.mjs';
1414
import * as rwhSettings from './modules/settings.mjs';
1515
import * as rwhAccounts from './modules/accounts.mjs';
16+
import * as rwhI18n from './modules/headers-i18n.mjs';
1617

17-
messenger.runtime.onInstalled.addListener(async function(details) {
18+
messenger.runtime.onInstalled.addListener(async function (details) {
1819
// About 'details' argument
1920
// Refer here: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onInstalled
2021
rwhLogger.debug(details);
2122
let accounts = await rwhAccounts.all();
2223
rwhSettings.setAccountDefaults(accounts);
2324
});
2425

25-
messenger.accounts.onCreated.addListener(async function(id, account){
26+
messenger.accounts.onCreated.addListener(async function (id, account) {
2627
rwhLogger.debug('onCreated', id, account);
2728
if (account.type === 'imap' || account.type === 'pop3') {
2829
rwhSettings.setDefault(`${id}.enabled`, true);
2930
}
3031
});
3132

32-
messenger.accounts.onDeleted.addListener(async function(id){
33+
messenger.accounts.onDeleted.addListener(async function (id) {
3334
rwhLogger.debug('onDeleted', id);
3435
rwhSettings.remove(`${id}.enabled`);
3536
});
3637

38+
async function detectLocaleAndSetAsDefault() {
39+
let uiLocale = messenger.i18n.getUILanguage();
40+
let selected = rwhI18n.i18n.lang[uiLocale];
41+
let currentLocale = await rwhSettings.getHeaderLocale();
42+
rwhLogger.debug('currentLocale:', currentLocale, 'uiLocale:', uiLocale, 'selected:', selected);
43+
if (selected !== 'undefined' && currentLocale !== uiLocale) {
44+
await rwhSettings.set('header.locale', uiLocale);
45+
}
46+
}
47+
3748
async function init() {
3849
await rwhSettings.setDefaults();
3950

@@ -43,11 +54,12 @@ async function init() {
4354
await rwhCompose.process(tab);
4455
});
4556

57+
await detectLocaleAndSetAsDefault();
4658
}
4759

4860
try {
4961
init();
5062
rwhLogger.info('Addon loaded successfully');
51-
} catch(e) {
63+
} catch (e) {
5264
rwhLogger.error(e);
5365
}

0 commit comments

Comments
 (0)