PageRenderTime 28ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/poet/trunk/pythonLibs/Django-1.3/build/lib/django/utils/translation/__init__.py

https://bitbucket.org/ssaltzman/poet
Python | 145 lines | 89 code | 29 blank | 27 comment | 5 complexity | 1884c2bf1940a7ef3a96b42b8535dd6c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, BSD-3-Clause
  1. """
  2. Internationalization support.
  3. """
  4. import warnings
  5. from os import path
  6. from django.utils.encoding import force_unicode
  7. from django.utils.functional import lazy
  8. from django.utils.importlib import import_module
  9. __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',
  10. 'ngettext_lazy', 'string_concat', 'activate', 'deactivate',
  11. 'get_language', 'get_language_bidi', 'get_date_formats',
  12. 'get_partial_date_formats', 'check_for_language', 'to_locale',
  13. 'get_language_from_request', 'templatize', 'ugettext', 'ugettext_lazy',
  14. 'ungettext', 'ungettext_lazy', 'pgettext', 'pgettext_lazy',
  15. 'npgettext', 'npgettext_lazy', 'deactivate_all', 'get_language_info']
  16. # Here be dragons, so a short explanation of the logic won't hurt:
  17. # We are trying to solve two problems: (1) access settings, in particular
  18. # settings.USE_I18N, as late as possible, so that modules can be imported
  19. # without having to first configure Django, and (2) if some other code creates
  20. # a reference to one of these functions, don't break that reference when we
  21. # replace the functions with their real counterparts (once we do access the
  22. # settings).
  23. class Trans(object):
  24. """
  25. The purpose of this class is to store the actual translation function upon
  26. receiving the first call to that function. After this is done, changes to
  27. USE_I18N will have no effect to which function is served upon request. If
  28. your tests rely on changing USE_I18N, you can delete all the functions
  29. from _trans.__dict__.
  30. Note that storing the function with setattr will have a noticeable
  31. performance effect, as access to the function goes the normal path,
  32. instead of using __getattr__.
  33. """
  34. def __getattr__(self, real_name):
  35. from django.conf import settings
  36. if settings.USE_I18N:
  37. from django.utils.translation import trans_real as trans
  38. # Make sure the project's locale dir isn't in LOCALE_PATHS
  39. if settings.SETTINGS_MODULE is not None:
  40. parts = settings.SETTINGS_MODULE.split('.')
  41. project = import_module(parts[0])
  42. project_locale_path = path.normpath(
  43. path.join(path.dirname(project.__file__), 'locale'))
  44. normalized_locale_paths = [path.normpath(locale_path)
  45. for locale_path in settings.LOCALE_PATHS]
  46. if (path.isdir(project_locale_path) and
  47. not project_locale_path in normalized_locale_paths):
  48. warnings.warn("Translations in the project directory "
  49. "aren't supported anymore. Use the "
  50. "LOCALE_PATHS setting instead.",
  51. PendingDeprecationWarning)
  52. else:
  53. from django.utils.translation import trans_null as trans
  54. setattr(self, real_name, getattr(trans, real_name))
  55. return getattr(trans, real_name)
  56. _trans = Trans()
  57. # The Trans class is no more needed, so remove it from the namespace.
  58. del Trans
  59. def gettext_noop(message):
  60. return _trans.gettext_noop(message)
  61. ugettext_noop = gettext_noop
  62. def gettext(message):
  63. return _trans.gettext(message)
  64. def ngettext(singular, plural, number):
  65. return _trans.ngettext(singular, plural, number)
  66. def ugettext(message):
  67. return _trans.ugettext(message)
  68. def ungettext(singular, plural, number):
  69. return _trans.ungettext(singular, plural, number)
  70. def pgettext(context, message):
  71. return _trans.pgettext(context, message)
  72. def npgettext(context, singular, plural, number):
  73. return _trans.npgettext(context, singular, plural, number)
  74. ngettext_lazy = lazy(ngettext, str)
  75. gettext_lazy = lazy(gettext, str)
  76. ungettext_lazy = lazy(ungettext, unicode)
  77. ugettext_lazy = lazy(ugettext, unicode)
  78. pgettext_lazy = lazy(pgettext, unicode)
  79. npgettext_lazy = lazy(npgettext, unicode)
  80. def activate(language):
  81. return _trans.activate(language)
  82. def deactivate():
  83. return _trans.deactivate()
  84. def get_language():
  85. return _trans.get_language()
  86. def get_language_bidi():
  87. return _trans.get_language_bidi()
  88. def get_date_formats():
  89. return _trans.get_date_formats()
  90. def get_partial_date_formats():
  91. return _trans.get_partial_date_formats()
  92. def check_for_language(lang_code):
  93. return _trans.check_for_language(lang_code)
  94. def to_locale(language):
  95. return _trans.to_locale(language)
  96. def get_language_from_request(request):
  97. return _trans.get_language_from_request(request)
  98. def templatize(src, origin=None):
  99. return _trans.templatize(src, origin)
  100. def deactivate_all():
  101. return _trans.deactivate_all()
  102. def _string_concat(*strings):
  103. """
  104. Lazy variant of string concatenation, needed for translations that are
  105. constructed from multiple parts.
  106. """
  107. return u''.join([force_unicode(s) for s in strings])
  108. string_concat = lazy(_string_concat, unicode)
  109. def get_language_info(lang_code):
  110. from django.conf.locale import LANG_INFO
  111. try:
  112. return LANG_INFO[lang_code]
  113. except KeyError:
  114. raise KeyError("Unknown language code %r." % lang_code)