From d3337e75a92d6ff052c9b5be0dc548bb4eebf827 Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Mon, 9 Sep 2024 07:30:33 +1000 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=20Settings/Lang=20cache=20improvement?= =?UTF-8?q?s=20#687=20+=20#766?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/js/common.js | 47 ++++++++++++++++--------- front/plugins/mikrotik_scan/config.json | 4 +-- server/initialise.py | 2 +- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/front/js/common.js b/front/js/common.js index 48415082..371bc72b 100755 --- a/front/js/common.js +++ b/front/js/common.js @@ -212,7 +212,8 @@ function cacheStrings() { return new Promise((resolve, reject) => { // Create a promise for each language - const languagePromises = allLanguages.map((language_code) => { + languagesToLoad = ['en_us', getLangCode()] + const languagePromises = languagesToLoad.map((language_code) => { return new Promise((resolveLang, rejectLang) => { // Fetch core strings and translations @@ -267,6 +268,29 @@ function cacheStrings() { function getString(key) { function fetchString(key) { + + lang_code = getLangCode(); + + let result = getCache(`pia_lang_${key}_${lang_code}`, true); + + if (isEmpty(result)) { + result = getCache(`pia_lang_${key}_en_us`, true); + } + + return result; + } + + if (isAppInitialized()) { + return fetchString(key); + } else { + callAfterAppInitialized(() => fetchString(key)); + } +} + +// ----------------------------------------------------------------------------- +// Get current language ISO code +function getLangCode() { + UI_LANG = getSetting("UI_LANG"); let lang_code = 'en_us'; @@ -310,20 +334,7 @@ function getString(key) { break; } - let result = getCache(`pia_lang_${key}_${lang_code}`, true); - - if (isEmpty(result)) { - result = getCache(`pia_lang_${key}_en_us`, true); - } - - return result; - } - - if (isAppInitialized()) { - return fetchString(key); - } else { - callAfterAppInitialized(() => fetchString(key)); - } + return lang_code; } @@ -1193,7 +1204,7 @@ const sessionStorageKey = "myScriptExecuted_common_js"; var completedCalls = [] var completedCalls_final = ['cacheSettings', 'cacheStrings', 'cacheDevices']; var completedCallsCount = 0; -var completedCallsCount_final = allLanguages.length + 2; // number of language files + cacheDevices + cacheSettings +var completedCallsCount_final; // ----------------------------------------------------------------------------- // Clearing all the caches @@ -1249,6 +1260,10 @@ function callAfterAppInitialized(callback) { // Check if the code has been executed before by checking sessionStorage function isAppInitialized() { // return arraysContainSameValues(getCache("completedCalls").split(',').filter(Boolean), completedCalls_final); + + // loading settings + 1 (or 2 language files if not english) + device cache. + completedCallsCount_final = getLangCode() == 'en_us' ? 3 : 4 ; + return (parseInt(getCache("completedCallsCount")) >= completedCallsCount_final); } diff --git a/front/plugins/mikrotik_scan/config.json b/front/plugins/mikrotik_scan/config.json index 9805c26c..3c2f0f5a 100755 --- a/front/plugins/mikrotik_scan/config.json +++ b/front/plugins/mikrotik_scan/config.json @@ -11,7 +11,7 @@ "display_name": [ { "language_code": "en_us", - "string": "Mikrotik (Name discovery)" + "string": "Mikrotik (Device discovery)" } ], "icon": [ @@ -23,7 +23,7 @@ "description": [ { "language_code": "en_us", - "string": "A plugin to discover device names." + "string": "A plugin to discover devices via Mikrotik." } ], "params": [ diff --git a/server/initialise.py b/server/initialise.py index cb02366b..26a25caf 100755 --- a/server/initialise.py +++ b/server/initialise.py @@ -74,7 +74,7 @@ def update_or_append(settings_list, item_tuple, key): for index, item in enumerate(settings_list): if item[0] == key: mylog('trace', ['[Import Config] OLD TUPLE : ', item]) - # Replace only non-empty values in the tuple + # Keep values marked as "_KEEP_" updated_tuple = tuple( new_val if new_val != "_KEEP_" else old_val for old_val, new_val in zip(item, item_tuple)