Compare commits
9 Commits
cf50296264
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 8792aaeeb9 | |||
| 774a1b0ce3 | |||
| 633c92608f | |||
| 3f0fe22317 | |||
| 2a3eca2c31 | |||
| f26cc65c7f | |||
| 0b9f8b6feb | |||
| 3581e6ae44 | |||
| c261f7e140 |
@@ -21,5 +21,5 @@ fmt = "<green>{elapsed} -- {time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{leve
|
||||
|
||||
Добавление в проект:
|
||||
```bash
|
||||
git submodule add ssh://git@git.anidev.ru:222/andiev-soft/I18N-Module src/modules/i18n
|
||||
git submodule add ssh://git@git.anidev.ru:222/AnidevSoft/I18N-Module.git src/modules/i18n
|
||||
```
|
||||
|
||||
+32
-1
@@ -1,3 +1,23 @@
|
||||
class _i18_proxy:
|
||||
@staticmethod
|
||||
def __getattr__(item):
|
||||
"""
|
||||
Get the phrase from the locale file. If the phrase is not found, return the key in uppercase.
|
||||
|
||||
:param key: Key to search in the locale file.
|
||||
:param kwargs: Arguments to format the phrase.
|
||||
:return: The phrase from the locale file.
|
||||
"""
|
||||
@staticmethod
|
||||
def __init__(**kwargs):
|
||||
"""
|
||||
Get the phrase from the locale file. If the phrase is not found, return the key in uppercase.
|
||||
|
||||
:param key: Key to search in the locale file.
|
||||
:param kwargs: Arguments to format the phrase.
|
||||
:return: The phrase from the locale file.
|
||||
"""
|
||||
|
||||
class i18n:
|
||||
@staticmethod
|
||||
def set_lang(lang, encoding=None) -> None:
|
||||
@@ -17,7 +37,18 @@ class i18n:
|
||||
:return: The phrase from the locale file.
|
||||
"""
|
||||
@staticmethod
|
||||
def __call__(key: str, **kwargs) -> str:
|
||||
def __getattr__(item):
|
||||
"""
|
||||
Get the phrase from the locale file. If the phrase is not found, return the key in uppercase.
|
||||
|
||||
:param key: Key to search in the locale file.
|
||||
:param kwargs: Arguments to format the phrase.
|
||||
:return: The phrase from the locale file.
|
||||
"""
|
||||
key = item.replace("_", ".") # заменяем _ на . для удобства
|
||||
return _i18_proxy([key]) # Начинаем цепочку
|
||||
@staticmethod
|
||||
def __init__(key: str, **kwargs) -> str:
|
||||
"""
|
||||
Get the phrase from the locale file. If the phrase is not found, return the key in uppercase.
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import builtins
|
||||
import textwrap
|
||||
from collections import defaultdict
|
||||
|
||||
import json5
|
||||
@@ -12,19 +13,46 @@ except ImportError:
|
||||
log = logger.bind(module="i18n", prefix="init")
|
||||
log_load = logger.bind(module="i18n", prefix="load")
|
||||
|
||||
str_funcs = {attr for attr in dir(str)}
|
||||
str_funcs.add('__func__')
|
||||
str_funcs.remove('__init__')
|
||||
str_funcs.remove('__len__')
|
||||
str_funcs.remove('__hash__')
|
||||
str_funcs.remove('__add__')
|
||||
str_funcs.remove('__str__')
|
||||
str_funcs.remove('__repr__')
|
||||
|
||||
|
||||
class NestedAccessor:
|
||||
def __init__(self, parent, key_chain):
|
||||
self.parent: "I18N" = parent
|
||||
self.key_chain = key_chain
|
||||
self.__name__ = f'\r<{".".join(key_chain)}>'.upper()
|
||||
# self.__func__ = self.key_chain
|
||||
|
||||
def __getattr__(self, item):
|
||||
return NestedAccessor(self.parent, self.key_chain + [item]) # Продолжаем цепочку
|
||||
# Если нет метода, то возвращаем str
|
||||
key = ".".join((*self.key_chain, item))
|
||||
if key in self.parent.locale:
|
||||
if "{" not in self.parent.locale[key]:
|
||||
return self.parent.get_phrase(key)
|
||||
if item in str_funcs:
|
||||
return getattr(self(), item)
|
||||
return NestedAccessor(self.parent, self.key_chain + [item])
|
||||
|
||||
def __len__(self):
|
||||
return len(self())
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self())
|
||||
|
||||
def __call__(self, **kwargs):
|
||||
key = ".".join(self.key_chain)
|
||||
return self.parent.get_phrase(key, **kwargs)
|
||||
|
||||
def __add__(self, other):
|
||||
return self() + other
|
||||
|
||||
def __str__(self):
|
||||
return self()
|
||||
|
||||
@@ -91,15 +119,12 @@ class I18N:
|
||||
phrase = self._locale.get(key)
|
||||
if not phrase:
|
||||
return f"<{key.upper()}>"
|
||||
return phrase.format_map(defaultdict(lambda: '<unknown>', kwargs))
|
||||
return textwrap.dedent(phrase.format_map(defaultdict(lambda: '<unknown>', kwargs)))
|
||||
|
||||
def __getattr__(self, item):
|
||||
key = item.replace("_", ".") # заменяем _ на . для удобства
|
||||
return NestedAccessor(self, [key]) # Начинаем цепочку
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
raise AttributeError("Can't set attribute")
|
||||
|
||||
def __call__(self, key: str, **kwargs):
|
||||
return self.get_phrase(key, **kwargs)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user