summaryrefslogtreecommitdiff
path: root/src/home/bot/lang.py
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2022-11-06 20:40:42 +0300
committerEvgeny Zinoviev <me@ch1p.io>2022-11-06 20:53:55 +0300
commit75ee161b6eb64cf19c8a9718d15047443f3e4ebe (patch)
treeccebc9cbd2709ad13a14ec00372fdcfe9226cd9f /src/home/bot/lang.py
parent28c67c4510a3bee574b4077be35147dba257c8f7 (diff)
inverter_bot: refactor and introduce new functions
Diffstat (limited to 'src/home/bot/lang.py')
-rw-r--r--src/home/bot/lang.py81
1 files changed, 0 insertions, 81 deletions
diff --git a/src/home/bot/lang.py b/src/home/bot/lang.py
deleted file mode 100644
index 624c748..0000000
--- a/src/home/bot/lang.py
+++ /dev/null
@@ -1,81 +0,0 @@
-from __future__ import annotations
-
-import logging
-
-from typing import Union, Optional, List, Dict
-
-logger = logging.getLogger(__name__)
-
-
-class LangStrings(dict):
- _lang: Optional[str]
-
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self._lang = None
-
- def setlang(self, lang: str):
- self._lang = lang
-
- def __missing__(self, key):
- logger.warning(f'key {key} is missing in language {self._lang}')
- return '{%s}' % key
-
- def __setitem__(self, key, value):
- raise NotImplementedError(f'setting translation strings this way is prohibited (was trying to set {key}={value})')
-
-
-class LangPack:
- strings: Dict[str, LangStrings[str, str]]
- default_lang: str
-
- def __init__(self):
- self.strings = {}
- self.default_lang = 'en'
-
- def ru(self, **kwargs) -> None:
- self.set(kwargs, 'ru')
-
- def en(self, **kwargs) -> None:
- self.set(kwargs, 'en')
-
- def set(self,
- strings: Union[LangStrings, dict],
- lang: str) -> None:
-
- if isinstance(strings, dict) and not isinstance(strings, LangStrings):
- strings = LangStrings(**strings)
- strings.setlang(lang)
-
- if lang not in self.strings:
- self.strings[lang] = strings
- else:
- self.strings[lang].update(strings)
-
- def all(self, key):
- result = []
- for strings in self.strings.values():
- result.append(strings[key])
- return result
-
- @property
- def languages(self) -> List[str]:
- return list(self.strings.keys())
-
- def get(self, key: str, lang: str, *args) -> str:
- if args:
- return self.strings[lang][key] % args
- else:
- return self.strings[lang][key]
-
- def __call__(self, *args, **kwargs):
- return self.strings[self.default_lang][args[0]]
-
- def __getitem__(self, key):
- return self.strings[self.default_lang][key]
-
- def __setitem__(self, key, value):
- raise NotImplementedError('setting translation strings this way is prohibited')
-
- def __contains__(self, key):
- return key in self.strings[self.default_lang]