mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Fixed #5494, #10765, #14924 -- Modified the order in which translations are read when composing the final translation to offer at runtime.
This is slightly backward-incompatible (could result in changed final translations for literals appearing multiple times in different .po files but with different translations). Translations are now read in the following order (from lower to higher priority): For the 'django' gettext domain: * Django translations * INSTALLED_APPS apps translations (with the ones listed first having higher priority) * settings/project path translations (deprecated, see below) * LOCALE_PATHS translations (with the ones listed first having higher priority) For the 'djangojs' gettext domain: * Python modules whose names are passed to the javascript_catalog view * LOCALE_PATHS translations (with the ones listed first having higher priority, previously they weren't included) Also, automatic loading of translations from the 'locale' subdir of the settings/project path is now deprecated. Thanks to vanschelven, vbmendes and an anonymous user for reporting issues, to vanschelven, Claude Paroz and an anonymous contributor for their initial work on fixes and to Jannis Leidel and Claude for review and discussion. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15441 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
"""
|
||||
Internationalization support.
|
||||
"""
|
||||
from os import path
|
||||
|
||||
from django.utils.encoding import force_unicode
|
||||
from django.utils.functional import lazy, curry
|
||||
from django.utils.functional import lazy
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
|
||||
__all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',
|
||||
@@ -33,10 +36,22 @@ class Trans(object):
|
||||
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
|
||||
|
||||
if settings.SETTINGS_MODULE is not None:
|
||||
import warnings
|
||||
parts = settings.SETTINGS_MODULE.split('.')
|
||||
project = import_module(parts[0])
|
||||
if path.isdir(path.join(path.dirname(project.__file__), 'locale')):
|
||||
warnings.warn(
|
||||
"Translations in the project directory aren't supported anymore. Use the LOCALE_PATHS setting instead.",
|
||||
PendingDeprecationWarning
|
||||
)
|
||||
|
||||
else:
|
||||
from django.utils.translation import trans_null as trans
|
||||
setattr(self, real_name, getattr(trans, real_name))
|
||||
|
||||
@@ -125,12 +125,12 @@ def translation(language):
|
||||
|
||||
global _translations
|
||||
|
||||
loc = to_locale(lang)
|
||||
|
||||
res = _translations.get(lang, None)
|
||||
if res is not None:
|
||||
return res
|
||||
|
||||
loc = to_locale(lang)
|
||||
|
||||
def _translation(path):
|
||||
try:
|
||||
t = gettext_module.translation('django', path, [loc], DjangoTranslation)
|
||||
@@ -159,11 +159,7 @@ def translation(language):
|
||||
res.merge(t)
|
||||
return res
|
||||
|
||||
for localepath in settings.LOCALE_PATHS:
|
||||
if os.path.isdir(localepath):
|
||||
res = _merge(localepath)
|
||||
|
||||
for appname in settings.INSTALLED_APPS:
|
||||
for appname in reversed(settings.INSTALLED_APPS):
|
||||
app = import_module(appname)
|
||||
apppath = os.path.join(os.path.dirname(app.__file__), 'locale')
|
||||
|
||||
@@ -173,6 +169,10 @@ def translation(language):
|
||||
if projectpath and os.path.isdir(projectpath):
|
||||
res = _merge(projectpath)
|
||||
|
||||
for localepath in reversed(settings.LOCALE_PATHS):
|
||||
if os.path.isdir(localepath):
|
||||
res = _merge(localepath)
|
||||
|
||||
if res is None:
|
||||
if fallback is not None:
|
||||
res = fallback
|
||||
|
||||
@@ -193,11 +193,15 @@ def javascript_catalog(request, domain='djangojs', packages=None):
|
||||
paths = []
|
||||
en_selected = locale.startswith('en')
|
||||
en_catalog_missing = True
|
||||
# first load all english languages files for defaults
|
||||
# paths of requested packages
|
||||
for package in packages:
|
||||
p = importlib.import_module(package)
|
||||
path = os.path.join(os.path.dirname(p.__file__), 'locale')
|
||||
paths.append(path)
|
||||
# add the filesystem paths listed in the LOCALE_PATHS setting
|
||||
paths.extend(list(reversed(settings.LOCALE_PATHS)))
|
||||
# first load all english languages files for defaults
|
||||
for path in paths:
|
||||
try:
|
||||
catalog = gettext_module.translation(domain, path, ['en'])
|
||||
t.update(catalog._catalog)
|
||||
@@ -275,4 +279,3 @@ def javascript_catalog(request, domain='djangojs', packages=None):
|
||||
src.append(LibFormatFoot)
|
||||
src = ''.join(src)
|
||||
return http.HttpResponse(src, 'text/javascript')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user