use local storage to cache locale choice

This commit is contained in:
wixette 2020-03-08 16:55:36 +08:00
parent a15dd897ae
commit af9abdaf4f
2 changed files with 23 additions and 0 deletions

View File

@ -72,6 +72,11 @@ l10n.MESSAGES = {
*/
l10n.current = 0;
/**
* Local storage key.
*/
l10n.localStorageKey = 'sim8800locale';
/**
* Switches to the next locale.
*/
@ -79,6 +84,21 @@ l10n.nextLocale = function() {
l10n.current++;
l10n.current = l10n.current % l10n.LOCALES.length;
l10n.updateMessages();
localStorage.setItem(l10n.localStorageKey, l10n.current);
};
/**
* Restores the last locale from local storage.
*/
l10n.restoreLocale = function() {
var val = localStorage.getItem(l10n.localStorageKey);
if (val) {
var index = parseInt(val);
if (!isNaN(index)) {
l10n.current = index % l10n.LOCALES.length;
l10n.updateMessages();
}
}
};
/**

View File

@ -553,6 +553,9 @@ panel.sim = null;
* Initializes thie UI.
*/
panel.init = function() {
// Restores the last locale if it exists.
l10n.restoreLocale();
// Initializes event listener for nav buttons.
var button = document.getElementById('locale');
button.addEventListener('click', l10n.nextLocale, false);