import gettext as gettext_module import os import sys import warnings from jogai import settings def to_language(locale): """Turn a locale name (en_US) into a language name (en-us).""" p = locale.find('_') if p >= 0: return locale[:p].lower() + '-' + locale[p + 1 :].lower() else: return locale.lower() def to_locale(language): """Turn a language name (en-us) into a locale name (en_US).""" lang, _, country = language.lower().partition('-') if not country: return language[:3].lower() + language[3:] # A language with > 2 characters after the dash only has its first # character after the dash capitalized; e.g. sr-latn becomes sr_Latn. # A language with 2 characters after the dash has both characters # capitalized; e.g. en-us becomes en_US. country, _, tail = country.partition('-') country = country.title() if len(country) > 2 else country.upper() if tail: country += '-' + tail return lang + '_' + country def gettext(message): """ TODO: Implement this function Translate the 'message' string. """ return message """ eol_message = message.replace('\r\n', '\n').replace('\r', '\n') if eol_message: _translation_object = GameTranslation(settings.LANGUAGE_CODE) result = _translation_object.gettext(eol_message) else: # Return an empty value of the corresponding type if an empty message # is given, instead of metadata, which is the default gettext behavior. result = type(message)('') return result """ class GameTranslation(gettext_module.GNUTranslations): domain = 'jogai' def __init__(self, language, domain=None, localedirs=None): """Create a class to manage game translations""" gettext_module.GNUTranslations.__init__(self) if domain is not None: self.domain = domain self.__language = language self.__locale = to_locale(language) self._manager = None if self.domain == 'jogai': if localedirs is not None: # A module-level cache is used for caching 'django' translations warnings.warn( 'localedirs is ignored when domain is "jogai".', RuntimeWarning, ) localedirs = None self._manager = self._init_translation_manager() if ( self.__language == settings.LANGUAGE_CODE and self.domain == 'jogai' and self._manager is None ): # default lang should have a translation file available. raise OSError( f'No translation files found for language {settings.LANGUAGE_CODE}.' ) def __repr__(self): return f'