CoolFace
Apppublic

Krish778/singer_07

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
i18n.py43 linesDownload Raw Back to root
1import json2 3def load_language_list(language):4    try:5        with open(f"./i18n/locale/{language}.json", "r", encoding="utf-8") as f:6            return json.load(f)7    except FileNotFoundError:8        raise FileNotFoundError(9            f"Failed to load language file for {language}. Check if the correct .json file exists."10        )11 12 13class I18nAuto:14    """15    A class used for internationalization using JSON language files.16 17    Examples18    --------19    >>> i18n = I18nAuto('en_US')20    >>> i18n.print()21    Using Language: en_US22    """23    def __init__(self, language=None):24        from locale import getdefaultlocale25        language = language or getdefaultlocale()[0]26        if not self._language_exists(language):27            language = "en_US"28 29        self.language_map = load_language_list(language)30        self.language = language31 32    @staticmethod33    def _language_exists(language):34        from os.path import exists35        return exists(f"./i18n/locale/{language}.json")36 37    def __call__(self, key):38        """Returns the translation of the given key if it exists, else returns the key itself."""39        return self.language_map.get(key, key)40 41    def print(self):42        """Prints the language currently in use."""43        print(f"Using Language: {self.language}")