diff options
| author | Christopher Long <indirecthit@gmail.com> | 2007-06-17 22:18:54 +0000 |
|---|---|---|
| committer | Christopher Long <indirecthit@gmail.com> | 2007-06-17 22:18:54 +0000 |
| commit | ae22b6d403dcf25098c77f0dfcf59ae58b186461 (patch) | |
| tree | c37fc631e99a7e4d909d6b6d236f495003731ea7 /django/utils/translation | |
| parent | 0cf7bc439129c66df8d64601e885f83b256b4f25 (diff) | |
per-object-permissions: Merged to trunk [5486] NOTE: Not fully tested, will be working on this over the next few weeks.
git-svn-id: http://code.djangoproject.com/svn/django/branches/per-object-permissions@5488 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/translation')
| -rw-r--r-- | django/utils/translation/__init__.py | 101 | ||||
| -rw-r--r-- | django/utils/translation/trans_null.py | 15 | ||||
| -rw-r--r-- | django/utils/translation/trans_real.py | 7 |
3 files changed, 110 insertions, 13 deletions
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 276e59f4bd..dbb97af76c 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -1,8 +1,97 @@ -from django.conf import settings +""" +Internationalization support. +""" +from django.utils.functional import lazy -if settings.USE_I18N: - from trans_real import * -else: - from trans_null import * +__all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', + 'ngettext_lazy', 'string_concat', 'activate', 'deactivate', + 'get_language', 'get_language_bidi', 'get_date_formats', + 'get_partial_date_formats', 'check_for_language', 'to_locale', + 'get_language_from_request', 'install', 'templatize'] + +# Here be dragons, so a short explanation of the logic won't hurt: +# We are trying to solve two problems: (1) access settings, in particular +# settings.USE_I18N, as late as possible, so that modules can be imported +# without having to first configure Django, and (2) if some other code creates +# a reference to one of these functions, don't break that reference when we +# replace the functions with their real counterparts (once we do access the +# settings). + +def delayed_loader(*args, **kwargs): + """ + Replace each real_* function with the corresponding function from either + trans_real or trans_null (e.g. real_gettext is replaced with + trans_real.gettext or trans_null.gettext). This function is run once, the + first time any i18n method is called. It replaces all the i18n methods at + once at that time. + """ + import traceback + from django.conf import settings + if settings.USE_I18N: + import trans_real as trans + else: + import trans_null as trans + caller = traceback.extract_stack(limit=2)[0][2] + g = globals() + for name in __all__: + if hasattr(trans, name): + g['real_%s' % name] = getattr(trans, name) + + # Make the originally requested function call on the way out the door. + return g[caller](*args, **kwargs) + +g = globals() +for name in __all__: + g['real_%s' % name] = delayed_loader +del g, delayed_loader + +def gettext_noop(message): + return real_gettext_noop(message) + +def gettext(message): + return real_gettext(message) + + +def ngettext(singular, plural, number): + return real_ngettext(singular, plural, number) + +def string_concat(*strings): + return real_string_concat(*strings) + +ngettext_lazy = lazy(ngettext, str, unicode) +gettext_lazy = lazy(gettext, str, unicode) +string_concat = lazy(string_concat, str, unicode) + +def activate(language): + return real_activate(language) + +def deactivate(): + return real_deactivate() + +def get_language(): + return real_get_language() + +def get_language_bidi(): + return real_get_language_bidi() + +def get_date_formats(): + return real_get_date_formats() + +def get_partial_date_formats(): + return real_get_partial_date_formats() + +def check_for_language(lang_code): + return real_check_for_language(lang_code) + +def to_locale(language): + return real_to_locale(language) + +def get_language_from_request(request): + return real_get_language_from_request(request) + +def install(): + return real_install() + +def templatize(src): + return real_templatize(src) -del settings diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py index 75ad573357..10b07529e3 100644 --- a/django/utils/translation/trans_null.py +++ b/django/utils/translation/trans_null.py @@ -9,7 +9,6 @@ def ngettext(singular, plural, number): return plural ngettext_lazy = ngettext -gettext = gettext_noop = gettext_lazy = _ = lambda x: x string_concat = lambda *strings: ''.join([str(el) for el in strings]) activate = lambda x: None deactivate = install = lambda: None @@ -19,6 +18,20 @@ get_date_formats = lambda: (settings.DATE_FORMAT, settings.DATETIME_FORMAT, sett get_partial_date_formats = lambda: (settings.YEAR_MONTH_FORMAT, settings.MONTH_DAY_FORMAT) check_for_language = lambda x: True +TECHNICAL_ID_MAP = { + "DATE_WITH_TIME_FULL": settings.DATETIME_FORMAT, + "DATE_FORMAT": settings.DATE_FORMAT, + "DATETIME_FORMAT": settings.DATETIME_FORMAT, + "TIME_FORMAT": settings.TIME_FORMAT, + "YEAR_MONTH_FORMAT": settings.YEAR_MONTH_FORMAT, + "MONTH_DAY_FORMAT": settings.MONTH_DAY_FORMAT, +} + +def gettext(message): + return TECHNICAL_ID_MAP.get(message, message) + +gettext_noop = gettext_lazy = _ = gettext + def to_locale(language): p = language.find('-') if p >= 0: diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 94df23a8e9..293b4ef9cd 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -3,7 +3,6 @@ import os, re, sys import gettext as gettext_module from cStringIO import StringIO -from django.utils.functional import lazy try: import threading @@ -200,7 +199,7 @@ def deactivate(): will resolve against the default translation object, again. """ global _active - if _active.has_key(currentThread()): + if currentThread() in _active: del _active[currentThread()] def get_language(): @@ -277,9 +276,6 @@ def ngettext(singular, plural, number): _default = translation(settings.LANGUAGE_CODE) return _default.ngettext(singular, plural, number) -gettext_lazy = lazy(gettext, str) -ngettext_lazy = lazy(ngettext, str) - def check_for_language(lang_code): """ Checks whether there is a global language file for the given language code. @@ -493,4 +489,3 @@ def string_concat(*strings): """ return ''.join([str(el) for el in strings]) -string_concat = lazy(string_concat, str) |
