| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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'<GameTranslation lang: {self.__language}'
- def _init_translation_manager(self):
- settingsfile = sys.modules[settings.__module__].__file__
- localedir = os.path.join(os.path.dirname(settingsfile), 'locale')
- return self._new_gnu_trans(localedir)
- def _new_gnu_trans(self, localedir):
- return gettext_module.translation(
- domain=self.domain,
- localedir=localedir,
- languages=[self.__locale],
- )
- def language(self):
- """Return the translation language."""
- return self.__language
|