2007-04-02 18:58:43 +08:00
|
|
|
"""
|
|
|
|
Internationalization support.
|
|
|
|
"""
|
2007-10-03 09:57:02 +08:00
|
|
|
from django.utils.encoding import force_unicode
|
2010-03-27 23:54:31 +08:00
|
|
|
from django.utils.functional import lazy, curry
|
|
|
|
|
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',
|
2008-08-29 04:20:40 +08:00
|
|
|
'get_language_from_request', 'templatize', 'ugettext', 'ugettext_lazy',
|
2010-11-04 18:48:27 +08:00
|
|
|
'ungettext', 'ungettext_lazy', 'pgettext', 'pgettext_lazy',
|
|
|
|
'npgettext', 'npgettext_lazy', '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).
|
|
|
|
|
2010-09-27 23:25:38 +08:00
|
|
|
class Trans(object):
|
2007-04-02 18:58:43 +08:00
|
|
|
"""
|
2010-09-27 23:25:38 +08:00
|
|
|
The purpose of this class is to store the actual translation function upon
|
|
|
|
receiving the first call to that function. After this is done, changes to
|
|
|
|
USE_I18N will have no effect to which function is served upon request. If
|
|
|
|
your tests rely on changing USE_I18N, you can delete all the functions
|
|
|
|
from _trans.__dict__.
|
|
|
|
|
|
|
|
Note that storing the function with setattr will have a noticeable
|
|
|
|
performance effect, as access to the function goes the normal path,
|
|
|
|
instead of using __getattr__.
|
2007-04-02 18:58:43 +08:00
|
|
|
"""
|
2010-09-27 23:25:38 +08:00
|
|
|
def __getattr__(self, real_name):
|
|
|
|
from django.conf import settings
|
|
|
|
if settings.USE_I18N:
|
|
|
|
from django.utils.translation import trans_real as trans
|
|
|
|
else:
|
|
|
|
from django.utils.translation import trans_null as trans
|
|
|
|
setattr(self, real_name, getattr(trans, real_name))
|
|
|
|
return getattr(trans, real_name)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
2010-09-27 23:25:38 +08:00
|
|
|
_trans = Trans()
|
2007-04-02 18:58:43 +08:00
|
|
|
|
2010-09-27 23:25:38 +08:00
|
|
|
# The Trans class is no more needed, so remove it from the namespace.
|
|
|
|
del Trans
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def gettext_noop(message):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.gettext_noop(message)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
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):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.gettext(message)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def ngettext(singular, plural, number):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.ngettext(singular, plural, number)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
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):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.ugettext(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
|
|
|
|
|
|
|
def ungettext(singular, plural, number):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.ungettext(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
|
|
|
|
2010-11-04 18:48:27 +08:00
|
|
|
def pgettext(context, message):
|
|
|
|
return _trans.pgettext(context, message)
|
|
|
|
|
|
|
|
def npgettext(context, singular, plural, number):
|
|
|
|
return _trans.npgettext(context, 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
|
|
|
ngettext_lazy = lazy(ngettext, str)
|
|
|
|
gettext_lazy = lazy(gettext, str)
|
|
|
|
ungettext_lazy = lazy(ungettext, unicode)
|
|
|
|
ugettext_lazy = lazy(ugettext, unicode)
|
2010-11-04 18:48:27 +08:00
|
|
|
pgettext_lazy = lazy(pgettext, unicode)
|
|
|
|
npgettext_lazy = lazy(npgettext, unicode)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def activate(language):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.activate(language)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def deactivate():
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.deactivate()
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def get_language():
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.get_language()
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def get_language_bidi():
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.get_language_bidi()
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def get_date_formats():
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.get_date_formats()
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def get_partial_date_formats():
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.get_partial_date_formats()
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def check_for_language(lang_code):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.check_for_language(lang_code)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def to_locale(language):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.to_locale(language)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
|
|
|
def get_language_from_request(request):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.get_language_from_request(request)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
2007-04-03 11:08:56 +08:00
|
|
|
def templatize(src):
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.templatize(src)
|
2007-04-03 11:08:56 +08:00
|
|
|
|
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():
|
2010-09-27 23:25:38 +08:00
|
|
|
return _trans.deactivate_all()
|
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
|
|
|
|
2010-03-27 23:54:31 +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])
|
2010-03-27 23:54:31 +08:00
|
|
|
string_concat = lazy(_string_concat, unicode)
|