-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
executable file
·89 lines (76 loc) · 3.83 KB
/
translator.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json
from random import randint
class Translator():
"""
Class to create language dynamic applications based on json files.
Author: official-Cromatin
Source: https://github.com/official-Cromatin/Python-Translator
Version: V2.0
"""
# You are allowed to change these
path_prefix = "./lang/" # Path under which the json files are found
file_format = ".json" # File format which should be loaded
placeholder_prefix = "{" # Initial character with which a placeholder is marked
placeholder_suffix = "}" # End character with which a placeholder is marked
error_no_attribute = "Placeholder" # Value to be used for an unspecified placeholder
error_file_not_found = "File not found for specified language" # Value to return if the file was not found
error_file_not_json = "File is not a valid json format" # Value to return if the file is not in json format
error_no_key_found = "Translation not found" # Value to return if the translation was not found
# But fingers off these
current_languages = []
cached_language = {}
def translate(lang:str, key:str, pick_random_array_value:bool = True, **kwargs):
"""
Main method of the class, it calls values depending on the key, adjusts replacement values and returns them as string
Arguments:
----------
- `lang`: Name of the file (excluding the filename extension) to be loaded
- `key`: Name of the key from which the value is to be retrieved
- `pick_random_array_value`: If the value for a json value is an array ...
- `True`: ... a value is selected randomly.
- `False`: ... the first value is selected.
- `**kwargs`: Optional arguments whose values replace the placeholders from the json value
Returns:
--------
`str`: String reflecting the entry of the corresponding json file
"""
if not lang in Translator.current_languages:
try:
text_wrapper = open(Translator.path_prefix + lang + Translator.file_format)
except FileNotFoundError:
return Translator.error_file_not_found
else:
try:
Translator.cached_language[lang] = json.load(text_wrapper)
except json.decoder.JSONDecodeError:
return Translator.error_file_not_json
else:
Translator.current_languages.append(lang)
try:
value:str = Translator.cached_language[lang][key]
except KeyError:
return Translator.error_no_key_found
if isinstance(value, list):
if pick_random_array_value:
value = value[randint(0, len(value) - 1)]
else:
value = value[0]
if kwargs.items():
opt_args = dict(kwargs.items())
runs = 0
for letter in value:
if letter == Translator.placeholder_prefix:
runs += 1
for _ in range(runs):
begin = value.rindex(Translator.placeholder_prefix) + len(Translator.placeholder_prefix)
end = value.rindex(Translator.placeholder_suffix)
placeholder = value[begin:end]
if placeholder in opt_args:
value = value.replace(Translator.placeholder_prefix + placeholder + Translator.placeholder_suffix, str(opt_args[placeholder]), 1)
else:
value = value.replace(Translator.placeholder_prefix + placeholder + Translator.placeholder_suffix, Translator.error_no_attribute, 1)
return value
def force_reload():
"""Method to clear the temporary cache. It causes a reread of the json files"""
Translator.current_languages = []
Translator.cached_language = {}