django1/django/utils/translation/__init__.py

244 lines
6.8 KiB
Python
Raw Normal View History

"""
Internationalization support.
"""
import re
from contextlib import ContextDecorator
2015-06-16 02:07:31 +08:00
from django.utils.functional import lazy
__all__ = [
'activate', 'deactivate', 'override', 'deactivate_all',
2013-11-03 03:37:48 +08:00
'get_language', 'get_language_from_request',
'get_language_info', 'get_language_bidi',
'check_for_language', 'to_locale', 'templatize',
'gettext', 'gettext_lazy', 'gettext_noop',
'ugettext', 'ugettext_lazy', 'ugettext_noop',
'ngettext', 'ngettext_lazy',
'ungettext', 'ungettext_lazy',
'pgettext', 'pgettext_lazy',
'npgettext', 'npgettext_lazy',
'LANGUAGE_SESSION_KEY',
]
LANGUAGE_SESSION_KEY = '_language'
2014-02-23 01:50:19 +08:00
class TranslatorCommentWarning(SyntaxWarning):
pass
# 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).
class Trans:
"""
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__.
"""
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)
2016-11-13 01:11:23 +08:00
_trans = Trans()
# The Trans class is no more needed, so remove it from the namespace.
del Trans
2013-11-03 07:53:29 +08:00
def gettext_noop(message):
return _trans.gettext_noop(message)
2016-11-13 01:11:23 +08:00
ugettext_noop = gettext_noop
2013-11-03 07:53:29 +08:00
def gettext(message):
return _trans.gettext(message)
2013-11-03 07:53:29 +08:00
# An alias since Django 2.0
ugettext = gettext
2013-11-03 07:53:29 +08:00
def ngettext(singular, plural, number):
return _trans.ngettext(singular, plural, number)
2013-11-03 07:53:29 +08:00
# An alias since Django 2.0
ungettext = ngettext
2013-11-03 07:53:29 +08:00
def pgettext(context, message):
return _trans.pgettext(context, message)
2013-11-03 07:53:29 +08:00
def npgettext(context, singular, plural, number):
return _trans.npgettext(context, singular, plural, number)
2016-11-13 01:11:23 +08:00
gettext_lazy = ugettext_lazy = lazy(gettext, str)
pgettext_lazy = lazy(pgettext, str)
2013-11-03 07:53:29 +08:00
def lazy_number(func, resultclass, number=None, **kwargs):
if isinstance(number, int):
kwargs['number'] = number
proxy = lazy(func, resultclass)(**kwargs)
else:
original_kwargs = kwargs.copy()
class NumberAwareString(resultclass):
def __bool__(self):
return bool(kwargs['singular'])
def __mod__(self, rhs):
if isinstance(rhs, dict) and number:
try:
number_value = rhs[number]
except KeyError:
2016-03-29 06:33:29 +08:00
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
translated = func(**kwargs)
try:
translated = translated % rhs
except TypeError:
# String doesn't contain a placeholder for the number.
pass
return translated
proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs)
proxy.__reduce__ = lambda: (_lazy_number_unpickle, (func, resultclass, number, original_kwargs))
return proxy
2013-11-03 07:53:29 +08:00
def _lazy_number_unpickle(func, resultclass, number, kwargs):
return lazy_number(func, resultclass, number=number, **kwargs)
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
# An alias since Django 2.0
ungettext_lazy = ngettext_lazy
2013-11-03 07:53:29 +08:00
def npgettext_lazy(context, singular, plural, number=None):
return lazy_number(npgettext, str, context=context, singular=singular, plural=plural, number=number)
2013-11-03 07:53:29 +08:00
def activate(language):
return _trans.activate(language)
2013-11-03 07:53:29 +08:00
def deactivate():
return _trans.deactivate()
2013-11-03 07:53:29 +08:00
class override(ContextDecorator):
def __init__(self, language, deactivate=False):
self.language = language
self.deactivate = deactivate
def __enter__(self):
self.old_language = get_language()
if self.language is not None:
activate(self.language)
else:
deactivate_all()
def __exit__(self, exc_type, exc_value, traceback):
if self.old_language is None:
deactivate_all()
elif self.deactivate:
deactivate()
else:
activate(self.old_language)
2013-11-03 07:53:29 +08:00
def get_language():
return _trans.get_language()
2013-11-03 07:53:29 +08:00
def get_language_bidi():
return _trans.get_language_bidi()
2013-11-03 07:53:29 +08:00
def check_for_language(lang_code):
return _trans.check_for_language(lang_code)
2013-11-03 07:53:29 +08:00
def to_locale(language):
return _trans.to_locale(language)
2013-11-03 07:53:29 +08:00
def get_language_from_request(request, check_path=False):
return _trans.get_language_from_request(request, check_path)
2013-11-03 07:53:29 +08:00
def get_language_from_path(path):
return _trans.get_language_from_path(path)
2013-11-03 07:53:29 +08:00
def templatize(src, **kwargs):
from .template import templatize
return templatize(src, **kwargs)
2013-11-03 07:53:29 +08:00
def deactivate_all():
return _trans.deactivate_all()
2013-11-03 07:53:29 +08:00
def get_language_info(lang_code):
from django.conf.locale import LANG_INFO
try:
lang_info = LANG_INFO[lang_code]
if 'fallback' in lang_info and 'name' not in lang_info:
info = get_language_info(lang_info['fallback'][0])
else:
info = lang_info
except KeyError:
if '-' not in lang_code:
raise KeyError("Unknown language code %s." % lang_code)
generic_lang_code = lang_code.split('-')[0]
try:
info = LANG_INFO[generic_lang_code]
except KeyError:
raise KeyError("Unknown language code %s and %s." % (lang_code, generic_lang_code))
if info:
info['name_translated'] = gettext_lazy(info['name'])
return info
2016-11-13 01:11:23 +08:00
trim_whitespace_re = re.compile(r'\s*\n\s*')
def trim_whitespace(s):
return trim_whitespace_re.sub(' ', s.strip())