translations.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import gettext as gettext_module
  2. import os
  3. import sys
  4. import warnings
  5. from jogai import settings
  6. def to_language(locale):
  7. """Turn a locale name (en_US) into a language name (en-us)."""
  8. p = locale.find('_')
  9. if p >= 0:
  10. return locale[:p].lower() + '-' + locale[p + 1 :].lower()
  11. else:
  12. return locale.lower()
  13. def to_locale(language):
  14. """Turn a language name (en-us) into a locale name (en_US)."""
  15. lang, _, country = language.lower().partition('-')
  16. if not country:
  17. return language[:3].lower() + language[3:]
  18. # A language with > 2 characters after the dash only has its first
  19. # character after the dash capitalized; e.g. sr-latn becomes sr_Latn.
  20. # A language with 2 characters after the dash has both characters
  21. # capitalized; e.g. en-us becomes en_US.
  22. country, _, tail = country.partition('-')
  23. country = country.title() if len(country) > 2 else country.upper()
  24. if tail:
  25. country += '-' + tail
  26. return lang + '_' + country
  27. def gettext(message):
  28. """
  29. TODO: Implement this function
  30. Translate the 'message' string.
  31. """
  32. return message
  33. """
  34. eol_message = message.replace('\r\n', '\n').replace('\r', '\n')
  35. if eol_message:
  36. _translation_object = GameTranslation(settings.LANGUAGE_CODE)
  37. result = _translation_object.gettext(eol_message)
  38. else:
  39. # Return an empty value of the corresponding type if an empty message
  40. # is given, instead of metadata, which is the default gettext behavior.
  41. result = type(message)('')
  42. return result
  43. """
  44. class GameTranslation(gettext_module.GNUTranslations):
  45. domain = 'jogai'
  46. def __init__(self, language, domain=None, localedirs=None):
  47. """Create a class to manage game translations"""
  48. gettext_module.GNUTranslations.__init__(self)
  49. if domain is not None:
  50. self.domain = domain
  51. self.__language = language
  52. self.__locale = to_locale(language)
  53. self._manager = None
  54. if self.domain == 'jogai':
  55. if localedirs is not None:
  56. # A module-level cache is used for caching 'django' translations
  57. warnings.warn(
  58. 'localedirs is ignored when domain is "jogai".',
  59. RuntimeWarning,
  60. )
  61. localedirs = None
  62. self._manager = self._init_translation_manager()
  63. if (
  64. self.__language == settings.LANGUAGE_CODE
  65. and self.domain == 'jogai'
  66. and self._manager is None
  67. ):
  68. # default lang should have a translation file available.
  69. raise OSError(
  70. f'No translation files found for language {settings.LANGUAGE_CODE}.'
  71. )
  72. def __repr__(self):
  73. return f'<GameTranslation lang: {self.__language}'
  74. def _init_translation_manager(self):
  75. settingsfile = sys.modules[settings.__module__].__file__
  76. localedir = os.path.join(os.path.dirname(settingsfile), 'locale')
  77. return self._new_gnu_trans(localedir)
  78. def _new_gnu_trans(self, localedir):
  79. return gettext_module.translation(
  80. domain=self.domain,
  81. localedir=localedir,
  82. languages=[self.__locale],
  83. )
  84. def language(self):
  85. """Return the translation language."""
  86. return self.__language