pylanguage.translator
1import pathlib 2import json 3from typing import List 4 5""" 6 Handle translations. All translations must be in <lang_code>.json file. 7""" 8 9class Translator: 10 """ 11 Class that handles translation. 12 """ 13 def __init__(self, base_file: str, lang_code: str, lang_dir: str, fallback_base: bool = True) -> None: 14 """ 15 Initialization code. 16 17 Args: 18 19 - base_file : string: File name containing base translations. 20 - lang_code : string: Language code of the requested translation. 21 - lang_dir : string: Directory where the base file and translations are stored. 22 - fallback_base: string: Use base file if translation not found. Default is True. 23 24 Returns: 25 26 - None 27 """ 28 self.fallback_base = fallback_base # Enable fallback to base file 29 self.lang_code = lang_code # Language code 30 self.lang_dir = lang_dir # Location of translations 31 self.lang_file = pathlib.Path().joinpath(self.lang_dir, self.lang_code + '.json') # Translation file 32 self.base_file = pathlib.Path().joinpath(self.lang_dir, base_file) # Base file 33 self.translations = {} # Translations 34 self.base = {} # Base strings 35 36 # Check if base file exist. If it does, get the base translation. 37 if pathlib.Path(self.base_file).exists(): 38 f = open(self.base_file) 39 try: 40 self.base = json.load(f) 41 except Exception: 42 raise Exception(f"Error parsing {self.base_file.resolve()}. Check if file is valid JSON.") 43 else: 44 raise FileNotFoundError(f"The file {self.base_file.resolve()} does not exist.") 45 46 # Check if translation file exist. If it does, get the translation. 47 if pathlib.Path(self.lang_file).exists(): 48 f = open(self.lang_file) 49 try: 50 self.translations = json.load(f) 51 except Exception: 52 raise Exception(f"Error parsing {self.lang_file.resolve()}. Check if file is valid JSON.") 53 else: 54 if not self.fallback_base: 55 raise FileNotFoundError(f"The file {self.lang_file.resolve()} does not exist.") 56 else: 57 self.translations = self.base 58 59 # Validate the translations. 60 for i in self.base.keys(): 61 if not i in self.translations.keys(): 62 raise Exception(f"Error parsing {self.translations.resolve()}. All keys in {self.base_file.resolve()} are not found in translations.") 63 64 # Validate the base. 65 for i in self.translations.keys(): 66 if not i in self.base.keys(): 67 raise Exception(f"Error parsing {self.base_file.resolve()}. All keys in {self.translations.resolve()} are not found in base file.") 68 69 def get(self, string: str): 70 """ 71 Get the translation of the string. 72 73 Args: 74 75 - string: string: String whose translation is to be returned. 76 77 Returns: 78 79 - translation: string: The translation corresponding to string. 80 """ 81 return self.translations[string] 82 83 def get_lang(self): 84 """ 85 Get the language code. 86 87 Args: 88 89 - None 90 91 Returns: 92 93 - lang_code: string: Language code of the translation file in use. 94 """ 95 return self.lang_code
class
Translator:
10class Translator: 11 """ 12 Class that handles translation. 13 """ 14 def __init__(self, base_file: str, lang_code: str, lang_dir: str, fallback_base: bool = True) -> None: 15 """ 16 Initialization code. 17 18 Args: 19 20 - base_file : string: File name containing base translations. 21 - lang_code : string: Language code of the requested translation. 22 - lang_dir : string: Directory where the base file and translations are stored. 23 - fallback_base: string: Use base file if translation not found. Default is True. 24 25 Returns: 26 27 - None 28 """ 29 self.fallback_base = fallback_base # Enable fallback to base file 30 self.lang_code = lang_code # Language code 31 self.lang_dir = lang_dir # Location of translations 32 self.lang_file = pathlib.Path().joinpath(self.lang_dir, self.lang_code + '.json') # Translation file 33 self.base_file = pathlib.Path().joinpath(self.lang_dir, base_file) # Base file 34 self.translations = {} # Translations 35 self.base = {} # Base strings 36 37 # Check if base file exist. If it does, get the base translation. 38 if pathlib.Path(self.base_file).exists(): 39 f = open(self.base_file) 40 try: 41 self.base = json.load(f) 42 except Exception: 43 raise Exception(f"Error parsing {self.base_file.resolve()}. Check if file is valid JSON.") 44 else: 45 raise FileNotFoundError(f"The file {self.base_file.resolve()} does not exist.") 46 47 # Check if translation file exist. If it does, get the translation. 48 if pathlib.Path(self.lang_file).exists(): 49 f = open(self.lang_file) 50 try: 51 self.translations = json.load(f) 52 except Exception: 53 raise Exception(f"Error parsing {self.lang_file.resolve()}. Check if file is valid JSON.") 54 else: 55 if not self.fallback_base: 56 raise FileNotFoundError(f"The file {self.lang_file.resolve()} does not exist.") 57 else: 58 self.translations = self.base 59 60 # Validate the translations. 61 for i in self.base.keys(): 62 if not i in self.translations.keys(): 63 raise Exception(f"Error parsing {self.translations.resolve()}. All keys in {self.base_file.resolve()} are not found in translations.") 64 65 # Validate the base. 66 for i in self.translations.keys(): 67 if not i in self.base.keys(): 68 raise Exception(f"Error parsing {self.base_file.resolve()}. All keys in {self.translations.resolve()} are not found in base file.") 69 70 def get(self, string: str): 71 """ 72 Get the translation of the string. 73 74 Args: 75 76 - string: string: String whose translation is to be returned. 77 78 Returns: 79 80 - translation: string: The translation corresponding to string. 81 """ 82 return self.translations[string] 83 84 def get_lang(self): 85 """ 86 Get the language code. 87 88 Args: 89 90 - None 91 92 Returns: 93 94 - lang_code: string: Language code of the translation file in use. 95 """ 96 return self.lang_code
Class that handles translation.
Translator( base_file: str, lang_code: str, lang_dir: str, fallback_base: bool = True)
14 def __init__(self, base_file: str, lang_code: str, lang_dir: str, fallback_base: bool = True) -> None: 15 """ 16 Initialization code. 17 18 Args: 19 20 - base_file : string: File name containing base translations. 21 - lang_code : string: Language code of the requested translation. 22 - lang_dir : string: Directory where the base file and translations are stored. 23 - fallback_base: string: Use base file if translation not found. Default is True. 24 25 Returns: 26 27 - None 28 """ 29 self.fallback_base = fallback_base # Enable fallback to base file 30 self.lang_code = lang_code # Language code 31 self.lang_dir = lang_dir # Location of translations 32 self.lang_file = pathlib.Path().joinpath(self.lang_dir, self.lang_code + '.json') # Translation file 33 self.base_file = pathlib.Path().joinpath(self.lang_dir, base_file) # Base file 34 self.translations = {} # Translations 35 self.base = {} # Base strings 36 37 # Check if base file exist. If it does, get the base translation. 38 if pathlib.Path(self.base_file).exists(): 39 f = open(self.base_file) 40 try: 41 self.base = json.load(f) 42 except Exception: 43 raise Exception(f"Error parsing {self.base_file.resolve()}. Check if file is valid JSON.") 44 else: 45 raise FileNotFoundError(f"The file {self.base_file.resolve()} does not exist.") 46 47 # Check if translation file exist. If it does, get the translation. 48 if pathlib.Path(self.lang_file).exists(): 49 f = open(self.lang_file) 50 try: 51 self.translations = json.load(f) 52 except Exception: 53 raise Exception(f"Error parsing {self.lang_file.resolve()}. Check if file is valid JSON.") 54 else: 55 if not self.fallback_base: 56 raise FileNotFoundError(f"The file {self.lang_file.resolve()} does not exist.") 57 else: 58 self.translations = self.base 59 60 # Validate the translations. 61 for i in self.base.keys(): 62 if not i in self.translations.keys(): 63 raise Exception(f"Error parsing {self.translations.resolve()}. All keys in {self.base_file.resolve()} are not found in translations.") 64 65 # Validate the base. 66 for i in self.translations.keys(): 67 if not i in self.base.keys(): 68 raise Exception(f"Error parsing {self.base_file.resolve()}. All keys in {self.translations.resolve()} are not found in base file.")
Initialization code.
Args:
- base_file : string: File name containing base translations.
- lang_code : string: Language code of the requested translation.
- lang_dir : string: Directory where the base file and translations are stored.
- fallback_base: string: Use base file if translation not found. Default is True.
Returns:
- None
def
get(self, string: str):
70 def get(self, string: str): 71 """ 72 Get the translation of the string. 73 74 Args: 75 76 - string: string: String whose translation is to be returned. 77 78 Returns: 79 80 - translation: string: The translation corresponding to string. 81 """ 82 return self.translations[string]
Get the translation of the string.
Args:
- string: string: String whose translation is to be returned.
Returns:
- translation: string: The translation corresponding to string.
def
get_lang(self):
84 def get_lang(self): 85 """ 86 Get the language code. 87 88 Args: 89 90 - None 91 92 Returns: 93 94 - lang_code: string: Language code of the translation file in use. 95 """ 96 return self.lang_code
Get the language code.
Args:
- None
Returns:
- lang_code: string: Language code of the translation file in use.