Updated docs
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user