-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang.py
More file actions
40 lines (28 loc) · 771 Bytes
/
lang.py
File metadata and controls
40 lines (28 loc) · 771 Bytes
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
import json, os
class Lang:
def __init__(self, filepath, lang_name):
self._file = filepath
self._lang = lang_name
self.load()
def get_name(self):
return self._lang
def load(self):
self.texts = {}
with open(self._file, 'r') as f:
self.texts = json.load(f)
if len(self.texts) < 1:
raise ValueError("locale is empty")
def get(self, key):
return self.texts.get(key, None)
@staticmethod
def create_locale(lang_name, texts):
filepath = f'langs/{lang_name}.json'
with open(filepath, 'w+') as f:
json.dump(texts, f, indent="\t")
return Lang(filepath, lang_name)
@staticmethod
def get_locale(lang_name):
filepath = f'langs/{lang_name}.json'
if not os.path.exists(filepath):
return None
return Lang(filepath, lang_name)