caching up to 600 entries :p
This commit is contained in:
parent
628de5f76e
commit
4aaf2dc336
1 changed files with 24 additions and 15 deletions
33
main.py
33
main.py
|
@ -9,6 +9,7 @@ import pathlib
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
ANSI = "\033["
|
ANSI = "\033["
|
||||||
RED = f"{ANSI}31m"
|
RED = f"{ANSI}31m"
|
||||||
|
@ -140,6 +141,7 @@ def set_language(lang: str):
|
||||||
else:
|
else:
|
||||||
print(f"[VOLTA] {RED}The fallback 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
|
||||||
|
_lookup_translation.cache_clear()
|
||||||
|
|
||||||
def check_missing_translations():
|
def check_missing_translations():
|
||||||
global LOCALE, ENGLISH_MISSING
|
global LOCALE, ENGLISH_MISSING
|
||||||
|
@ -175,24 +177,31 @@ def check_missing_translations():
|
||||||
|
|
||||||
printedsystemfallback = False
|
printedsystemfallback = False
|
||||||
|
|
||||||
|
@lru_cache(maxsize=600)
|
||||||
|
def _lookup_translation(lang: str, key: str):
|
||||||
|
return translations.get(lang, {}).get(key)
|
||||||
|
|
||||||
def get_translation(lang: str, key: str):
|
def get_translation(lang: str, key: str):
|
||||||
global printedsystemfallback
|
global printedsystemfallback
|
||||||
if ENGLISH_MISSING:
|
if ENGLISH_MISSING:
|
||||||
return f"[VOLTA] {RED}No fallback available!{RESET}"
|
return f"[VOLTA] {RED}No fallback available!{RESET}"
|
||||||
fallback_translations = translations.get(FALLBACK_LOCALE, {})
|
|
||||||
|
val = _lookup_translation(lang, key)
|
||||||
|
if val:
|
||||||
|
return val
|
||||||
sys_lang = get_system_locale().split("_")[0] if get_system_locale() else None
|
sys_lang = get_system_locale().split("_")[0] if get_system_locale() else None
|
||||||
sys_translations = translations.get(sys_lang, {}) if sys_lang else {}
|
if sys_lang and sys_lang != lang:
|
||||||
lang_translations = translations.get(lang, {})
|
sys_val = _lookup_translation(sys_lang, key)
|
||||||
if key in lang_translations:
|
if sys_val:
|
||||||
return lang_translations[key]
|
if not printedsystemfallback:
|
||||||
if sys_lang and sys_lang != lang and key in sys_translations:
|
print(f"[VOLTA] {YELLOW}Falling back to system language {sys_lang}!{RESET}")
|
||||||
if not printedsystemfallback:
|
printedsystemfallback = True
|
||||||
print(f"[VOLTA] {YELLOW}Falling back to system language {sys_lang}!{RESET}")
|
return sys_val
|
||||||
printedsystemfallback = True
|
fallback_val = _lookup_translation(FALLBACK_LOCALE, key)
|
||||||
return sys_translations[key]
|
if fallback_val:
|
||||||
if key in fallback_translations:
|
|
||||||
print(f"[VOLTA] {YELLOW}Missing key: '{key}' in '{lang}', falling back to fallback locale '{FALLBACK_LOCALE}'{RESET}")
|
print(f"[VOLTA] {YELLOW}Missing key: '{key}' in '{lang}', falling back to fallback locale '{FALLBACK_LOCALE}'{RESET}")
|
||||||
return fallback_translations[key]
|
return fallback_val
|
||||||
|
|
||||||
return f"[VOLTA] {YELLOW}Missing key: '{key}' in all locales!{RESET}"
|
return f"[VOLTA] {YELLOW}Missing key: '{key}' in all locales!{RESET}"
|
||||||
|
|
||||||
def _(key: str) -> str:
|
def _(key: str) -> str:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue