2007-04-02 18:58:43 +08:00
|
|
|
"""
|
|
|
|
Internationalization support.
|
|
|
|
"""
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
2015-06-16 02:07:31 +08:00
|
|
|
|
2013-11-03 03:01:17 +08:00
|
|
|
import re
|
2015-06-16 02:07:31 +08:00
|
|
|
|
|
|
|
from django.utils import six
|
2014-08-20 00:44:10 +08:00
|
|
|
from django.utils.decorators import ContextDecorator
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import force_text
|
2011-02-08 02:48:40 +08:00
|
|
|
from django.utils.functional import lazy
|
2006-07-04 11:58:45 +08:00
|
|
|
|
2011-05-11 02:49:04 +08:00
|
|
|
__all__ = [
|
|
|
|
'activate', 'deactivate', 'override', 'deactivate_all',
|
2013-11-03 03:37:48 +08:00
|
|
|
'get_language', 'get_language_from_request',
|
2011-05-11 02:49:04 +08:00
|
|
|
'get_language_info', 'get_language_bidi',
|
|
|
|
'check_for_language', 'to_locale', 'templatize', 'string_concat',
|
|
|
|
'gettext', 'gettext_lazy', 'gettext_noop',
|
|
|
|
'ugettext', 'ugettext_lazy', 'ugettext_noop',
|
|
|
|
'ngettext', 'ngettext_lazy',
|
|
|
|
'ungettext', 'ungettext_lazy',
|
|
|
|
'pgettext', 'pgettext_lazy',
|
|
|
|
'npgettext', 'npgettext_lazy',
|
2014-05-07 07:47:22 +08:00
|
|
|
'LANGUAGE_SESSION_KEY',
|
2011-05-11 02:49:04 +08:00
|
|
|
]
|
2007-04-02 18:58:43 +08:00
|
|
|
|
2014-02-22 21:27:57 +08:00
|
|
|
LANGUAGE_SESSION_KEY = '_language'
|
2013-01-21 02:07:10 +08:00
|
|
|
|
2014-02-23 01:50:19 +08:00
|
|
|
|
2013-01-21 02:07:10 +08:00
|
|
|
class TranslatorCommentWarning(SyntaxWarning):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
2011-02-08 02:48:40 +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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 07:53:29 +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
|
|
|
|
2013-11-03 07:53:29 +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 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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2010-11-04 18:48:27 +08:00
|
|
|
def pgettext(context, message):
|
|
|
|
return _trans.pgettext(context, message)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2010-11-04 18:48:27 +08:00
|
|
|
def npgettext(context, singular, plural, number):
|
|
|
|
return _trans.npgettext(context, singular, plural, number)
|
|
|
|
|
2012-08-18 17:30:48 +08:00
|
|
|
gettext_lazy = lazy(gettext, str)
|
2012-07-20 20:48:51 +08:00
|
|
|
ugettext_lazy = lazy(ugettext, six.text_type)
|
|
|
|
pgettext_lazy = lazy(pgettext, six.text_type)
|
2013-01-31 03:28:16 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2013-01-31 03:28:16 +08:00
|
|
|
def lazy_number(func, resultclass, number=None, **kwargs):
|
2014-08-31 00:48:55 +08:00
|
|
|
if isinstance(number, six.integer_types):
|
2013-01-31 03:28:16 +08:00
|
|
|
kwargs['number'] = number
|
|
|
|
proxy = lazy(func, resultclass)(**kwargs)
|
|
|
|
else:
|
|
|
|
class NumberAwareString(resultclass):
|
2015-10-20 03:46:55 +08:00
|
|
|
def __bool__(self):
|
|
|
|
return bool(kwargs['singular'])
|
|
|
|
|
|
|
|
def __nonzero__(self): # Python 2 compatibility
|
|
|
|
return type(self).__bool__(self)
|
|
|
|
|
2013-01-31 03:28:16 +08:00
|
|
|
def __mod__(self, rhs):
|
|
|
|
if isinstance(rhs, dict) and number:
|
|
|
|
try:
|
|
|
|
number_value = rhs[number]
|
|
|
|
except KeyError:
|
|
|
|
raise KeyError('Your dictionary lacks key \'%s\'. '
|
|
|
|
'Please provide it, because it is required to '
|
|
|
|
'determine whether string is singular or plural.'
|
|
|
|
% number)
|
|
|
|
else:
|
|
|
|
number_value = rhs
|
|
|
|
kwargs['number'] = number_value
|
2013-02-02 17:56:41 +08:00
|
|
|
translated = func(**kwargs)
|
|
|
|
try:
|
|
|
|
translated = translated % rhs
|
|
|
|
except TypeError:
|
|
|
|
# String doesn't contain a placeholder for the number
|
|
|
|
pass
|
|
|
|
return translated
|
2013-01-31 03:28:16 +08:00
|
|
|
|
|
|
|
proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs)
|
|
|
|
return proxy
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2013-01-31 03:28:16 +08:00
|
|
|
def ngettext_lazy(singular, plural, number=None):
|
|
|
|
return lazy_number(ngettext, str, singular=singular, plural=plural, number=number)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2013-01-31 03:28:16 +08:00
|
|
|
def ungettext_lazy(singular, plural, number=None):
|
|
|
|
return lazy_number(ungettext, six.text_type, singular=singular, plural=plural, number=number)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2013-01-31 03:28:16 +08:00
|
|
|
def npgettext_lazy(context, singular, plural, number=None):
|
|
|
|
return lazy_number(npgettext, six.text_type, context=context, singular=singular, plural=plural, number=number)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2014-08-20 00:44:10 +08:00
|
|
|
class override(ContextDecorator):
|
2011-05-06 21:29:32 +08:00
|
|
|
def __init__(self, language, deactivate=False):
|
|
|
|
self.language = language
|
|
|
|
self.deactivate = deactivate
|
|
|
|
|
|
|
|
def __enter__(self):
|
2014-08-29 04:26:37 +08:00
|
|
|
self.old_language = get_language()
|
2011-09-08 21:24:41 +08:00
|
|
|
if self.language is not None:
|
|
|
|
activate(self.language)
|
|
|
|
else:
|
|
|
|
deactivate_all()
|
2011-05-06 21:29:32 +08:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
2015-08-21 20:09:29 +08:00
|
|
|
if self.old_language is None:
|
|
|
|
deactivate_all()
|
|
|
|
elif self.deactivate:
|
2011-05-06 21:29:32 +08:00
|
|
|
deactivate()
|
|
|
|
else:
|
|
|
|
activate(self.old_language)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2012-02-18 21:37:30 +08:00
|
|
|
def get_language_from_request(request, check_path=False):
|
|
|
|
return _trans.get_language_from_request(request, check_path)
|
2007-04-02 18:58:43 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2013-11-12 14:54:01 +08:00
|
|
|
def get_language_from_path(path):
|
|
|
|
return _trans.get_language_from_path(path)
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2010-12-05 01:42:54 +08:00
|
|
|
def templatize(src, origin=None):
|
|
|
|
return _trans.templatize(src, origin)
|
2007-04-03 11:08:56 +08:00
|
|
|
|
2013-11-03 07:53:29 +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
|
|
|
|
2013-11-03 07:53:29 +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.
|
|
|
|
"""
|
2013-08-30 07:20:00 +08:00
|
|
|
return ''.join(force_text(s) for s in strings)
|
2012-07-20 20:48:51 +08:00
|
|
|
string_concat = lazy(_string_concat, six.text_type)
|
2010-12-13 07:02:45 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2010-12-13 07:02:45 +08:00
|
|
|
def get_language_info(lang_code):
|
|
|
|
from django.conf.locale import LANG_INFO
|
|
|
|
try:
|
2014-07-12 03:01:37 +08:00
|
|
|
lang_info = LANG_INFO[lang_code]
|
|
|
|
if 'fallback' in lang_info and 'name' not in lang_info:
|
2015-05-31 22:20:19 +08:00
|
|
|
info = get_language_info(lang_info['fallback'][0])
|
|
|
|
else:
|
|
|
|
info = lang_info
|
2010-12-13 07:02:45 +08:00
|
|
|
except KeyError:
|
2013-02-24 21:43:45 +08:00
|
|
|
if '-' not in lang_code:
|
|
|
|
raise KeyError("Unknown language code %s." % lang_code)
|
|
|
|
generic_lang_code = lang_code.split('-')[0]
|
|
|
|
try:
|
2015-05-31 22:20:19 +08:00
|
|
|
info = LANG_INFO[generic_lang_code]
|
2013-02-24 21:43:45 +08:00
|
|
|
except KeyError:
|
|
|
|
raise KeyError("Unknown language code %s and %s." % (lang_code, generic_lang_code))
|
2013-11-03 03:01:17 +08:00
|
|
|
|
2015-05-31 22:20:19 +08:00
|
|
|
if info:
|
|
|
|
info['name_translated'] = ugettext_lazy(info['name'])
|
|
|
|
return info
|
|
|
|
|
2013-11-03 03:01:17 +08:00
|
|
|
trim_whitespace_re = re.compile('\s*\n\s*')
|
|
|
|
|
|
|
|
|
|
|
|
def trim_whitespace(s):
|
|
|
|
return trim_whitespace_re.sub(' ', s.strip())
|