2007-04-02 18:58:43 +08:00
|
|
|
"""
|
|
|
|
Internationalization support.
|
|
|
|
"""
|
|
|
|
from django.utils.functional import lazy
|
2007-10-03 09:57:02 +08:00
|
|
|
from django.utils.encoding import force_unicode
|
2006-07-04 11:58:45 +08:00
|
|
|
|
2007-04-02 18:58:43 +08:00
|
|
|
__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',
|
2007-10-22 01:14:25 +08:00
|
|
|
'get_language_from_request', 'templatize', 'ugettext',
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'ungettext', 'deactivate_all']
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
# 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.
|
2007-10-03 09:57:02 +08:00
|
|
|
return g['real_%s' % caller](*args, **kwargs)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
ugettext_noop = gettext_noop
|
|
|
|
|
2007-04-02 18:58:43 +08:00
|
|
|
def gettext(message):
|
|
|
|
return real_gettext(message)
|
|
|
|
|
|
|
|
def ngettext(singular, plural, number):
|
|
|
|
return real_ngettext(singular, plural, number)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def ugettext(message):
|
|
|
|
return real_ugettext(message)
|
|
|
|
|
|
|
|
def ungettext(singular, plural, number):
|
|
|
|
return real_ungettext(singular, plural, number)
|
|
|
|
|
|
|
|
ngettext_lazy = lazy(ngettext, str)
|
|
|
|
gettext_lazy = lazy(gettext, str)
|
|
|
|
ungettext_lazy = lazy(ungettext, unicode)
|
|
|
|
ugettext_lazy = lazy(ugettext, unicode)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2007-04-03 11:08:56 +08:00
|
|
|
def templatize(src):
|
|
|
|
return real_templatize(src)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def deactivate_all():
|
|
|
|
return real_deactivate_all()
|
|
|
|
|
2007-10-03 09:57:02 +08:00
|
|
|
def string_concat(*strings):
|
2007-10-04 06:12:22 +08:00
|
|
|
"""
|
2007-10-03 09:57:02 +08:00
|
|
|
Lazy variant of string concatenation, needed for translations that are
|
|
|
|
constructed from multiple parts.
|
|
|
|
"""
|
|
|
|
return u''.join([force_unicode(s) for s in strings])
|
|
|
|
string_concat = lazy(string_concat, unicode)
|