diff --git a/i18n.py b/i18n.py index d64f2ea..28b17c7 100644 --- a/i18n.py +++ b/i18n.py @@ -4,7 +4,7 @@ import os def load_language_list(language): - with open(f"./lib/i18n/{language}.json", "r", encoding="utf-8") as f: + with open(f"./i18n/locale/{language}.json", "r", encoding="utf-8") as f: language_list = json.load(f) return language_list diff --git a/i18n/i18n.py b/i18n/i18n.py new file mode 100644 index 0000000..28b17c7 --- /dev/null +++ b/i18n/i18n.py @@ -0,0 +1,28 @@ +import locale +import json +import os + + +def load_language_list(language): + with open(f"./i18n/locale/{language}.json", "r", encoding="utf-8") as f: + language_list = json.load(f) + return language_list + + +class I18nAuto: + def __init__(self, language=None): + if language in ["Auto", None]: + language = locale.getdefaultlocale()[ + 0 + ] # getlocale can't identify the system's language ((None, None)) + if not os.path.exists(f"./lib/i18n/{language}.json"): + language = "en_US" + self.language = language + # print("Use Language:", language) + self.language_map = load_language_list(language) + + def __call__(self, key): + return self.language_map.get(key, key) + + def print(self): + print("Use Language:", self.language) diff --git a/lib/i18n/en_US.json b/i18n/locale/en_US.json similarity index 100% rename from lib/i18n/en_US.json rename to i18n/locale/en_US.json diff --git a/lib/i18n/es_ES.json b/i18n/locale/es_ES.json similarity index 100% rename from lib/i18n/es_ES.json rename to i18n/locale/es_ES.json diff --git a/lib/i18n/it_IT.json b/i18n/locale/it_IT.json similarity index 100% rename from lib/i18n/it_IT.json rename to i18n/locale/it_IT.json diff --git a/lib/i18n/ja_JP.json b/i18n/locale/ja_JP.json similarity index 100% rename from lib/i18n/ja_JP.json rename to i18n/locale/ja_JP.json diff --git a/lib/i18n/ru_RU.json b/i18n/locale/ru_RU.json similarity index 100% rename from lib/i18n/ru_RU.json rename to i18n/locale/ru_RU.json diff --git a/lib/i18n/tr_TR.json b/i18n/locale/tr_TR.json similarity index 100% rename from lib/i18n/tr_TR.json rename to i18n/locale/tr_TR.json diff --git a/lib/i18n/zh_CN.json b/i18n/locale/zh_CN.json similarity index 100% rename from lib/i18n/zh_CN.json rename to i18n/locale/zh_CN.json diff --git a/lib/i18n/zh_HK.json b/i18n/locale/zh_HK.json similarity index 100% rename from lib/i18n/zh_HK.json rename to i18n/locale/zh_HK.json diff --git a/lib/i18n/zh_SG.json b/i18n/locale/zh_SG.json similarity index 100% rename from lib/i18n/zh_SG.json rename to i18n/locale/zh_SG.json diff --git a/lib/i18n/zh_TW.json b/i18n/locale/zh_TW.json similarity index 100% rename from lib/i18n/zh_TW.json rename to i18n/locale/zh_TW.json diff --git a/lib/i18n/locale_diff.py b/i18n/locale_diff.py similarity index 98% rename from lib/i18n/locale_diff.py rename to i18n/locale_diff.py index 2572779..196829b 100644 --- a/lib/i18n/locale_diff.py +++ b/i18n/locale_diff.py @@ -6,7 +6,7 @@ from collections import OrderedDict standard_file = "zh_CN.json" # Find all JSON files in the directory -dir_path = "./" +dir_path = "i18n/locale" languages = [ f for f in os.listdir(dir_path) if f.endswith(".json") and f != standard_file ]