2023-04-12 04:48:39 +02:00
|
|
|
import locale
|
|
|
|
import json
|
2023-04-15 15:45:01 +02:00
|
|
|
import os
|
2023-04-12 04:48:39 +02:00
|
|
|
|
2023-04-15 13:44:24 +02:00
|
|
|
|
2023-04-12 04:48:39 +02:00
|
|
|
def load_language_list(language):
|
2023-04-16 11:30:50 +02:00
|
|
|
with open(f"./i18n/{language}.json", "r", encoding="utf-8") as f:
|
2023-04-12 04:48:39 +02:00
|
|
|
language_list = json.load(f)
|
|
|
|
return language_list
|
|
|
|
|
2023-04-15 13:44:24 +02:00
|
|
|
|
2023-04-12 04:48:39 +02:00
|
|
|
class I18nAuto:
|
|
|
|
def __init__(self, language=None):
|
2023-04-25 04:51:20 +02:00
|
|
|
if language in ["Auto", None]:
|
2023-04-24 14:35:56 +02:00
|
|
|
language = locale.getdefaultlocale()[
|
|
|
|
0
|
|
|
|
] # getlocale can't identify the system's language ((None, None))
|
2023-04-16 11:30:50 +02:00
|
|
|
if not os.path.exists(f"./i18n/{language}.json"):
|
2023-04-15 15:45:01 +02:00
|
|
|
language = "en_US"
|
2023-04-12 04:48:39 +02:00
|
|
|
self.language = language
|
2023-04-13 17:57:27 +02:00
|
|
|
print("Use Language:", language)
|
2023-04-12 10:53:50 +02:00
|
|
|
self.language_map = load_language_list(language)
|
2023-04-12 04:48:39 +02:00
|
|
|
|
|
|
|
def __call__(self, key):
|
2023-05-05 08:18:51 +02:00
|
|
|
return self.language_map.get(key, key)
|