Added cpl-mail
This commit is contained in:
27
src/cpl-translation/cpl/translation/__init__.py
Normal file
27
src/cpl-translation/cpl/translation/__init__.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from .translate_pipe import TranslatePipe
|
||||
from .translation_service import TranslationService
|
||||
from .translation_service_abc import TranslationServiceABC
|
||||
from .translation_settings import TranslationSettings
|
||||
|
||||
|
||||
def add_translation(self):
|
||||
from cpl.core.console import Console
|
||||
from cpl.core.pipes import PipeABC
|
||||
from cpl_translation.translate_pipe import TranslatePipe
|
||||
from cpl_translation.translation_service import TranslationService
|
||||
from cpl_translation.translation_service_abc import TranslationServiceABC
|
||||
|
||||
try:
|
||||
self.add_singleton(TranslationServiceABC, TranslationService)
|
||||
self.add_transient(PipeABC, TranslatePipe)
|
||||
except ImportError as 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()
|
||||
15
src/cpl-translation/cpl/translation/translate_pipe.py
Normal file
15
src/cpl-translation/cpl/translation/translate_pipe.py
Normal file
@@ -0,0 +1,15 @@
|
||||
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:
|
||||
Console.error(f"Translation {value} not found")
|
||||
return ""
|
||||
62
src/cpl-translation/cpl/translation/translation_service.py
Normal file
62
src/cpl-translation/cpl/translation/translation_service.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import json
|
||||
import os.path
|
||||
from functools import reduce
|
||||
|
||||
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):
|
||||
if lang not in self._translation:
|
||||
raise KeyError()
|
||||
|
||||
self._default_language = lang
|
||||
self.set_lang(lang)
|
||||
|
||||
def set_lang(self, lang: str):
|
||||
if lang not in self._translation:
|
||||
raise KeyError()
|
||||
|
||||
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", encoding="utf-8") 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
|
||||
@@ -0,0 +1,29 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from cpl_translation.translation_settings import TranslationSettings
|
||||
|
||||
|
||||
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, settings: TranslationSettings):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def translate(self, key: str) -> str:
|
||||
pass
|
||||
21
src/cpl-translation/cpl/translation/translation_settings.py
Normal file
21
src/cpl-translation/cpl/translation/translation_settings.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from cpl.core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
|
||||
|
||||
class TranslationSettings(ConfigurationModelABC):
|
||||
def __init__(
|
||||
self,
|
||||
languages: list = None,
|
||||
default_language: str = None,
|
||||
):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
|
||||
self._languages = [] if languages is None else languages
|
||||
self._default_lang = default_language
|
||||
|
||||
@property
|
||||
def languages(self) -> list[str]:
|
||||
return self._languages
|
||||
|
||||
@property
|
||||
def default_language(self) -> str:
|
||||
return self._default_lang
|
||||
30
src/cpl-translation/pyproject.toml
Normal file
30
src/cpl-translation/pyproject.toml
Normal file
@@ -0,0 +1,30 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=70.1.0", "wheel>=0.43.0"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "cpl-translation"
|
||||
version = "2024.7.0"
|
||||
description = "CPL translation"
|
||||
readme = "CPL translation package"
|
||||
requires-python = ">=3.12"
|
||||
license = { text = "MIT" }
|
||||
authors = [
|
||||
{ name = "Sven Heidemann", email = "sven.heidemann@sh-edraft.de" }
|
||||
]
|
||||
keywords = ["cpl", "translation", "backend", "shared", "library"]
|
||||
|
||||
dynamic = ["dependencies", "optional-dependencies"]
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://www.sh-edraft.de"
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
where = ["."]
|
||||
include = ["cpl*"]
|
||||
|
||||
[tool.setuptools.dynamic]
|
||||
dependencies = { file = ["requirements.txt"] }
|
||||
optional-dependencies.dev = { file = ["requirements.dev.txt"] }
|
||||
|
||||
|
||||
1
src/cpl-translation/requirements.dev.txt
Normal file
1
src/cpl-translation/requirements.dev.txt
Normal file
@@ -0,0 +1 @@
|
||||
black==25.1.0
|
||||
1
src/cpl-translation/requirements.txt
Normal file
1
src/cpl-translation/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
cpl-core
|
||||
Reference in New Issue
Block a user