From 49cc2b803664fe6c3484c45977cb1568754eae30 Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 4 Feb 2020 15:13:14 +0200 Subject: [PATCH] Add language switcher --- config/template_config.ini | 2 ++ core.py | 4 +++- database.py | 5 ++++- strings.py => strings/it_IT.py | 0 utils.py | 8 +++++--- worker.py | 4 +++- 6 files changed, 17 insertions(+), 6 deletions(-) rename strings.py => strings/it_IT.py (100%) diff --git a/config/template_config.ini b/config/template_config.ini index 1a2b8b6..59bd650 100644 --- a/config/template_config.ini +++ b/config/template_config.ini @@ -8,6 +8,8 @@ version = 13 ; Set this to no when you are done editing the file is_template = yes +; Language code for string file +language = it_IT # Telegram bot parameters [Telegram] diff --git a/core.py b/core.py index b254e96..fee9281 100644 --- a/core.py +++ b/core.py @@ -1,11 +1,13 @@ import sys import telegram -import strings import worker import configloader import utils import threading +import importlib +language = configloader.config["Config"]["language"] +strings = importlib.import_module("strings." + language) def main(): """The core code of the program. Should be run only in the main process!""" diff --git a/database.py b/database.py index 7b09e4a..5b87b18 100644 --- a/database.py +++ b/database.py @@ -5,9 +5,12 @@ from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base import configloader import telegram -import strings import requests import utils +import importlib + +language = configloader.config["Config"]["language"] +strings = importlib.import_module("strings." + language) # Create a (lazy) database engine engine = create_engine(configloader.config["Database"]["engine"]) diff --git a/strings.py b/strings/it_IT.py similarity index 100% rename from strings.py rename to strings/it_IT.py diff --git a/utils.py b/utils.py index ecce767..6d9bfe1 100644 --- a/utils.py +++ b/utils.py @@ -2,11 +2,13 @@ import telegram import telegram.error import time from configloader import config -from strings import currency_format_string, currency_symbol import typing import os import sys -import strings +import importlib + +language = config["Config"]["language"] +strings = importlib.import_module("strings." + language) if config["Error Reporting"]["sentry_token"] != \ "https://00000000000000000000000000000000:00000000000000000000000000000000@sentry.io/0000000": @@ -40,7 +42,7 @@ class Price: return f"" def __str__(self): - return currency_format_string.format(symbol=currency_symbol, + return strings.currency_format_string.format(symbol=strings.currency_symbol, value="{0:.2f}".format( self.value / (10 ** int(config["Payments"]["currency_exp"])))) diff --git a/worker.py b/worker.py index c8c7744..1f0dd26 100644 --- a/worker.py +++ b/worker.py @@ -3,7 +3,6 @@ import typing import uuid import datetime import telegram -import strings import configloader import sys import queue as queuem @@ -13,7 +12,10 @@ import utils import os from html import escape import requests +import importlib +language = configloader.config["Config"]["language"] +strings = importlib.import_module("strings." + language) class StopSignal: """A data class that should be sent to the worker when the conversation has to be stopped abnormally."""