From 55d63c1f7a7344e9d9d7b71b20d09310ed57b3fb Mon Sep 17 00:00:00 2001 From: luukas Date: Thu, 27 May 2021 21:39:46 +0300 Subject: [PATCH] Add translation file caching, show file read error (if any) and fix a typo in the Finnish translation. --- app/classes/shared/translation.py | 26 +++++++++++++++---- .../templates/panel/server_files.html | 11 +++++--- app/translations/en_EN.json | 3 ++- app/translations/fi_FI.json | 5 ++-- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/app/classes/shared/translation.py b/app/classes/shared/translation.py index 76d44aaa..5809e592 100644 --- a/app/classes/shared/translation.py +++ b/app/classes/shared/translation.py @@ -11,14 +11,21 @@ logger = logging.getLogger(__name__) class Translation(): def __init__(self): self.translations_path = os.path.join(helper.root_dir, 'app', 'translations') + self.cached_translation = None + self.cached_translation_lang = None def translate(self, page, word): translated_word = None lang = helper.get_setting('language') fallback_lang = 'en_EN' - translated_word = \ - self.translate_inner(page, word, lang) or \ - self.translate_inner(page, word, fallback_lang) + lang_file_exists = helper.check_file_exists( + os.path.join( + self.translations_path, lang + '.json' + ) + ) + + translated_word = self.translate_inner(page, word, lang) \ + if lang_file_exists else self.translate_inner(page, word, fallback_lang) if translated_word: if isinstance(translated_word, dict): return json.dumps(translated_word) @@ -31,8 +38,17 @@ class Translation(): lang + '.json' ) try: - with open(lang_file, 'r') as f: - data = json.load(f) + if not self.cached_translation: + with open(lang_file, 'r') as f: + data = json.load(f) + self.cached_translation = data + elif self.cached_translation_lang != lang: + with open(lang_file, 'r') as f: + data = json.load(f) + self.cached_translation = data + self.cached_translation_lang = lang + else: + data = self.cached_translation try: translated_page = data[page] diff --git a/app/frontend/templates/panel/server_files.html b/app/frontend/templates/panel/server_files.html index 60037056..de1cc745 100644 --- a/app/frontend/templates/panel/server_files.html +++ b/app/frontend/templates/panel/server_files.html @@ -194,6 +194,7 @@ }
+

{{ translate('serverFiles', 'editingFile') }}
file_contents
@@ -344,10 +345,13 @@ console.log('Got File Contents From Server'); json = JSON.parse(data) if (json.error) { - $('#editorParent').toggle(false) + $('#editorParent').toggle(false) // hide + $('#fileError').toggle(true) // show + $('#fileError').text("{{ translate('serverFiles', 'fileReadError') }}: " + json.error) // show error editor.blur() } else { - $('#editorParent').toggle(true) + $('#editorParent').toggle(true) // show + $('#fileError').toggle(false) // hide setFileName(event.target.innerText); editor.session.setValue(json.content); } @@ -376,7 +380,8 @@ } setFileName(); - $('#editorParent').toggle(false) + $('#editorParent').toggle(false) // show + $('#fileError').toggle(false) // hide editor.blur() function setMode (extension) { diff --git a/app/translations/en_EN.json b/app/translations/en_EN.json index a5549fa8..d22eff97 100644 --- a/app/translations/en_EN.json +++ b/app/translations/en_EN.json @@ -170,7 +170,8 @@ "yesDelete": "Yes, I understand the consequences", "noDelete": "No", "unsupportedLanguage": "Warning: This is not a supported file type", - "keybindings": "Keybindings" + "keybindings": "Keybindings", + "fileReadError": "File read error" }, "serverConfig": { "serverName": "Server Name", diff --git a/app/translations/fi_FI.json b/app/translations/fi_FI.json index c0d89946..c13182fa 100644 --- a/app/translations/fi_FI.json +++ b/app/translations/fi_FI.json @@ -166,11 +166,12 @@ "createDirQuestion": "Minkä nimen haluat uudelle hakemistolle?", "renameItemQuestion": "Mikä uuden nimen pitäisi olla?", "deleteItemQuestion": "Haluatko varmasti poistaa \" + name + \"?", - "deleteItemQuestionMessage": "Olet poistamassa \\\"\" + path + \"\\\"!

Tämä toiminta on peruuttamaton ja se menetetään ikuisesti!", + "deleteItemQuestionMessage": "Olet poistamassa \\\"\" + path + \"\\\"!

Tämä toiminto on peruuttamaton ja se menetetään ikuisesti!", "yesDelete": "Kyllä, ymmärrän seuraukset", "noDelete": "En", "unsupportedLanguage": "Varoitus: Tätä tiedostotyyppiä ei tueta", - "keybindings": "Pikanäppäimet" + "keybindings": "Pikanäppäimet", + "fileReadError": "Tiedoston lukuvirhe" }, "serverConfig": { "serverName": "Palvelimen nimi",