Tuchuanhuhuhu commited on
Commit
9f7176b
1 Parent(s): b3d9df8

chore: Support both styles like en_US and en-US

Browse files
Files changed (2) hide show
  1. modules/config.py +0 -4
  2. modules/webui_locale.py +9 -2
modules/config.py CHANGED
@@ -45,10 +45,6 @@ def load_config_to_environ(key_list):
45
  if key in config:
46
  os.environ[key.upper()] = os.environ.get(key.upper(), config[key])
47
 
48
-
49
- lang_config = config.get("language", "auto")
50
- language = os.environ.get("LANGUAGE", lang_config)
51
-
52
  hide_history_when_not_logged_in = config.get(
53
  "hide_history_when_not_logged_in", False)
54
  check_update = config.get("check_update", True)
 
45
  if key in config:
46
  os.environ[key.upper()] = os.environ.get(key.upper(), config[key])
47
 
 
 
 
 
48
  hide_history_when_not_logged_in = config.get(
49
  "hide_history_when_not_logged_in", False)
50
  check_update = config.get("check_update", True)
modules/webui_locale.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import locale
 
3
  import commentjson as json
4
 
5
  class I18nAuto:
@@ -9,8 +10,9 @@ class I18nAuto:
9
  config = json.load(f)
10
  else:
11
  config = {}
12
- lang_config = config.get("language", "auto")
13
- language = os.environ.get("LANGUAGE", lang_config)
 
14
  if language == "auto":
15
  language = locale.getdefaultlocale()[0] # get the language code of the system (ex. zh_CN)
16
  self.language_map = {}
@@ -18,6 +20,11 @@ class I18nAuto:
18
  if self.file_is_exists:
19
  with open(f"./locale/{language}.json", "r", encoding="utf-8") as f:
20
  self.language_map.update(json.load(f))
 
 
 
 
 
21
 
22
  def __call__(self, key):
23
  if self.file_is_exists and key in self.language_map:
 
1
  import os
2
  import locale
3
+ import logging
4
  import commentjson as json
5
 
6
  class I18nAuto:
 
10
  config = json.load(f)
11
  else:
12
  config = {}
13
+ language = config.get("language", "auto")
14
+ language = os.environ.get("LANGUAGE", language)
15
+ language = language.replace("-", "_")
16
  if language == "auto":
17
  language = locale.getdefaultlocale()[0] # get the language code of the system (ex. zh_CN)
18
  self.language_map = {}
 
20
  if self.file_is_exists:
21
  with open(f"./locale/{language}.json", "r", encoding="utf-8") as f:
22
  self.language_map.update(json.load(f))
23
+ else:
24
+ logging.warning(f"Language file for {language} does not exist. Using English instead.")
25
+ logging.warning(f"Available languages: {', '.join([x[:-5] for x in os.listdir('./locale')])}")
26
+ with open(f"./locale/en_US.json", "r", encoding="utf-8") as f:
27
+ self.language_map.update(json.load(f))
28
 
29
  def __call__(self, key):
30
  if self.file_is_exists and key in self.language_map: