Files
Ultroid-fork/strings/strings.py
Devesh Pal 6c7af44f09 Ultroid v0.1 Updates 13.09.2021
Co-authored-by: Danish <danish@ultroid.tech>
Co-authored-by: Aditya <me@xditya.me>
Co-authored-by: Amit Sharma <48654350+buddhhu@users.noreply.github.com>
Co-authored-by: sppidy <sppidy@users.noreply.github.com>
Co-authored-by: Arnab Paryali <Arnabxd@users.noreply.github.com>
Co-authored-by: divkix <divkix@users.noreply.github.com>
Co-authored-by: hellboi_atul <hellboi-atul@users.noreply.github.com>
Co-authored-by: Programming Error <error@notavailable.live>
2021-09-13 01:03:39 +05:30

40 lines
1.1 KiB
Python

from os import listdir, path
from typing import Any, Dict, List, Union
from google_trans_new import google_translator
from pyUltroid import udB
from yaml import safe_load
languages = {}
Trs = google_translator()
strings_folder = path.join(path.dirname(path.realpath(__file__)), "strings")
for file in listdir(strings_folder):
if file.endswith(".yml"):
code = file[:-4]
languages[code] = safe_load(
open(path.join(strings_folder, file), encoding="UTF-8"),
)
def get_string(key: str) -> Any:
try:
return languages[(udB.get("language") or "en")][key]
except KeyError:
try:
return Trs.translate(languages["en"][key], lang_tgt=udB.get("language"))
except KeyError:
return f"Warning: could not load any string with the key {key}"
def get_languages() -> Dict[str, Union[str, List[str]]]:
return {
code: {
"name": languages[code]["name"],
"natively": languages[code]["natively"],
"authors": languages[code]["authors"],
}
for code in languages
}