up to date with volta
This commit is contained in:
parent
0a044f349b
commit
c732049d79
1 changed files with 16 additions and 12 deletions
|
@ -25,7 +25,9 @@ EXCLUDE_DIRS = {'.git', '__pycache__'}
|
||||||
|
|
||||||
locales_dirs = []
|
locales_dirs = []
|
||||||
ENGLISH_MISSING = False
|
ENGLISH_MISSING = False
|
||||||
|
FALLBACK_LOCALE = "en"
|
||||||
|
if os.getenv("fallback_locale"):
|
||||||
|
FALLBACK_LOCALE = os.getenv("fallback_locale")
|
||||||
def find_locales_dirs(base_path):
|
def find_locales_dirs(base_path):
|
||||||
found = []
|
found = []
|
||||||
for root, dirs, files in os.walk(base_path):
|
for root, dirs, files in os.walk(base_path):
|
||||||
|
@ -85,17 +87,17 @@ def set_language(lang: str):
|
||||||
LOCALE = lang
|
LOCALE = lang
|
||||||
else:
|
else:
|
||||||
print(f"[VOLTA] {RED}Language '{lang}' not found, defaulting to 'en'{RESET}")
|
print(f"[VOLTA] {RED}Language '{lang}' not found, defaulting to 'en'{RESET}")
|
||||||
if "en" in translations:
|
if FALLBACK_LOCALE in translations:
|
||||||
LOCALE = "en"
|
LOCALE = FALLBACK_LOCALE
|
||||||
else:
|
else:
|
||||||
print(f"[VOLTA] {RED}The English translations cannot be found! No fallback available.{RESET}")
|
print(f"[VOLTA] {RED}The fallback translations cannot be found! No fallback available.{RESET}")
|
||||||
ENGLISH_MISSING = True
|
ENGLISH_MISSING = True
|
||||||
|
|
||||||
def check_missing_translations():
|
def check_missing_translations():
|
||||||
global LOCALE, ENGLISH_MISSING
|
global LOCALE, ENGLISH_MISSING
|
||||||
load_translations()
|
load_translations()
|
||||||
if "en" not in translations:
|
if FALLBACK_LOCALE not in translations:
|
||||||
print(f"[VOLTA] {RED}English translations (en.json) missing from assets/locales. Exiting.{RESET}")
|
print(f"[VOLTA] {RED}Fallback translations ({FALLBACK_LOCALE}.json) missing from assets/locales. Exiting.{RESET}")
|
||||||
ENGLISH_MISSING = True
|
ENGLISH_MISSING = True
|
||||||
return
|
return
|
||||||
if LOCALE == "en":
|
if LOCALE == "en":
|
||||||
|
@ -113,8 +115,8 @@ def check_missing_translations():
|
||||||
if missing_count > 0:
|
if missing_count > 0:
|
||||||
percent_missing = (missing_count / total_keys) * 100
|
percent_missing = (missing_count / total_keys) * 100
|
||||||
if percent_missing == 100:
|
if percent_missing == 100:
|
||||||
print(f"[VOLTA] {RED}Warning: All keys are missing in locale '{LOCALE}'! Defaulting back to en{RESET}")
|
print(f"[VOLTA] {RED}Warning: All keys are missing in locale '{LOCALE}'! Defaulting back to {FALLBACK_LOCALE}{RESET}")
|
||||||
set_language("en")
|
set_language(FALLBACK_LOCALE)
|
||||||
elif percent_missing > 0:
|
elif percent_missing > 0:
|
||||||
print(f"{YELLOW}Warning: {missing_count}/{total_keys} keys missing in locale '{LOCALE}' ({percent_missing:.1f}%)!{RESET}")
|
print(f"{YELLOW}Warning: {missing_count}/{total_keys} keys missing in locale '{LOCALE}' ({percent_missing:.1f}%)!{RESET}")
|
||||||
for key in sorted(missing_keys):
|
for key in sorted(missing_keys):
|
||||||
|
@ -131,10 +133,10 @@ def get_translation(lang: str, key: str):
|
||||||
if key in lang_translations:
|
if key in lang_translations:
|
||||||
return lang_translations[key]
|
return lang_translations[key]
|
||||||
else:
|
else:
|
||||||
if key not in translations.get("en", {}):
|
if key not in translations.get(FALLBACK_LOCALE, {}):
|
||||||
return f"[VOLTA] {RED}Missing key: '{key}' in en.json!{RESET}"
|
return f"[VOLTA] {RED}Missing key: '{key}' in {FALLBACK_LOCALE}.json!{RESET}"
|
||||||
fallback = translations.get("en", {}).get(key, key)
|
fallback = translations.get(FALLBACK_LOCALE, {}).get(key, key)
|
||||||
print(f"[VOLTA] {RED}Missing key: '{key}' in language '{lang}', falling back to: '{fallback}'{RESET}") # yeah probably print this
|
print(f"[VOLTA] {RED}Missing key: '{key}' in language '{lang}', falling back to: '{fallback}' using {FALLBACK_LOCALE}.json{RESET}") # yeah probably print this
|
||||||
return fallback
|
return fallback
|
||||||
|
|
||||||
def _(key: str) -> str:
|
def _(key: str) -> str:
|
||||||
|
@ -145,3 +147,5 @@ load_translations()
|
||||||
watchdog_thread = threading.Thread(target=reload_if_changed, daemon=True)
|
watchdog_thread = threading.Thread(target=reload_if_changed, daemon=True)
|
||||||
watchdog_thread.start()
|
watchdog_thread.start()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("Volta should not be run directly! Please use it as a module..")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue