diff --git a/src/modules/__init__.py b/src/modules/__init__.py index 9673d66..003fa13 100644 --- a/src/modules/__init__.py +++ b/src/modules/__init__.py @@ -3,8 +3,9 @@ # Developed by KuiToi Dev # File modules.__init__.py # Written by: SantaSpeen -# Version 1.0 +# Version 1.1 # Licence: FPA # (c) kuitoi.su 2023 from .console import Console from .config_provider import ConfigProvider, Config +from .i18n import MultiLanguage diff --git a/src/modules/i18n/__init__.py b/src/modules/i18n/__init__.py new file mode 100644 index 0000000..a54bcc5 --- /dev/null +++ b/src/modules/i18n/__init__.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + +# Developed by KuiToi Dev +# File modules.i18n.__init__.py +# Written by: SantaSpeen +# Version 1.0 +# Licence: FPA +# (c) kuitoi.su 2023 +from .i18n import MultiLanguage diff --git a/src/modules/i18n/files/en.json b/src/modules/i18n/files/en.json new file mode 100644 index 0000000..1189e06 --- /dev/null +++ b/src/modules/i18n/files/en.json @@ -0,0 +1,8 @@ +{ + "hello": "Hello from KuiToi-Server!", + "config_file": "Use kuitoi.yml for config.", + "debug": "Getting new logging with DEBUG level!", + "config_info": "Server config: %s", + "init": "Initializing ready.", + "ready": "Server started!" +} \ No newline at end of file diff --git a/src/modules/i18n/files/ru.json b/src/modules/i18n/files/ru.json new file mode 100644 index 0000000..a2fd7fc --- /dev/null +++ b/src/modules/i18n/files/ru.json @@ -0,0 +1,8 @@ +{ + "hello": "Привет из KuiToi-Server!", + "config_file": "Используй kuitoi.yml для настройки.", + "debug": "Начата новая сессия логирования с уровнем DEBUG!", + "config_info": "Конфиг сервера: %s", + "init": "Инициализация окончена.", + "ready": "Сервер запущен!" +} \ No newline at end of file diff --git a/src/modules/i18n/i18n-builtins.pyi b/src/modules/i18n/i18n-builtins.pyi new file mode 100644 index 0000000..da8744e --- /dev/null +++ b/src/modules/i18n/i18n-builtins.pyi @@ -0,0 +1,10 @@ +class i18n: + hello: str = "Hello from KuiToi-Server!" + config_file: str = "Use kuitoi.yml for config." + debug: str = "Getting new logging with DEBUG level!" + config_info: str = "Server config: %s" + init: str = "Initializing ready." + ready: str = "Server started!" +i18n_data = {"hello":"Hello from KuiToi-Server!","config_file":"Use kuitoi.yml for config.", + "debug":"Getting new logging with DEBUG level!","config_info":"Server config: %s", + "init":"Initializing ready.","ready":"Server started!"} diff --git a/src/modules/i18n/i18n.py b/src/modules/i18n/i18n.py new file mode 100644 index 0000000..6f8585d --- /dev/null +++ b/src/modules/i18n/i18n.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- + +# Developed by KuiToi Dev +# File modules.i18n.i18n.py +# Written by: SantaSpeen +# Version 1.0 +# Licence: FPA +# (c) kuitoi.su 2023 +import builtins +import json + +from core import get_logger + + +class i18n: + + def __init__(self, data): + self.hello: str = data["hello"] + self.config_file: str = data["config_file"] + self.debug: str = data["debug"] + self.config_info: str = data["config_info"] + self.init: str = data["init"] + self.ready: str = data["ready"] + + +class MultiLanguage: + + def __init__(self, language: str = None, files_dir="modules/i18n/files/"): + if language is None: + language = "en" + self.__data = {} + self.__i18n = None + self.language = language + self.files_dir = files_dir + self.log = get_logger("i18n") + self.set_language(language) + + def set_language(self, language): + self.log.debug(f"set_language({language})") + self.language = language + if language != "en": + self.open_file() + else: + self.__data = { + "hello": "Hello from KuiToi-Server!", + "config_file": "Use kuitoi.yml for config.", + "debug": "Getting new logging with DEBUG level!", + "config_info": "Server config: %s", + "init": "Initializing ready.", + "ready": "Server started!" + } + self.__i18n = i18n(self.__data) + + def open_file(self): + self.log.debug("open_file") + file = self.files_dir + self.language + ".json" + try: + with open(file) as f: + self.__data.update(json.load(f)) + except FileNotFoundError: + self.log.warning(f"Localisation {self.language} not found; Setting language to: en.") + self.set_language("en") + + def builtins_hook(self) -> None: + self.log.debug("used builtins_hook") + builtins.i18n = self.__i18n + builtins.i18n_data = self.__data