From af9abdaf4f4ff1ad03cf6a015eeefb876bd300cf Mon Sep 17 00:00:00 2001 From: wixette Date: Sun, 8 Mar 2020 16:55:36 +0800 Subject: [PATCH] use local storage to cache locale choice --- js/l10n.js | 20 ++++++++++++++++++++ js/panel.js | 3 +++ 2 files changed, 23 insertions(+) diff --git a/js/l10n.js b/js/l10n.js index a45c8e1..ed7e3da 100644 --- a/js/l10n.js +++ b/js/l10n.js @@ -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(); + } + } }; /** diff --git a/js/panel.js b/js/panel.js index 8161ba5..3b7e129 100644 --- a/js/panel.js +++ b/js/panel.js @@ -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);