From 2443134bfc0cc65a78ac85106ff6483430046208 Mon Sep 17 00:00:00 2001 From: Ulrich Carmesin Date: Thu, 18 Aug 2022 13:48:53 +0200 Subject: [PATCH] new i18n_tool --- test/test_08i18n.py | 96 ++++++++++++++++++++++++++++++++++++++++++++ test/test_31db.py | 4 +- utils/config_tool.py | 6 +++ utils/i18n_tool.py | 95 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 test/test_08i18n.py create mode 100644 utils/i18n_tool.py diff --git a/test/test_08i18n.py b/test/test_08i18n.py new file mode 100644 index 0000000..3ddc966 --- /dev/null +++ b/test/test_08i18n.py @@ -0,0 +1,96 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# --------------------------------------------------------------------------------------------------------- +# Author : Ulrich Carmesin +# Source : gitea.ucarmesin.de +# --------------------------------------------------------------------------------------------------------- +import unittest +import os +import inspect +import utils.path_tool +import basic.program +import basic.constants as B +import test.constants +import test.testtools +import utils.path_const as P +import utils.i18n_tool +import basic.constants as B + +HOME_PATH = test.constants.HOME_PATH +OS_SYSTEM = test.constants.OS_SYSTEM + +# here you can select single testfunction for developping the tests +TEST_FUNCTIONS = ["test_01class", "test_02text", "test_03aliasList"] +#TEST_FUNCTIONS = [ "test_03aliasList"] +verbose = False + +EXP_KEY_MISSING = "key is missing {}" +EXP_KEY_DOESNT_EXIST = "key doesnt exist in domain {}" + +class MyTestCase(unittest.TestCase): + mymsg = "--------------------------------------------------------------" + + def test_01class(self): + global mymsg + actfunction = str(inspect.currentframe().f_code.co_name) + cnttest = 0 + if actfunction not in TEST_FUNCTIONS: + return + job = test.testtools.getJob() + i18n = utils.i18n_tool.I18n.getInstance() + self.assertIsNotNone(i18n) + cnttest += 1 + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) + + + def test_02text(self): + global mymsg + actfunction = str(inspect.currentframe().f_code.co_name) + cnttest = 0 + if actfunction not in TEST_FUNCTIONS: + return + job = test.testtools.getJob() + job.conf.confs["language"] = "de" + i18n = utils.i18n_tool.I18n.getInstance() + res = i18n.getText(f"{EXP_KEY_MISSING=}", job) + if verbose: print("RESULT "+res) + self.assertEqual(res, "Schluesselwort fehlt {}") + cnttest += 1 + res = i18n.getText(f"{EXP_KEY_DOESNT_EXIST=}", job) + if verbose: print("RESULT "+res) + self.assertEqual(res, "key doesnt exist in domain {}") + cnttest += 1 + res = i18n.getText(f"{B.ATTR_APPS_PROJECT=}", job) + if verbose: print("RESULT "+res) + self.assertEqual(res, "project") + cnttest += 1 + job.conf.confs["language"] = "fr" + i18n = utils.i18n_tool.I18n.getInstance() + self.assertRaises(Exception, i18n.getText, (f"{EXP_KEY_MISSING=}", job)) + cnttest += 1 + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) + + + def test_03aliasList(self): + global mymsg + actfunction = str(inspect.currentframe().f_code.co_name) + cnttest = 0 + if actfunction not in TEST_FUNCTIONS: + return + job = test.testtools.getJob() + job.conf.confs["language"] = "de" + i18n = utils.i18n_tool.I18n.getInstance() + # i18n.getText("EXP_KEY_MISSING", EXP_KEY_MISSING, job) + res = i18n.getAliasList(f"{EXP_KEY_MISSING=}", job) + if verbose: print("RESULT "+str(res)) + self.assertEqual(res, ['key is missing {}', 'Schluesselwort fehlt {}']) + cnttest += 1 + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) + + def test_zzz(self): + if verbose: print(MyTestCase.mymsg) + + +if __name__ == '__main__': + verbose = True + unittest.main() diff --git a/test/test_31db.py b/test/test_31db.py index af530f8..8893f58 100644 --- a/test/test_31db.py +++ b/test/test_31db.py @@ -21,8 +21,8 @@ HOME_PATH = test.constants.HOME_PATH conf = {} # here you can select single testfunction for developping the tests # "test_toolhandling", "test_parseSql" -> test of components -TEST_FUNCTIONS = ["test_formatDbRows", "test_getTechnicalIDFields"] -TEST_FUNCTIONS = ["test_getTechnicalIDFields"] +TEST_FUNCTIONS = ["test_formatDbRows"] +# TEST_FUNCTIONS = ["test_getTechnicalIDFields"] class MyTestCase(unittest.TestCase): mymsg = "--------------------------------------------------------------" diff --git a/utils/config_tool.py b/utils/config_tool.py index 023db30..7ddc343 100644 --- a/utils/config_tool.py +++ b/utils/config_tool.py @@ -44,6 +44,12 @@ def getConfigPath(modul, name, subname=""): job.debug(verify, "getConfig " + modul + ", " + name) #TODO path rejoin, config as const if modul == P.KEY_TOOL: + for format in CONFIG_FORMAT: + pathname = os.path.join(job.conf.getPath(P.ATTR_PATH_COMPONENTS), + P.KEY_TOOL+"_"+name+"."+format) + job.debug(verify, "1 " + pathname) + if os.path.exists(pathname): + return pathname for format in CONFIG_FORMAT: pathname = os.path.join(job.conf.getPath(P.ATTR_PATH_HOME), P.VAL_CONFIG, P.KEY_TOOL+"_"+name+"."+format) diff --git a/utils/i18n_tool.py b/utils/i18n_tool.py new file mode 100644 index 0000000..2388dfb --- /dev/null +++ b/utils/i18n_tool.py @@ -0,0 +1,95 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# --------------------------------------------------------------------------------------------------------- +# Author : Ulrich Carmesin +# Source : gitea.ucarmesin.de +# --------------------------------------------------------------------------------------------------------- +import utils.config_tool +import utils.path_const as P +import basic.program + +DEFAULT_LANGUAGE = "en" +EXP_KEY_MISSING = "key is missing {}" +EXP_KEY_DOESNT_EXIST = "key doesnt exist in domain {}" + +class I18n: + __instance = None + cache = {} + """ + in this class there should be managed each defined key-value-pairs + the pairs ara loaded from the path testdata/catalog: + * initially the csv-file catalog.csv + * on demand other csv-files in the path + """ + + def __init__(self): + self.cache = {} + self.cache = utils.config_tool.getConfig(P.KEY_TOOL, "i18n") + I18n.__instance = self + pass + + + @staticmethod + def getInstance(): + if I18n.__instance == None: + return I18n() + return I18n.__instance + + + def getText(self, key, job=None): + """ + this function gets the text depending on language which is set in job.conf + :param domain: + :param key: + :return: + if not (isinstance(langue, str) and len(langue)): + raise Exception(EXP_KEY_MISSING, (domain, key)) + if not (isinstance(key, str) and len(key)): + raise Exception(EXP_KEY_MISSING, (langue, key)) + + return self.cache[langue][key] + """ + if job is None: + jon = basic.program.Job.getInstance() + language = job.conf.confs["language"] + if language not in self.cache: + raise Exception(EXP_KEY_MISSING, (key)) + print(key) + out = self.extractText(key) + key = self.extractKey(key) + if key in self.cache[language]: + out = self.cache[language][key] + elif key in self.cache[DEFAULT_LANGUAGE]: + out = self.cache[DEFAULT_LANGUAGE][key] + return out + + def getAliasList(self, key, job=None): + if job is None: + jon = basic.program.Job.getInstance() + out = [] + key = self.extractKey(key) + for language in self.cache: + out.append(self.cache[language][key]) + return out + + def extractKey(self, key): + if "=" in key: + i = key.find("=") + x = key[0:i] + if "." in x: + i = x.find(".") + y = x[i + 1:] + else: + y = x + key = y + print("= in key " + y) + else: + print("!= in key") + y = key + return key + + def extractText(self, key): + if "=" in key: + i = key.find("=") + return key[i + 2:-1] + return "" \ No newline at end of file