Added cpl-mail
Some checks failed
Build on push / prepare (push) Successful in 9s
Build on push / core (push) Successful in 18s
Build on push / query (push) Successful in 25s
Build on push / translation (push) Failing after 8s
Build on push / mail (push) Successful in 14s

This commit is contained in:
2025-09-15 20:56:07 +02:00
parent 3b120370b8
commit 25b4ca0696
344 changed files with 4567 additions and 4946 deletions

View 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()

View 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 ""

View 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

View File

@@ -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

View 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

View 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"] }

View File

@@ -0,0 +1 @@
black==25.1.0

View File

@@ -0,0 +1 @@
cpl-core