Added logic to load translations and to translate texts
This commit is contained in:
@@ -4,19 +4,19 @@
|
||||
"Version": {
|
||||
"Major": "2022",
|
||||
"Minor": "8",
|
||||
"Micro": "1"
|
||||
"Micro": "1.dev7"
|
||||
},
|
||||
"Author": "Sven Heidemann",
|
||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||
"Description": "sh-edraft Common Python library Translation",
|
||||
"LongDescription": "sh-edraft Common Python library Python i18n based Translation implementation",
|
||||
"LongDescription": "sh-edraft Common Python library Python Translation",
|
||||
"URL": "https://www.sh-edraft.de",
|
||||
"CopyrightDate": "2022",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||
"Dependencies": [
|
||||
"cpl-core>=2022.6.0"
|
||||
"cpl-core>=2022.8.1.dev7"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli>=2022.6.0"
|
||||
|
16
src/cpl_translation/translate_pipe.py
Normal file
16
src/cpl_translation/translate_pipe.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.pipes.pipe_abc import PipeABC
|
||||
from cpl_translation.translation_service_abc import TranslationServiceABC
|
||||
|
||||
|
||||
class TranslatePipe(PipeABC):
|
||||
|
||||
def __init__(self, translation: TranslationServiceABC):
|
||||
self._translation = translation
|
||||
|
||||
def transform(self, value: any, *args):
|
||||
try:
|
||||
return self._translation.translate(value)
|
||||
except KeyError as e:
|
||||
Console.error(f'Translation {value} not found')
|
||||
return ''
|
54
src/cpl_translation/translation_service.py
Normal file
54
src/cpl_translation/translation_service.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import json
|
||||
import os.path
|
||||
from functools import reduce
|
||||
|
||||
from cpl_core.console import Console
|
||||
from cpl_translation.translation_service_abc import TranslationServiceABC
|
||||
from cpl_translation.translation_settings import TranslationSettings
|
||||
|
||||
|
||||
class TranslationService(TranslationServiceABC):
|
||||
|
||||
def __init__(self):
|
||||
self._translation = {}
|
||||
|
||||
self._language = ''
|
||||
self._default_language = ''
|
||||
|
||||
TranslationServiceABC.__init__(self)
|
||||
|
||||
def set_default_lang(self, lang: str):
|
||||
self._default_language = lang
|
||||
self.set_lang(lang)
|
||||
|
||||
def set_lang(self, lang: str):
|
||||
self._language = lang
|
||||
|
||||
def load(self, lang: str):
|
||||
if not os.path.exists(f'translation/{lang}.json'):
|
||||
raise FileNotFoundError()
|
||||
|
||||
file_dict = {}
|
||||
with open(f'translation/{lang}.json', 'r') as file:
|
||||
file_dict = json.load(file)
|
||||
file.close()
|
||||
|
||||
self._translation[lang] = file_dict
|
||||
|
||||
def load_by_settings(self, settings: TranslationSettings):
|
||||
if settings is None:
|
||||
raise Exception(f'{TranslationSettings.__name__} not loaded')
|
||||
|
||||
self._language = settings.default_language
|
||||
self._default_language = settings.default_language
|
||||
|
||||
for lang in settings.languages:
|
||||
self.load(lang)
|
||||
|
||||
def translate(self, key: str) -> str:
|
||||
value = reduce(lambda d, key: d.get(key) if isinstance(d, dict) else None, key.split("."), self._translation[self._language])
|
||||
|
||||
if value is None:
|
||||
raise KeyError(f'Translation {key} not found')
|
||||
|
||||
return value
|
22
src/cpl_translation/translation_service_abc.py
Normal file
22
src/cpl_translation/translation_service_abc.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class TranslationServiceABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def set_default_lang(self, lang: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def set_lang(self, lang: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def load(self, lang: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def load_by_settings(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def translate(self, key: str) -> str: pass
|
29
src/cpl_translation/translation_settings.py
Normal file
29
src/cpl_translation/translation_settings.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import traceback
|
||||
|
||||
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl_core.console import Console
|
||||
|
||||
|
||||
class TranslationSettings(ConfigurationModelABC):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
|
||||
self._languages = []
|
||||
self._default_lang = ''
|
||||
|
||||
@property
|
||||
def languages(self) -> list[str]:
|
||||
return self._languages
|
||||
|
||||
@property
|
||||
def default_language(self) -> str:
|
||||
return self._default_lang
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._languages = settings['Languages']
|
||||
self._default_lang = settings['DefaultLanguage']
|
||||
except Exception as e:
|
||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
|
||||
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
Reference in New Issue
Block a user