Updated docs
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
cpl-translation sh-edraft Common Python library Translation
|
||||
cpl-translation CPL Translation
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
sh-edraft Common Python library Python Translation
|
||||
CPL translation extension
|
||||
|
||||
:copyright: (c) 2022 - 2023 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'cpl_translation'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2022 - 2023 sh-edraft.de'
|
||||
__version__ = '2022.12.1'
|
||||
__title__ = "cpl_translation"
|
||||
__author__ = "Sven Heidemann"
|
||||
__license__ = "MIT"
|
||||
__copyright__ = "Copyright (c) 2022 - 2023 sh-edraft.de"
|
||||
__version__ = "2023.2.0"
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
@@ -25,6 +25,7 @@ from .translate_pipe import TranslatePipe
|
||||
from .translation_service import TranslationService
|
||||
from .translation_service_abc import TranslationServiceABC
|
||||
from .translation_settings import TranslationSettings
|
||||
|
||||
# build-ignore
|
||||
|
||||
|
||||
@@ -39,16 +40,17 @@ def add_translation(self):
|
||||
self.add_singleton(TranslationServiceABC, TranslationService)
|
||||
self.add_transient(PipeABC, TranslatePipe)
|
||||
except ImportError as e:
|
||||
Console.error('cpl-translation is not installed', str(e))
|
||||
Console.error("cpl-translation is not installed", str(e))
|
||||
|
||||
|
||||
def init():
|
||||
from cpl_core.dependency_injection import ServiceCollection
|
||||
|
||||
ServiceCollection.add_translation = add_translation
|
||||
|
||||
|
||||
init()
|
||||
# build-ignore-end
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major='2022', minor='12', micro='1')
|
||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||
version_info = VersionInfo(major="2023", minor="2", micro="0")
|
||||
|
@@ -2,24 +2,24 @@
|
||||
"ProjectSettings": {
|
||||
"Name": "cpl-translation",
|
||||
"Version": {
|
||||
"Major": "2022",
|
||||
"Minor": "12",
|
||||
"Micro": "1"
|
||||
"Major": "2023",
|
||||
"Minor": "2",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "Sven Heidemann",
|
||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||
"Description": "sh-edraft Common Python library Translation",
|
||||
"LongDescription": "sh-edraft Common Python library Python Translation",
|
||||
"Description": "CPL Translation",
|
||||
"LongDescription": "CPL translation extension",
|
||||
"URL": "https://www.sh-edraft.de",
|
||||
"CopyrightDate": "2022 - 2023",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||
"Dependencies": [
|
||||
"cpl-core>=2022.12.1"
|
||||
"cpl-core>=2023.2.0"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli>=2022.12.1"
|
||||
"cpl-cli>=2023.2.0"
|
||||
],
|
||||
"PythonVersion": ">=3.10",
|
||||
"PythonPath": {},
|
||||
@@ -41,7 +41,8 @@
|
||||
"PackageData": {
|
||||
"cpl_translation": [
|
||||
".cpl/*.py"
|
||||
]},
|
||||
]
|
||||
},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
@@ -4,7 +4,6 @@ from cpl_translation.translation_service_abc import TranslationServiceABC
|
||||
|
||||
|
||||
class TranslatePipe(PipeABC):
|
||||
|
||||
def __init__(self, translation: TranslationServiceABC):
|
||||
self._translation = translation
|
||||
|
||||
@@ -12,5 +11,5 @@ class TranslatePipe(PipeABC):
|
||||
try:
|
||||
return self._translation.translate(value)
|
||||
except KeyError as e:
|
||||
Console.error(f'Translation {value} not found')
|
||||
return ''
|
||||
Console.error(f"Translation {value} not found")
|
||||
return ""
|
||||
|
@@ -7,12 +7,11 @@ from cpl_translation.translation_settings import TranslationSettings
|
||||
|
||||
|
||||
class TranslationService(TranslationServiceABC):
|
||||
|
||||
def __init__(self):
|
||||
self._translation = {}
|
||||
|
||||
self._language = ''
|
||||
self._default_language = ''
|
||||
self._language = ""
|
||||
self._default_language = ""
|
||||
|
||||
TranslationServiceABC.__init__(self)
|
||||
|
||||
@@ -30,11 +29,11 @@ class TranslationService(TranslationServiceABC):
|
||||
self._language = lang
|
||||
|
||||
def load(self, lang: str):
|
||||
if not os.path.exists(f'translation/{lang}.json'):
|
||||
if not os.path.exists(f"translation/{lang}.json"):
|
||||
raise FileNotFoundError()
|
||||
|
||||
file_dict = {}
|
||||
with open(f'translation/{lang}.json', 'r', encoding='utf-8') as file:
|
||||
with open(f"translation/{lang}.json", "r", encoding="utf-8") as file:
|
||||
file_dict = json.load(file)
|
||||
file.close()
|
||||
|
||||
@@ -42,7 +41,7 @@ class TranslationService(TranslationServiceABC):
|
||||
|
||||
def load_by_settings(self, settings: TranslationSettings):
|
||||
if settings is None:
|
||||
raise Exception(f'{TranslationSettings.__name__} not loaded')
|
||||
raise Exception(f"{TranslationSettings.__name__} not loaded")
|
||||
|
||||
self._language = settings.default_language
|
||||
self._default_language = settings.default_language
|
||||
@@ -51,9 +50,13 @@ class TranslationService(TranslationServiceABC):
|
||||
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])
|
||||
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')
|
||||
raise KeyError(f"Translation {key} not found")
|
||||
|
||||
return value
|
||||
|
@@ -4,21 +4,26 @@ from cpl_translation.translation_settings import TranslationSettings
|
||||
|
||||
|
||||
class TranslationServiceABC(ABC):
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
def set_default_lang(self, lang: str):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_default_lang(self, lang: str): pass
|
||||
def set_lang(self, lang: str):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_lang(self, lang: str): pass
|
||||
def load(self, lang: str):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def load(self, lang: str): pass
|
||||
def load_by_settings(self, settings: TranslationSettings):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def load_by_settings(self, settings: TranslationSettings): pass
|
||||
|
||||
@abstractmethod
|
||||
def translate(self, key: str) -> str: pass
|
||||
def translate(self, key: str) -> str:
|
||||
pass
|
||||
|
@@ -5,12 +5,11 @@ from cpl_core.console import Console
|
||||
|
||||
|
||||
class TranslationSettings(ConfigurationModelABC):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
|
||||
self._languages = []
|
||||
self._default_lang = ''
|
||||
self._default_lang = ""
|
||||
|
||||
@property
|
||||
def languages(self) -> list[str]:
|
||||
@@ -22,8 +21,8 @@ class TranslationSettings(ConfigurationModelABC):
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._languages = settings['Languages']
|
||||
self._default_lang = settings['DefaultLanguage']
|
||||
self._languages = settings["Languages"]
|
||||
self._default_lang = settings["DefaultLanguage"]
|
||||
except Exception as e:
|
||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings')
|
||||
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||
Console.error(f"[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings")
|
||||
Console.error(f"[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}")
|
||||
|
Reference in New Issue
Block a user