i18n.py 790 B

123456789101112131415161718192021222324252627
  1. import json
  2. import locale
  3. import os
  4. def load_language_list(language):
  5. with open(f"./i18n/locale/{language}.json", "r", encoding="utf-8") as f:
  6. language_list = json.load(f)
  7. return language_list
  8. class I18nAuto:
  9. def __init__(self, language=None):
  10. if language in ["Auto", None]:
  11. language = locale.getdefaultlocale()[
  12. 0
  13. ] # getlocale can't identify the system's language ((None, None))
  14. if not os.path.exists(f"./i18n/locale/{language}.json"):
  15. language = "en_US"
  16. self.language = language
  17. self.language_map = load_language_list(language)
  18. def __call__(self, key):
  19. return self.language_map.get(key, key)
  20. def __repr__(self):
  21. return "Use Language: " + self.language