2007-04-02 18:58:43 +08:00
|
|
|
"""
|
|
|
|
Internationalization support.
|
|
|
|
"""
|
2017-09-07 20:16:21 +08:00
|
|
|
from contextlib import ContextDecorator
|
2019-06-10 03:48:20 +08:00
|
|
|
from decimal import ROUND_UP, Decimal
|
2015-06-16 02:07:31 +08:00
|
|
|
|
2019-01-14 09:33:47 +08:00
|
|
|
from django.utils.autoreload import autoreload_started, file_changed
|
2011-02-08 02:48:40 +08:00
|
|
|
from django.utils.functional import lazy
|
2019-10-26 22:42:32 +08:00
|
|
|
from django.utils.regex_helper import _lazy_re_compile
|
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',
|
2018-05-13 03:03:38 +08:00
|
|
|
'check_for_language', 'to_language', 'to_locale', 'templatize',
|
2011-05-11 02:49:04 +08:00
|
|
|
'gettext', 'gettext_lazy', 'gettext_noop',
|
|
|
|
'ngettext', 'ngettext_lazy',
|
|
|
|
'pgettext', 'pgettext_lazy',
|
|
|
|
'npgettext', 'npgettext_lazy',
|
|
|
|
]
|
2007-04-02 18:58:43 +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).
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Trans:
|
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
|
2020-07-24 14:25:47 +08:00
|
|
|
from django.utils.translation.reloader import (
|
|
|
|
translation_file_changed, watch_for_translation_changes,
|
|
|
|
)
|
2019-01-14 09:33:47 +08:00
|
|
|
autoreload_started.connect(watch_for_translation_changes, dispatch_uid='translation_file_changed')
|
|
|
|
file_changed.connect(translation_file_changed, dispatch_uid='translation_file_changed')
|
2010-09-27 23:25:38 +08:00
|
|
|
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
|
|
|
|
2016-11-13 01:11:23 +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
|
|
|
|
2016-11-13 01:11:23 +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
|
|
|
|
2017-01-27 03:56:34 +08:00
|
|
|
def ngettext(singular, plural, number):
|
|
|
|
return _trans.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
|
|
|
|
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)
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2019-02-06 11:45:26 +08:00
|
|
|
gettext_lazy = lazy(gettext, str)
|
2016-12-29 23:27:49 +08:00
|
|
|
pgettext_lazy = lazy(pgettext, str)
|
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):
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(number, int):
|
2013-01-31 03:28:16 +08:00
|
|
|
kwargs['number'] = number
|
|
|
|
proxy = lazy(func, resultclass)(**kwargs)
|
|
|
|
else:
|
2016-02-12 09:12:54 +08:00
|
|
|
original_kwargs = kwargs.copy()
|
|
|
|
|
2013-01-31 03:28:16 +08:00
|
|
|
class NumberAwareString(resultclass):
|
2015-10-20 03:46:55 +08:00
|
|
|
def __bool__(self):
|
|
|
|
return bool(kwargs['singular'])
|
|
|
|
|
2018-11-26 05:50:34 +08:00
|
|
|
def _get_number_value(self, values):
|
|
|
|
try:
|
|
|
|
return values[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
|
|
|
|
)
|
|
|
|
|
|
|
|
def _translate(self, number_value):
|
|
|
|
kwargs['number'] = number_value
|
|
|
|
return func(**kwargs)
|
|
|
|
|
|
|
|
def format(self, *args, **kwargs):
|
|
|
|
number_value = self._get_number_value(kwargs) if kwargs and number else args[0]
|
|
|
|
return self._translate(number_value).format(*args, **kwargs)
|
|
|
|
|
2013-01-31 03:28:16 +08:00
|
|
|
def __mod__(self, rhs):
|
|
|
|
if isinstance(rhs, dict) and number:
|
2018-11-26 05:50:34 +08:00
|
|
|
number_value = self._get_number_value(rhs)
|
2013-01-31 03:28:16 +08:00
|
|
|
else:
|
|
|
|
number_value = rhs
|
2018-11-26 05:50:34 +08:00
|
|
|
translated = self._translate(number_value)
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2013-02-02 17:56:41 +08:00
|
|
|
translated = translated % rhs
|
2017-09-07 20:16:21 +08:00
|
|
|
except TypeError:
|
|
|
|
# String doesn't contain a placeholder for the number.
|
|
|
|
pass
|
2013-02-02 17:56:41 +08:00
|
|
|
return translated
|
2013-01-31 03:28:16 +08:00
|
|
|
|
|
|
|
proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs)
|
2016-02-12 09:12:54 +08:00
|
|
|
proxy.__reduce__ = lambda: (_lazy_number_unpickle, (func, resultclass, number, original_kwargs))
|
2013-01-31 03:28:16 +08:00
|
|
|
return proxy
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2016-02-12 09:12:54 +08:00
|
|
|
def _lazy_number_unpickle(func, resultclass, number, kwargs):
|
|
|
|
return lazy_number(func, resultclass, number=number, **kwargs)
|
|
|
|
|
|
|
|
|
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 npgettext_lazy(context, singular, plural, number=None):
|
2016-12-29 23:27:49 +08:00
|
|
|
return lazy_number(npgettext, str, 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
|
|
|
|
2018-05-13 03:03:38 +08:00
|
|
|
def to_language(locale):
|
|
|
|
"""Turn a locale name (en_US) into a language name (en-us)."""
|
|
|
|
p = locale.find('_')
|
|
|
|
if p >= 0:
|
|
|
|
return locale[:p].lower() + '-' + locale[p + 1:].lower()
|
|
|
|
else:
|
|
|
|
return locale.lower()
|
|
|
|
|
|
|
|
|
2007-04-02 18:58:43 +08:00
|
|
|
def to_locale(language):
|
2018-05-13 00:38:53 +08:00
|
|
|
"""Turn a language name (en-us) into a locale name (en_US)."""
|
2021-03-22 02:03:57 +08:00
|
|
|
lang, _, country = language.lower().partition('-')
|
2018-11-22 22:47:01 +08:00
|
|
|
if not country:
|
2021-03-22 02:03:57 +08:00
|
|
|
return language[:3].lower() + language[3:]
|
2018-05-13 00:38:53 +08:00
|
|
|
# A language with > 2 characters after the dash only has its first
|
|
|
|
# character after the dash capitalized; e.g. sr-latn becomes sr_Latn.
|
|
|
|
# A language with 2 characters after the dash has both characters
|
|
|
|
# capitalized; e.g. en-us becomes en_US.
|
2018-11-22 22:47:01 +08:00
|
|
|
country, _, tail = country.partition('-')
|
|
|
|
country = country.title() if len(country) > 2 else country.upper()
|
|
|
|
if tail:
|
|
|
|
country += '-' + tail
|
2021-03-22 02:03:57 +08:00
|
|
|
return lang + '_' + country
|
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
|
|
|
|
2017-06-28 07:50:26 +08:00
|
|
|
def get_supported_language_variant(lang_code, *, strict=False):
|
|
|
|
return _trans.get_supported_language_variant(lang_code, strict)
|
|
|
|
|
|
|
|
|
2016-09-29 22:13:10 +08:00
|
|
|
def templatize(src, **kwargs):
|
2016-08-08 21:46:52 +08:00
|
|
|
from .template import templatize
|
2016-09-29 22:13:10 +08:00
|
|
|
return templatize(src, **kwargs)
|
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-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:
|
2017-01-27 03:58:33 +08:00
|
|
|
info['name_translated'] = gettext_lazy(info['name'])
|
2015-05-31 22:20:19 +08:00
|
|
|
return info
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2019-10-26 22:42:32 +08:00
|
|
|
trim_whitespace_re = _lazy_re_compile(r'\s*\n\s*')
|
2013-11-03 03:01:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
def trim_whitespace(s):
|
|
|
|
return trim_whitespace_re.sub(' ', s.strip())
|
2019-06-10 03:48:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
def round_away_from_one(value):
|
|
|
|
return int(Decimal(value - 1).quantize(Decimal('0'), rounding=ROUND_UP)) + 1
|