2021-10-09 10:51:41 +00:00
|
|
|
import json
|
|
|
|
import logging
|
2022-01-15 15:38:08 +00:00
|
|
|
import os
|
|
|
|
import typing as t
|
|
|
|
|
|
|
|
from app.classes.shared.console import console
|
|
|
|
from app.classes.shared.helpers import helper
|
2021-10-09 10:51:41 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-01-15 15:38:08 +00:00
|
|
|
|
|
|
|
class Translation:
|
2021-10-09 10:51:41 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.translations_path = os.path.join(helper.root_dir, 'app', 'translations')
|
|
|
|
self.cached_translation = None
|
|
|
|
self.cached_translation_lang = None
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2022-01-15 15:38:08 +00:00
|
|
|
def get_language_file(self, language: str):
|
|
|
|
return os.path.join(self.translations_path, str(language) + '.json')
|
2021-10-09 10:51:41 +00:00
|
|
|
|
2022-01-15 15:38:08 +00:00
|
|
|
def translate(self, page, word, language):
|
|
|
|
fallback_language = 'en_EN'
|
2021-10-09 10:51:41 +00:00
|
|
|
|
2022-01-15 15:38:08 +00:00
|
|
|
translated_word = self.translate_inner(page, word, language)
|
|
|
|
if translated_word is None:
|
|
|
|
translated_word = self.translate_inner(page, word, fallback_language)
|
2021-10-09 10:51:41 +00:00
|
|
|
|
|
|
|
if translated_word:
|
2022-01-15 15:38:08 +00:00
|
|
|
if isinstance(translated_word, dict):
|
|
|
|
# JSON objects
|
|
|
|
return json.dumps(translated_word)
|
|
|
|
elif isinstance(translated_word, str):
|
|
|
|
# Basic strings
|
|
|
|
return translated_word
|
|
|
|
elif hasattr(translated_word, '__iter__'):
|
|
|
|
# Multiline strings
|
|
|
|
return '\n'.join(translated_word)
|
2021-10-09 10:51:41 +00:00
|
|
|
return 'Error while getting translation'
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2022-01-15 15:38:08 +00:00
|
|
|
def translate_inner(self, page, word, language) -> t.Union[t.Any, None]:
|
|
|
|
language_file = self.get_language_file(language)
|
2021-10-09 10:51:41 +00:00
|
|
|
try:
|
|
|
|
if not self.cached_translation:
|
2022-01-15 15:38:08 +00:00
|
|
|
with open(language_file, 'r', encoding='utf-8') as f:
|
2021-10-09 10:51:41 +00:00
|
|
|
data = json.load(f)
|
|
|
|
self.cached_translation = data
|
2022-01-15 15:38:08 +00:00
|
|
|
elif self.cached_translation_lang != language:
|
|
|
|
with open(language_file, 'r', encoding='utf-8') as f:
|
2021-10-09 10:51:41 +00:00
|
|
|
data = json.load(f)
|
|
|
|
self.cached_translation = data
|
2022-01-15 15:38:08 +00:00
|
|
|
self.cached_translation_lang = language
|
2021-10-09 10:51:41 +00:00
|
|
|
else:
|
|
|
|
data = self.cached_translation
|
|
|
|
|
|
|
|
try:
|
|
|
|
translated_page = data[page]
|
|
|
|
except KeyError:
|
2022-01-15 15:38:08 +00:00
|
|
|
logger.error('Translation File Error: page {} does not exist for lang {}'.format(page, language))
|
|
|
|
console.error('Translation File Error: page {} does not exist for lang {}'.format(page, language))
|
2021-10-09 10:51:41 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
translated_word = translated_page[word]
|
|
|
|
return translated_word
|
|
|
|
except KeyError:
|
2022-01-15 15:38:08 +00:00
|
|
|
logger.error(f'Translation File Error: word {word} does not exist on page {page} for lang {language}')
|
|
|
|
console.error(f'Translation File Error: word {word} does not exist on page {page} for lang {language}')
|
2021-10-09 10:51:41 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
except Exception as e:
|
2022-01-15 15:38:08 +00:00
|
|
|
logger.critical(f'Translation File Error: Unable to read {language_file} due to {e}')
|
|
|
|
console.critical(f'Translation File Error: Unable to read {language_file} due to {e}')
|
2021-10-09 10:51:41 +00:00
|
|
|
return None
|
|
|
|
|
2022-01-15 15:38:08 +00:00
|
|
|
|
|
|
|
translation = Translation()
|