Renamed project dirs
All checks were successful
Test before pr merge / test-lint (pull_request) Successful in 6s

This commit is contained in:
2025-10-11 09:32:13 +02:00
parent f1aaaf2a5b
commit 90ff8d466d
319 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
from .translate_pipe import TranslatePipe
from .translation_module import TranslationModule
from .translation_service import TranslationService
from .translation_service_abc import TranslationServiceABC
from .translation_settings import TranslationSettings
__version__ = "1.0.0"

View File

@@ -0,0 +1,20 @@
from cpl.core.console import Console
from cpl.core.pipes.pipe_abc import PipeABC
from cpl.core.typing import T
from cpl.dependency import get_provider
from cpl.translation.translation_service_abc import TranslationServiceABC
class TranslatePipe(PipeABC):
@staticmethod
def to_str(value: T, *args) -> str:
try:
translations = get_provider().get_service(TranslationServiceABC)
return translations.translate(value)
except KeyError:
Console.error(f"Translation {value} not found")
return ""
@staticmethod
def from_str(value: str, *args) -> T:
pass

View File

@@ -0,0 +1,7 @@
from cpl.dependency.module.module import Module
from cpl.translation.translation_service import TranslationService
from cpl.translation.translation_service_abc import TranslationServiceABC
class TranslationModule(Module):
singleton = [(TranslationServiceABC, TranslationService)]

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,23 @@
from abc import ABC, abstractmethod
from cpl.translation.translation_settings import TranslationSettings
class TranslationServiceABC(ABC):
@abstractmethod
def __init__(self): ...
@abstractmethod
def set_default_lang(self, lang: str): ...
@abstractmethod
def set_lang(self, lang: str): ...
@abstractmethod
def load(self, lang: str): ...
@abstractmethod
def load_by_settings(self, settings: TranslationSettings): ...
@abstractmethod
def translate(self, key: str) -> str: ...

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,2 @@
cpl-core
cpl-dependency