2008-03-08 11:55:47 +08:00
|
|
|
"""Translation helper functions."""
|
2017-01-19 04:30:21 +08:00
|
|
|
import functools
|
2015-01-28 20:35:27 +08:00
|
|
|
import gettext as gettext_module
|
2007-10-27 03:52:42 +08:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
2013-01-21 02:07:10 +08:00
|
|
|
import warnings
|
2019-04-12 21:15:18 +08:00
|
|
|
|
|
|
|
from asgiref.local import Local
|
2007-10-27 03:52:42 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2014-05-01 00:04:30 +08:00
|
|
|
from django.conf import settings
|
2014-07-12 03:01:37 +08:00
|
|
|
from django.conf.locale import LANG_INFO
|
2014-06-21 21:00:35 +08:00
|
|
|
from django.core.exceptions import AppRegistryNotReady
|
2014-10-09 23:46:40 +08:00
|
|
|
from django.core.signals import setting_changed
|
2013-10-28 23:57:56 +08:00
|
|
|
from django.dispatch import receiver
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.safestring import SafeData, mark_safe
|
2018-05-13 00:38:53 +08:00
|
|
|
|
2016-08-15 04:42:49 +08:00
|
|
|
from . import to_language, to_locale
|
2011-01-17 17:52:47 +08:00
|
|
|
|
2014-03-18 01:06:11 +08:00
|
|
|
# Translations are cached in a dictionary for every language.
|
2005-11-07 05:55:39 +08:00
|
|
|
# The active translations are stored by threadid to make them thread local.
|
2005-11-04 12:59:46 +08:00
|
|
|
_translations = {}
|
2019-04-12 21:15:18 +08:00
|
|
|
_active = Local()
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2005-11-07 05:55:39 +08:00
|
|
|
# The default translation is based on the settings file.
|
2005-11-04 12:59:46 +08:00
|
|
|
_default = None
|
|
|
|
|
2010-11-04 18:48:27 +08:00
|
|
|
# magic gettext number to separate context from message
|
2012-06-08 00:08:47 +08:00
|
|
|
CONTEXT_SEPARATOR = "\x04"
|
2010-11-04 18:48:27 +08:00
|
|
|
|
2012-10-21 07:25:35 +08:00
|
|
|
# Format of Accept-Language header values. From RFC 2616, section 14.4 and 3.9
|
|
|
|
# and RFC 3066, section 2.1
|
2007-10-27 03:52:42 +08:00
|
|
|
accept_language_re = re.compile(r'''
|
2012-10-21 07:25:35 +08:00
|
|
|
([A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*|\*) # "en", "en-au", "x-y-z", "es-419", "*"
|
2016-06-12 09:32:56 +08:00
|
|
|
(?:\s*;\s*q=(0(?:\.\d{,3})?|1(?:\.0{,3})?))? # Optional "q=1.00", "q=0.8"
|
2011-06-27 00:51:54 +08:00
|
|
|
(?:\s*,\s*|$) # Multiple accepts per header.
|
2007-10-27 03:52:42 +08:00
|
|
|
''', re.VERBOSE)
|
|
|
|
|
2014-12-30 22:41:31 +08:00
|
|
|
language_code_re = re.compile(
|
|
|
|
r'^[a-z]{1,8}(?:-[a-z0-9]{1,8})*(?:@[a-z0-9]{1,20})?$',
|
|
|
|
re.IGNORECASE
|
|
|
|
)
|
2013-11-20 23:31:53 +08:00
|
|
|
|
2016-10-28 17:36:53 +08:00
|
|
|
language_code_prefix_re = re.compile(r'^/(\w+([@-]\w+)?)(/|$)')
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2013-01-21 02:07:10 +08:00
|
|
|
|
2013-10-28 23:57:56 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def reset_cache(**kwargs):
|
|
|
|
"""
|
|
|
|
Reset global state when LANGUAGES setting has been changed, as some
|
|
|
|
languages should no longer be accepted.
|
|
|
|
"""
|
2013-11-12 14:54:01 +08:00
|
|
|
if kwargs['setting'] in ('LANGUAGES', 'LANGUAGE_CODE'):
|
|
|
|
check_for_language.cache_clear()
|
2014-11-19 04:53:54 +08:00
|
|
|
get_languages.cache_clear()
|
2013-11-12 14:54:01 +08:00
|
|
|
get_supported_language_variant.cache_clear()
|
2013-10-28 23:57:56 +08:00
|
|
|
|
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
class DjangoTranslation(gettext_module.GNUTranslations):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Set up the GNUTranslations context with regard to output charset.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2014-05-01 00:04:30 +08:00
|
|
|
This translation object will be constructed out of multiple GNUTranslations
|
|
|
|
objects by merging their catalogs. It will construct an object for the
|
|
|
|
requested language and add a fallback to the default language, if it's
|
|
|
|
different from the requested language.
|
|
|
|
"""
|
2016-03-06 08:01:39 +08:00
|
|
|
domain = 'django'
|
|
|
|
|
|
|
|
def __init__(self, language, domain=None, localedirs=None):
|
2014-05-01 00:04:30 +08:00
|
|
|
"""Create a GNUTranslations() using many locale directories"""
|
|
|
|
gettext_module.GNUTranslations.__init__(self)
|
2016-03-06 08:01:39 +08:00
|
|
|
if domain is not None:
|
|
|
|
self.domain = domain
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
self.__language = language
|
2010-09-27 23:25:38 +08:00
|
|
|
self.__to_language = to_language(language)
|
2014-05-01 00:04:30 +08:00
|
|
|
self.__locale = to_locale(language)
|
2015-12-19 00:00:53 +08:00
|
|
|
self._catalog = None
|
2016-06-07 21:02:42 +08:00
|
|
|
# If a language doesn't have a catalog, use the Germanic default for
|
|
|
|
# pluralization: anything except one is pluralized.
|
|
|
|
self.plural = lambda n: int(n != 1)
|
2014-05-01 00:04:30 +08:00
|
|
|
|
2016-03-06 08:01:39 +08:00
|
|
|
if self.domain == 'django':
|
|
|
|
if localedirs is not None:
|
|
|
|
# A module-level cache is used for caching 'django' translations
|
|
|
|
warnings.warn("localedirs is ignored when domain is 'django'.", RuntimeWarning)
|
|
|
|
localedirs = None
|
|
|
|
self._init_translation_catalog()
|
|
|
|
|
|
|
|
if localedirs:
|
|
|
|
for localedir in localedirs:
|
|
|
|
translation = self._new_gnu_trans(localedir)
|
|
|
|
self.merge(translation)
|
|
|
|
else:
|
|
|
|
self._add_installed_apps_translations()
|
|
|
|
|
2014-05-01 00:04:30 +08:00
|
|
|
self._add_local_translations()
|
2016-04-04 08:37:32 +08:00
|
|
|
if self.__language == settings.LANGUAGE_CODE and self.domain == 'django' and self._catalog is None:
|
2015-12-19 00:00:53 +08:00
|
|
|
# default lang should have at least one translation file available.
|
2019-01-28 23:01:35 +08:00
|
|
|
raise OSError('No translation files found for default language %s.' % settings.LANGUAGE_CODE)
|
2016-03-06 08:01:39 +08:00
|
|
|
self._add_fallback(localedirs)
|
2016-01-07 01:33:29 +08:00
|
|
|
if self._catalog is None:
|
|
|
|
# No catalogs found for this language, set an empty catalog.
|
|
|
|
self._catalog = {}
|
2014-05-01 00:04:30 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<DjangoTranslation lang:%s>" % self.__language
|
|
|
|
|
|
|
|
def _new_gnu_trans(self, localedir, use_null_fallback=True):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Return a mergeable gettext.GNUTranslations instance.
|
2014-05-01 00:04:30 +08:00
|
|
|
|
|
|
|
A convenience wrapper. By default gettext uses 'fallback=False'.
|
|
|
|
Using param `use_null_fallback` to avoid confusion with any other
|
|
|
|
references to 'fallback'.
|
|
|
|
"""
|
2015-12-19 00:00:53 +08:00
|
|
|
return gettext_module.translation(
|
2016-03-06 08:01:39 +08:00
|
|
|
domain=self.domain,
|
2014-05-01 00:04:30 +08:00
|
|
|
localedir=localedir,
|
|
|
|
languages=[self.__locale],
|
2018-03-16 17:54:34 +08:00
|
|
|
fallback=use_null_fallback,
|
|
|
|
)
|
2014-05-01 00:04:30 +08:00
|
|
|
|
|
|
|
def _init_translation_catalog(self):
|
2017-01-25 04:32:33 +08:00
|
|
|
"""Create a base catalog using global django translations."""
|
2017-01-20 21:01:02 +08:00
|
|
|
settingsfile = sys.modules[settings.__module__].__file__
|
2014-05-01 00:04:30 +08:00
|
|
|
localedir = os.path.join(os.path.dirname(settingsfile), 'locale')
|
2015-12-19 00:00:53 +08:00
|
|
|
translation = self._new_gnu_trans(localedir)
|
|
|
|
self.merge(translation)
|
2014-05-01 00:04:30 +08:00
|
|
|
|
|
|
|
def _add_installed_apps_translations(self):
|
2017-01-25 04:32:33 +08:00
|
|
|
"""Merge translations from each installed app."""
|
2014-06-21 21:00:35 +08:00
|
|
|
try:
|
|
|
|
app_configs = reversed(list(apps.get_app_configs()))
|
|
|
|
except AppRegistryNotReady:
|
|
|
|
raise AppRegistryNotReady(
|
|
|
|
"The translation infrastructure cannot be initialized before the "
|
|
|
|
"apps registry is ready. Check that you don't make non-lazy "
|
|
|
|
"gettext calls at import time.")
|
|
|
|
for app_config in app_configs:
|
2014-05-01 00:04:30 +08:00
|
|
|
localedir = os.path.join(app_config.path, 'locale')
|
2016-11-07 16:59:13 +08:00
|
|
|
if os.path.exists(localedir):
|
|
|
|
translation = self._new_gnu_trans(localedir)
|
|
|
|
self.merge(translation)
|
2014-05-01 00:04:30 +08:00
|
|
|
|
|
|
|
def _add_local_translations(self):
|
2017-01-25 04:32:33 +08:00
|
|
|
"""Merge translations defined in LOCALE_PATHS."""
|
2014-05-01 00:04:30 +08:00
|
|
|
for localedir in reversed(settings.LOCALE_PATHS):
|
|
|
|
translation = self._new_gnu_trans(localedir)
|
|
|
|
self.merge(translation)
|
|
|
|
|
2016-03-06 08:01:39 +08:00
|
|
|
def _add_fallback(self, localedirs=None):
|
2017-01-25 04:32:33 +08:00
|
|
|
"""Set the GNUTranslations() fallback with the default language."""
|
2015-02-28 03:17:04 +08:00
|
|
|
# Don't set a fallback for the default language or any English variant
|
|
|
|
# (as it's empty, so it'll ALWAYS fall back to the default language)
|
|
|
|
if self.__language == settings.LANGUAGE_CODE or self.__language.startswith('en'):
|
2014-05-01 00:04:30 +08:00
|
|
|
return
|
2016-03-06 08:01:39 +08:00
|
|
|
if self.domain == 'django':
|
|
|
|
# Get from cache
|
|
|
|
default_translation = translation(settings.LANGUAGE_CODE)
|
|
|
|
else:
|
|
|
|
default_translation = DjangoTranslation(
|
|
|
|
settings.LANGUAGE_CODE, domain=self.domain, localedirs=localedirs
|
|
|
|
)
|
2014-05-01 00:04:30 +08:00
|
|
|
self.add_fallback(default_translation)
|
|
|
|
|
|
|
|
def merge(self, other):
|
|
|
|
"""Merge another translation into this catalog."""
|
2015-12-19 00:00:53 +08:00
|
|
|
if not getattr(other, '_catalog', None):
|
|
|
|
return # NullTranslations() has no _catalog
|
|
|
|
if self._catalog is None:
|
|
|
|
# Take plural and _info from first catalog found (generally Django's).
|
|
|
|
self.plural = other.plural
|
|
|
|
self._info = other._info.copy()
|
|
|
|
self._catalog = other._catalog.copy()
|
|
|
|
else:
|
|
|
|
self._catalog.update(other._catalog)
|
2018-02-20 02:09:33 +08:00
|
|
|
if other._fallback:
|
|
|
|
self.add_fallback(other._fallback)
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
def language(self):
|
2017-01-25 04:32:33 +08:00
|
|
|
"""Return the translation language."""
|
2005-11-04 12:59:46 +08:00
|
|
|
return self.__language
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2010-09-27 23:25:38 +08:00
|
|
|
def to_language(self):
|
2017-01-25 04:32:33 +08:00
|
|
|
"""Return the translation language name."""
|
2010-09-27 23:25:38 +08:00
|
|
|
return self.__to_language
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def translation(language):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Return a translation object in the default 'django' domain.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
global _translations
|
2014-05-01 06:33:46 +08:00
|
|
|
if language not in _translations:
|
2014-05-01 00:04:30 +08:00
|
|
|
_translations[language] = DjangoTranslation(language)
|
|
|
|
return _translations[language]
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def activate(language):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Fetch the translation object for a given language and install it as the
|
2014-03-18 01:06:11 +08:00
|
|
|
current translation object for the current thread.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2015-01-08 18:11:35 +08:00
|
|
|
if not language:
|
|
|
|
return
|
2011-01-17 17:52:47 +08:00
|
|
|
_active.value = translation(language)
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def deactivate():
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Uninstall the active translation object so that further _() calls resolve
|
|
|
|
to the default translation object.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2011-01-17 17:52:47 +08:00
|
|
|
if hasattr(_active, "value"):
|
|
|
|
del _active.value
|
2005-11-04 12:59:46 +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():
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Make the active translation object a NullTranslations() instance. This is
|
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
|
|
|
useful when we want delayed translations to appear as the original string
|
|
|
|
for some reason.
|
|
|
|
"""
|
2011-01-17 17:52:47 +08:00
|
|
|
_active.value = gettext_module.NullTranslations()
|
2015-01-08 18:11:35 +08:00
|
|
|
_active.value.to_language = lambda *args: None
|
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
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def get_language():
|
2017-01-25 04:32:33 +08:00
|
|
|
"""Return the currently selected language."""
|
2011-01-17 17:52:47 +08:00
|
|
|
t = getattr(_active, "value", None)
|
2005-11-04 12:59:46 +08:00
|
|
|
if t is not None:
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2010-09-27 23:25:38 +08:00
|
|
|
return t.to_language()
|
2017-09-07 20:16:21 +08:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2005-11-07 05:55:39 +08:00
|
|
|
# If we don't have a real translation object, assume it's the default language.
|
2006-05-02 09:31:56 +08:00
|
|
|
return settings.LANGUAGE_CODE
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2006-05-16 15:35:20 +08:00
|
|
|
def get_language_bidi():
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Return selected language's BiDi layout.
|
2010-10-11 20:20:07 +08:00
|
|
|
|
2010-05-09 05:38:27 +08:00
|
|
|
* False = left-to-right layout
|
|
|
|
* True = right-to-left layout
|
2006-05-16 15:35:20 +08:00
|
|
|
"""
|
2015-04-03 18:11:54 +08:00
|
|
|
lang = get_language()
|
|
|
|
if lang is None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
base_lang = get_language().split('-')[0]
|
|
|
|
return base_lang in settings.LANGUAGES_BIDI
|
2006-06-02 12:20:32 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2005-12-04 20:06:16 +08:00
|
|
|
def catalog():
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Return the current active catalog for further processing.
|
2005-12-04 20:06:16 +08:00
|
|
|
This can be used if you need to modify the catalog or want to access the
|
|
|
|
whole message catalog instead of just translating one string.
|
|
|
|
"""
|
2011-01-17 17:52:47 +08:00
|
|
|
global _default
|
|
|
|
|
|
|
|
t = getattr(_active, "value", None)
|
2005-12-04 20:06:16 +08:00
|
|
|
if t is not None:
|
|
|
|
return t
|
|
|
|
if _default is None:
|
|
|
|
_default = translation(settings.LANGUAGE_CODE)
|
|
|
|
return _default
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2017-01-27 03:56:34 +08:00
|
|
|
def gettext(message):
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2017-01-27 03:56:34 +08:00
|
|
|
Translate the 'message' string. It uses the current thread to find the
|
2007-10-22 01:14:25 +08:00
|
|
|
translation object to use. If no current translation is activated, the
|
|
|
|
message will be run through the default translation object.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2011-01-17 17:52:47 +08:00
|
|
|
global _default
|
|
|
|
|
2017-01-20 17:20:53 +08:00
|
|
|
eol_message = message.replace('\r\n', '\n').replace('\r', '\n')
|
2014-09-07 19:56:34 +08:00
|
|
|
|
2017-11-30 00:54:34 +08:00
|
|
|
if eol_message:
|
2014-09-07 19:56:34 +08:00
|
|
|
_default = _default or translation(settings.LANGUAGE_CODE)
|
|
|
|
translation_object = getattr(_active, "value", _default)
|
|
|
|
|
2017-01-27 03:56:34 +08:00
|
|
|
result = translation_object.gettext(eol_message)
|
2017-11-30 00:54:34 +08:00
|
|
|
else:
|
|
|
|
# Return an empty value of the corresponding type if an empty message
|
|
|
|
# is given, instead of metadata, which is the default gettext behavior.
|
|
|
|
result = type(message)('')
|
2014-09-07 19:56:34 +08:00
|
|
|
|
2007-11-17 20:11:54 +08:00
|
|
|
if isinstance(message, SafeData):
|
|
|
|
return mark_safe(result)
|
2014-09-07 19:56:34 +08:00
|
|
|
|
2007-11-17 20:11:54 +08:00
|
|
|
return result
|
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):
|
2012-10-15 16:00:22 +08:00
|
|
|
msg_with_ctxt = "%s%s%s" % (context, CONTEXT_SEPARATOR, message)
|
2017-01-27 03:56:34 +08:00
|
|
|
result = gettext(msg_with_ctxt)
|
2010-11-04 18:48:27 +08:00
|
|
|
if CONTEXT_SEPARATOR in result:
|
|
|
|
# Translation not found
|
2017-03-04 22:47:49 +08:00
|
|
|
result = message
|
2017-06-15 14:47:07 +08:00
|
|
|
elif isinstance(message, SafeData):
|
|
|
|
result = mark_safe(result)
|
2010-11-04 18:48:27 +08:00
|
|
|
return result
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def gettext_noop(message):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Mark strings for translation but don't translate them now. This can be
|
2005-11-07 05:55:39 +08:00
|
|
|
used to store strings in global variables that should stay in the base
|
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
|
|
|
language (because they might be used externally) and will be translated
|
|
|
|
later.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
return message
|
|
|
|
|
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 do_ntranslate(singular, plural, number, translation_function):
|
2011-01-17 17:52:47 +08:00
|
|
|
global _default
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2011-01-17 17:52:47 +08:00
|
|
|
t = getattr(_active, "value", None)
|
2005-11-04 12:59:46 +08:00
|
|
|
if t is not None:
|
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
|
|
|
return getattr(t, translation_function)(singular, plural, number)
|
2005-11-04 12:59:46 +08:00
|
|
|
if _default is None:
|
2005-11-16 20:13:28 +08:00
|
|
|
_default = translation(settings.LANGUAGE_CODE)
|
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
|
|
|
return getattr(_default, translation_function)(singular, plural, number)
|
|
|
|
|
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 ngettext(singular, plural, number):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Return a string of the translation of either the singular or plural,
|
2012-08-18 17:30:48 +08:00
|
|
|
based on the 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
|
|
|
"""
|
|
|
|
return do_ntranslate(singular, plural, number, 'ngettext')
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2010-11-04 18:48:27 +08:00
|
|
|
def npgettext(context, singular, plural, number):
|
2012-10-15 16:00:22 +08:00
|
|
|
msgs_with_ctxt = ("%s%s%s" % (context, CONTEXT_SEPARATOR, singular),
|
|
|
|
"%s%s%s" % (context, CONTEXT_SEPARATOR, plural),
|
|
|
|
number)
|
2017-01-27 03:56:34 +08:00
|
|
|
result = ngettext(*msgs_with_ctxt)
|
2010-11-04 18:48:27 +08:00
|
|
|
if CONTEXT_SEPARATOR in result:
|
|
|
|
# Translation not found
|
2017-01-27 03:56:34 +08:00
|
|
|
result = ngettext(singular, plural, number)
|
2010-11-04 18:48:27 +08:00
|
|
|
return result
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2011-02-13 03:12:28 +08:00
|
|
|
def all_locale_paths():
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Return a list of paths to user-provides languages files.
|
2011-02-13 03:12:28 +08:00
|
|
|
"""
|
|
|
|
globalpath = os.path.join(
|
2017-01-20 21:01:02 +08:00
|
|
|
os.path.dirname(sys.modules[settings.__module__].__file__), 'locale')
|
2018-02-18 23:35:06 +08:00
|
|
|
app_paths = []
|
|
|
|
for app_config in apps.get_app_configs():
|
|
|
|
locale_path = os.path.join(app_config.path, 'locale')
|
|
|
|
if os.path.exists(locale_path):
|
|
|
|
app_paths.append(locale_path)
|
2018-09-28 21:57:12 +08:00
|
|
|
return [globalpath, *settings.LOCALE_PATHS, *app_paths]
|
2011-02-13 03:12:28 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2017-01-19 04:30:21 +08:00
|
|
|
@functools.lru_cache(maxsize=1000)
|
2005-11-04 12:59:46 +08:00
|
|
|
def check_for_language(lang_code):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Check whether there is a global language file for the given language
|
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
|
|
|
code. This is used to decide whether a user-provided language is
|
2013-11-20 23:31:53 +08:00
|
|
|
available.
|
2014-03-30 02:44:11 +08:00
|
|
|
|
|
|
|
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
|
|
|
|
as the provided language codes are taken from the HTTP request. See also
|
|
|
|
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2013-11-20 23:31:53 +08:00
|
|
|
# First, a quick check to make sure lang_code is well-formed (#21458)
|
2015-04-03 18:11:54 +08:00
|
|
|
if lang_code is None or not language_code_re.search(lang_code):
|
2013-11-20 23:31:53 +08:00
|
|
|
return False
|
2017-12-27 02:44:12 +08:00
|
|
|
return any(
|
|
|
|
gettext_module.find('django', path, [to_locale(lang_code)]) is not None
|
|
|
|
for path in all_locale_paths()
|
|
|
|
)
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2017-01-19 04:30:21 +08:00
|
|
|
@functools.lru_cache()
|
2014-11-19 04:53:54 +08:00
|
|
|
def get_languages():
|
|
|
|
"""
|
2019-02-05 19:22:08 +08:00
|
|
|
Cache of settings.LANGUAGES in a dictionary for easy lookups by key.
|
2014-11-19 04:53:54 +08:00
|
|
|
"""
|
2019-02-05 19:22:08 +08:00
|
|
|
return dict(settings.LANGUAGES)
|
2014-11-19 04:53:54 +08:00
|
|
|
|
|
|
|
|
2017-01-19 04:30:21 +08:00
|
|
|
@functools.lru_cache(maxsize=1000)
|
2013-11-12 14:54:01 +08:00
|
|
|
def get_supported_language_variant(lang_code, strict=False):
|
2013-02-24 21:43:45 +08:00
|
|
|
"""
|
2018-02-21 22:30:56 +08:00
|
|
|
Return the language code that's listed in supported languages, possibly
|
2017-01-25 04:32:33 +08:00
|
|
|
selecting a more generic variant. Raise LookupError if nothing is found.
|
2013-05-19 18:43:34 +08:00
|
|
|
|
2018-02-21 22:30:56 +08:00
|
|
|
If `strict` is False (the default), look for a country-specific variant
|
|
|
|
when neither the language code nor its generic variant is found.
|
2013-11-12 14:54:01 +08:00
|
|
|
|
|
|
|
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
|
|
|
|
as the provided language codes are taken from the HTTP request. See also
|
|
|
|
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
|
2013-02-24 21:43:45 +08:00
|
|
|
"""
|
2013-05-18 20:37:04 +08:00
|
|
|
if lang_code:
|
2014-07-12 03:01:37 +08:00
|
|
|
# If 'fr-ca' is not supported, try special fallback or language-only 'fr'.
|
|
|
|
possible_lang_codes = [lang_code]
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2014-07-12 03:01:37 +08:00
|
|
|
possible_lang_codes.extend(LANG_INFO[lang_code]['fallback'])
|
2017-09-07 20:16:21 +08:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2013-05-19 18:43:34 +08:00
|
|
|
generic_lang_code = lang_code.split('-')[0]
|
2014-07-12 03:01:37 +08:00
|
|
|
possible_lang_codes.append(generic_lang_code)
|
2014-11-19 04:53:54 +08:00
|
|
|
supported_lang_codes = get_languages()
|
2014-07-12 03:01:37 +08:00
|
|
|
|
|
|
|
for code in possible_lang_codes:
|
2014-11-19 04:53:54 +08:00
|
|
|
if code in supported_lang_codes and check_for_language(code):
|
2013-05-18 20:37:04 +08:00
|
|
|
return code
|
2013-05-19 18:43:34 +08:00
|
|
|
if not strict:
|
|
|
|
# if fr-fr is not supported, try fr-ca.
|
2014-11-19 04:53:54 +08:00
|
|
|
for supported_code in supported_lang_codes:
|
2013-11-12 14:54:01 +08:00
|
|
|
if supported_code.startswith(generic_lang_code + '-'):
|
2013-05-19 18:43:34 +08:00
|
|
|
return supported_code
|
2013-02-24 21:43:45 +08:00
|
|
|
raise LookupError(lang_code)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2013-11-12 14:54:01 +08:00
|
|
|
def get_language_from_path(path, strict=False):
|
2011-06-16 01:29:10 +08:00
|
|
|
"""
|
2018-02-21 22:30:56 +08:00
|
|
|
Return the language code if there's a valid language code found in `path`.
|
2013-05-19 18:43:34 +08:00
|
|
|
|
2018-02-21 22:30:56 +08:00
|
|
|
If `strict` is False (the default), look for a country-specific variant
|
|
|
|
when neither the language code nor its generic variant is found.
|
2011-06-16 01:29:10 +08:00
|
|
|
"""
|
|
|
|
regex_match = language_code_prefix_re.match(path)
|
2013-05-19 18:43:34 +08:00
|
|
|
if not regex_match:
|
|
|
|
return None
|
|
|
|
lang_code = regex_match.group(1)
|
|
|
|
try:
|
2013-11-12 14:54:01 +08:00
|
|
|
return get_supported_language_variant(lang_code, strict=strict)
|
2013-05-19 18:43:34 +08:00
|
|
|
except LookupError:
|
|
|
|
return None
|
2011-06-16 01:29:10 +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):
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Analyze the request to find what language the user wants the system to
|
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
|
|
|
show. Only languages listed in settings.LANGUAGES are taken into account.
|
|
|
|
If the user requests a sublanguage where we have a main language, we send
|
|
|
|
out the main language.
|
2012-02-18 21:37:30 +08:00
|
|
|
|
|
|
|
If check_path is True, the URL path prefix will be checked for a language
|
|
|
|
code, otherwise this is skipped for backwards compatibility.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2012-02-18 21:37:30 +08:00
|
|
|
if check_path:
|
2013-11-12 14:54:01 +08:00
|
|
|
lang_code = get_language_from_path(request.path_info)
|
2012-02-18 21:37:30 +08:00
|
|
|
if lang_code is not None:
|
|
|
|
return lang_code
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2008-03-01 02:38:44 +08:00
|
|
|
lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
|
2016-08-15 04:42:49 +08:00
|
|
|
if lang_code is not None and lang_code in get_languages() and check_for_language(lang_code):
|
|
|
|
return lang_code
|
2010-02-16 20:13:48 +08:00
|
|
|
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2013-11-12 14:54:01 +08:00
|
|
|
return get_supported_language_variant(lang_code)
|
2017-09-07 20:16:21 +08:00
|
|
|
except LookupError:
|
|
|
|
pass
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2007-10-27 03:52:42 +08:00
|
|
|
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
|
2008-02-06 09:04:30 +08:00
|
|
|
for accept_lang, unused in parse_accept_lang_header(accept):
|
|
|
|
if accept_lang == '*':
|
2007-10-27 03:52:42 +08:00
|
|
|
break
|
|
|
|
|
2013-11-12 14:54:01 +08:00
|
|
|
if not language_code_re.search(accept_lang):
|
2007-10-27 03:52:42 +08:00
|
|
|
continue
|
|
|
|
|
2013-05-18 20:37:04 +08:00
|
|
|
try:
|
2013-11-12 14:54:01 +08:00
|
|
|
return get_supported_language_variant(accept_lang)
|
2013-05-18 20:37:04 +08:00
|
|
|
except LookupError:
|
|
|
|
continue
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2013-02-24 21:43:45 +08:00
|
|
|
try:
|
2013-11-12 14:54:01 +08:00
|
|
|
return get_supported_language_variant(settings.LANGUAGE_CODE)
|
2013-02-24 21:43:45 +08:00
|
|
|
except LookupError:
|
|
|
|
return settings.LANGUAGE_CODE
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2017-09-20 06:16:48 +08:00
|
|
|
@functools.lru_cache(maxsize=1000)
|
2007-10-27 03:52:42 +08:00
|
|
|
def parse_accept_lang_header(lang_string):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Parse the lang_string, which is the body of an HTTP Accept-Language
|
2017-09-20 06:16:48 +08:00
|
|
|
header, and return a tuple of (lang, q-value), ordered by 'q' values.
|
2007-10-27 03:52:42 +08:00
|
|
|
|
2017-09-20 06:16:48 +08:00
|
|
|
Return an empty tuple if there are any format errors in lang_string.
|
2007-10-27 03:52:42 +08:00
|
|
|
"""
|
|
|
|
result = []
|
2013-11-12 14:54:01 +08:00
|
|
|
pieces = accept_language_re.split(lang_string.lower())
|
2007-10-27 03:52:42 +08:00
|
|
|
if pieces[-1]:
|
2017-10-21 21:42:25 +08:00
|
|
|
return ()
|
2007-10-27 03:52:42 +08:00
|
|
|
for i in range(0, len(pieces) - 1, 3):
|
2013-10-27 09:27:42 +08:00
|
|
|
first, lang, priority = pieces[i:i + 3]
|
2007-10-27 03:52:42 +08:00
|
|
|
if first:
|
2017-10-21 21:42:25 +08:00
|
|
|
return ()
|
2013-05-17 22:33:36 +08:00
|
|
|
if priority:
|
2016-06-12 09:32:56 +08:00
|
|
|
priority = float(priority)
|
2016-06-13 19:58:25 +08:00
|
|
|
else:
|
2013-09-04 02:22:21 +08:00
|
|
|
priority = 1.0
|
2007-10-27 03:52:42 +08:00
|
|
|
result.append((lang, priority))
|
2010-08-07 00:31:44 +08:00
|
|
|
result.sort(key=lambda k: k[1], reverse=True)
|
2017-09-20 06:16:48 +08:00
|
|
|
return tuple(result)
|