Files
Ultroid-fork/strings/strings.py
Devesh Pal 0df53caf4c Ultroid v0.3 Updates
Co-authored-by: Aditya <xditya@ultroid.tech>
Co-authored-by: Danish <danish@ultroid.tech>
Co-authored-by: Amit Sharma <48654350+buddhhu@users.noreply.github.com>
Co-authored-by: TechiError <error@notavailable.live>
Co-authored-by: Avish Kumar <85635883+aviskumar@users.noreply.github.com>
Co-authored-by: Vɪɴᴀʏᴀᴋ Pᴀɴᴅᴇʏ <87496159+harpia-vieillot@users.noreply.github.com>
Co-authored-by: Shrimadhav U K <6317196+spechide@users.noreply.github.com>
Co-authored-by: Dark <darkbeamer.official@gmail.com>
Co-authored-by: Muhamad Risman Aziz <62795826+mrismanaziz@users.noreply.github.com>
Co-authored-by: Ashik Muhammed <84127769+MR-JINN-OF-TG@users.noreply.github.com>
Co-authored-by: MMETMA <79155572+MMETMA@users.noreply.github.com>
Co-authored-by: amirmehdinzri <94852182+amirmehdinzri@users.noreply.github.com>
2021-12-31 23:48:53 +05:30

55 lines
1.5 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, LOGS
from yaml import safe_load
language = [udB.get_key("language") or "en"]
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]
try:
languages[code] = safe_load(
open(path.join(strings_folder, file), encoding="UTF-8"),
)
except Exception as er:
LOGS.info(f"Error in {file[:-4]} language file")
LOGS.exception(er)
def get_string(key: str) -> Any:
lang = language[0]
try:
return languages[lang][key]
except KeyError:
try:
en_ = languages["en"][key]
tr = Trs.translate(en_, lang_tgt=lang).replace("\ N", "\n")
if en_.count("{}") != tr.count("{}"):
tr = en_
if languages.get(lang):
languages[lang][key] = tr
else:
languages.update({lang: {key: tr}})
return tr
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
}