forked from azlux/botamusique
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constants.py
63 lines (51 loc) · 1.94 KB
/
constants.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import json
import variables as var
default_lang_dict = {}
lang_dict = {}
def load_lang(lang):
global lang_dict, default_lang_dict
root_dir = os.path.dirname(__file__)
with open(os.path.join(root_dir, "lang/en_US.json"), "r") as f:
default_lang_dict = json.load(f)
with open(os.path.join(root_dir, f"lang/{lang}.json"), "r") as f:
lang_dict = json.load(f)
def tr_cli(option, *argv, **kwargs):
try:
if option in lang_dict['cli'] and lang_dict['cli'][option]:
string = lang_dict['cli'][option]
else:
string = default_lang_dict['cli'][option]
except KeyError:
raise KeyError("Missed strings in language file: '{string}'. ".format(string=option))
return _tr(string, *argv, **kwargs)
def tr_web(option, *argv, **kwargs):
try:
if option in lang_dict['web'] and lang_dict['web'][option]:
string = lang_dict['web'][option]
else:
string = default_lang_dict['web'][option]
except KeyError:
raise KeyError("Missed strings in language file: '{string}'. ".format(string=option))
return _tr(string, *argv, **kwargs)
def _tr(string, *argv, **kwargs):
if argv or kwargs:
try:
formatted = string.format(*argv, **kwargs)
return formatted
except KeyError as e:
raise KeyError(
"Missed/Unexpected placeholder {{{placeholder}}} in string "
"'{string}'. ".format(placeholder=str(e).strip("'"),
string=string))
except TypeError:
raise KeyError(
"Missed placeholder in string '{string}'. ".format(string=string))
else:
return string
def commands(command):
try:
string = var.config.get("commands", command)
return string
except KeyError:
raise KeyError("Missed command in configuration file: '{string}'. ".format(string=command))