-
Notifications
You must be signed in to change notification settings - Fork 15
/
translator.py
34 lines (29 loc) · 1.29 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
import sys
from os import path
import json
from .parser import TranslatorInterface
from typing import Optional
class Translator(TranslatorInterface):
def __init__(self, translations_dir):
self.translations_dir = translations_dir
self.translations = {}
def load_translations(self, translation_file: str) -> bool:
if translation_file not in self.translations:
try:
with open(path.join(self.translations_dir, translation_file), 'r') as fin:
self.translations[translation_file] = json.load(fin)
except (FileNotFoundError, json.decoder.JSONDecodeError):
print('Invalid translation configuration.', file=sys.stderr)
return False
return True
def translate(self, formatted_cmd, input_args: dict, translation_file: str) -> Optional[str]:
if isinstance(formatted_cmd, list):
formatted_cmd = ' '.join(formatted_cmd)
if not isinstance(formatted_cmd, str):
raise TypeError
load_success = self.load_translations(translation_file)
if not load_success:
return None
translation_dict = self.translations[translation_file]
base_str = translation_dict[formatted_cmd]
return base_str.format(**input_args)