mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Reused the DjangoTranslation class for the javascript_catalog view
Thanks Tim Graham and Cristiano Coelho for the reviews. Refs #26328, #26319.
This commit is contained in:
parent
c5fda55edc
commit
11c60b5298
@ -100,9 +100,13 @@ class DjangoTranslation(gettext_module.GNUTranslations):
|
|||||||
requested language and add a fallback to the default language, if it's
|
requested language and add a fallback to the default language, if it's
|
||||||
different from the requested language.
|
different from the requested language.
|
||||||
"""
|
"""
|
||||||
def __init__(self, language):
|
domain = 'django'
|
||||||
|
|
||||||
|
def __init__(self, language, domain=None, localedirs=None):
|
||||||
"""Create a GNUTranslations() using many locale directories"""
|
"""Create a GNUTranslations() using many locale directories"""
|
||||||
gettext_module.GNUTranslations.__init__(self)
|
gettext_module.GNUTranslations.__init__(self)
|
||||||
|
if domain is not None:
|
||||||
|
self.domain = domain
|
||||||
self.set_output_charset('utf-8') # For Python 2 gettext() (#25720)
|
self.set_output_charset('utf-8') # For Python 2 gettext() (#25720)
|
||||||
|
|
||||||
self.__language = language
|
self.__language = language
|
||||||
@ -110,13 +114,26 @@ class DjangoTranslation(gettext_module.GNUTranslations):
|
|||||||
self.__locale = to_locale(language)
|
self.__locale = to_locale(language)
|
||||||
self._catalog = None
|
self._catalog = None
|
||||||
|
|
||||||
self._init_translation_catalog()
|
if self.domain == 'django':
|
||||||
self._add_installed_apps_translations()
|
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()
|
||||||
|
|
||||||
self._add_local_translations()
|
self._add_local_translations()
|
||||||
if self.__language == settings.LANGUAGE_CODE and self._catalog is None:
|
if (self.__language == settings.LANGUAGE_CODE and self.domain == 'django'
|
||||||
|
and self._catalog is None):
|
||||||
# default lang should have at least one translation file available.
|
# default lang should have at least one translation file available.
|
||||||
raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)
|
raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)
|
||||||
self._add_fallback()
|
self._add_fallback(localedirs)
|
||||||
if self._catalog is None:
|
if self._catalog is None:
|
||||||
# No catalogs found for this language, set an empty catalog.
|
# No catalogs found for this language, set an empty catalog.
|
||||||
self._catalog = {}
|
self._catalog = {}
|
||||||
@ -133,7 +150,7 @@ class DjangoTranslation(gettext_module.GNUTranslations):
|
|||||||
references to 'fallback'.
|
references to 'fallback'.
|
||||||
"""
|
"""
|
||||||
return gettext_module.translation(
|
return gettext_module.translation(
|
||||||
domain='django',
|
domain=self.domain,
|
||||||
localedir=localedir,
|
localedir=localedir,
|
||||||
languages=[self.__locale],
|
languages=[self.__locale],
|
||||||
codeset='utf-8',
|
codeset='utf-8',
|
||||||
@ -166,13 +183,19 @@ class DjangoTranslation(gettext_module.GNUTranslations):
|
|||||||
translation = self._new_gnu_trans(localedir)
|
translation = self._new_gnu_trans(localedir)
|
||||||
self.merge(translation)
|
self.merge(translation)
|
||||||
|
|
||||||
def _add_fallback(self):
|
def _add_fallback(self, localedirs=None):
|
||||||
"""Sets the GNUTranslations() fallback with the default language."""
|
"""Sets the GNUTranslations() fallback with the default language."""
|
||||||
# Don't set a fallback for the default language or any English variant
|
# 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)
|
# (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'):
|
if self.__language == settings.LANGUAGE_CODE or self.__language.startswith('en'):
|
||||||
return
|
return
|
||||||
default_translation = translation(settings.LANGUAGE_CODE)
|
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
|
||||||
|
)
|
||||||
self.add_fallback(default_translation)
|
self.add_fallback(default_translation)
|
||||||
|
|
||||||
def merge(self, other):
|
def merge(self, other):
|
||||||
@ -198,7 +221,7 @@ class DjangoTranslation(gettext_module.GNUTranslations):
|
|||||||
|
|
||||||
def translation(language):
|
def translation(language):
|
||||||
"""
|
"""
|
||||||
Returns a translation object.
|
Returns a translation object in the default 'django' domain.
|
||||||
"""
|
"""
|
||||||
global _translations
|
global _translations
|
||||||
if language not in _translations:
|
if language not in _translations:
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import gettext as gettext_module
|
|
||||||
import importlib
|
import importlib
|
||||||
|
import itertools
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -16,6 +16,7 @@ from django.utils.http import is_safe_url
|
|||||||
from django.utils.translation import (
|
from django.utils.translation import (
|
||||||
LANGUAGE_SESSION_KEY, check_for_language, get_language, to_locale,
|
LANGUAGE_SESSION_KEY, check_for_language, get_language, to_locale,
|
||||||
)
|
)
|
||||||
|
from django.utils.translation.trans_real import DjangoTranslation
|
||||||
|
|
||||||
DEFAULT_PACKAGES = ['django.conf']
|
DEFAULT_PACKAGES = ['django.conf']
|
||||||
LANGUAGE_QUERY_PARAMETER = 'language'
|
LANGUAGE_QUERY_PARAMETER = 'language'
|
||||||
@ -202,67 +203,25 @@ def render_javascript_catalog(catalog=None, plural=None):
|
|||||||
|
|
||||||
|
|
||||||
def get_javascript_catalog(locale, domain, packages):
|
def get_javascript_catalog(locale, domain, packages):
|
||||||
default_locale = to_locale(settings.LANGUAGE_CODE)
|
|
||||||
app_configs = apps.get_app_configs()
|
app_configs = apps.get_app_configs()
|
||||||
allowable_packages = set(app_config.name for app_config in app_configs)
|
allowable_packages = set(app_config.name for app_config in app_configs)
|
||||||
allowable_packages.update(DEFAULT_PACKAGES)
|
allowable_packages.update(DEFAULT_PACKAGES)
|
||||||
packages = [p for p in packages if p in allowable_packages]
|
packages = [p for p in packages if p in allowable_packages]
|
||||||
t = {}
|
|
||||||
paths = []
|
paths = []
|
||||||
en_selected = locale.startswith('en')
|
|
||||||
en_catalog_missing = True
|
|
||||||
# paths of requested packages
|
# paths of requested packages
|
||||||
for package in packages:
|
for package in packages:
|
||||||
p = importlib.import_module(package)
|
p = importlib.import_module(package)
|
||||||
path = os.path.join(os.path.dirname(upath(p.__file__)), 'locale')
|
path = os.path.join(os.path.dirname(upath(p.__file__)), 'locale')
|
||||||
paths.append(path)
|
paths.append(path)
|
||||||
# add the filesystem paths listed in the LOCALE_PATHS setting
|
|
||||||
paths.extend(reversed(settings.LOCALE_PATHS))
|
trans = DjangoTranslation(locale, domain=domain, localedirs=paths)
|
||||||
# first load all english languages files for defaults
|
trans_cat = trans._catalog
|
||||||
for path in paths:
|
|
||||||
try:
|
|
||||||
catalog = gettext_module.translation(domain, path, ['en'])
|
|
||||||
t.update(catalog._catalog)
|
|
||||||
except IOError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# 'en' is the selected language and at least one of the packages
|
|
||||||
# listed in `packages` has an 'en' catalog
|
|
||||||
if en_selected:
|
|
||||||
en_catalog_missing = False
|
|
||||||
# next load the settings.LANGUAGE_CODE translations if it isn't english
|
|
||||||
if default_locale != 'en':
|
|
||||||
for path in paths:
|
|
||||||
try:
|
|
||||||
catalog = gettext_module.translation(domain, path, [default_locale])
|
|
||||||
except IOError:
|
|
||||||
catalog = None
|
|
||||||
if catalog is not None:
|
|
||||||
t.update(catalog._catalog)
|
|
||||||
# last load the currently selected language, if it isn't identical to the default.
|
|
||||||
if locale != default_locale:
|
|
||||||
# If the currently selected language is English but it doesn't have a
|
|
||||||
# translation catalog (presumably due to being the language translated
|
|
||||||
# from) then a wrong language catalog might have been loaded in the
|
|
||||||
# previous step. It needs to be discarded.
|
|
||||||
if en_selected and en_catalog_missing:
|
|
||||||
t = {}
|
|
||||||
else:
|
|
||||||
locale_t = {}
|
|
||||||
for path in paths:
|
|
||||||
try:
|
|
||||||
catalog = gettext_module.translation(domain, path, [locale])
|
|
||||||
except IOError:
|
|
||||||
catalog = None
|
|
||||||
if catalog is not None:
|
|
||||||
locale_t.update(catalog._catalog)
|
|
||||||
if locale_t:
|
|
||||||
t = locale_t
|
|
||||||
plural = None
|
plural = None
|
||||||
if '' in t:
|
if '' in trans_cat:
|
||||||
for l in t[''].split('\n'):
|
for line in trans_cat[''].split('\n'):
|
||||||
if l.startswith('Plural-Forms:'):
|
if line.startswith('Plural-Forms:'):
|
||||||
plural = l.split(':', 1)[1].strip()
|
plural = line.split(':', 1)[1].strip()
|
||||||
if plural is not None:
|
if plural is not None:
|
||||||
# this should actually be a compiled function of a typical plural-form:
|
# this should actually be a compiled function of a typical plural-form:
|
||||||
# Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 :
|
# Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 :
|
||||||
@ -272,18 +231,19 @@ def get_javascript_catalog(locale, domain, packages):
|
|||||||
pdict = {}
|
pdict = {}
|
||||||
maxcnts = {}
|
maxcnts = {}
|
||||||
catalog = {}
|
catalog = {}
|
||||||
for k, v in t.items():
|
trans_fallback_cat = trans._fallback._catalog if trans._fallback else {}
|
||||||
if k == '':
|
for key, value in itertools.chain(six.iteritems(trans_cat), six.iteritems(trans_fallback_cat)):
|
||||||
|
if key == '' or key in catalog:
|
||||||
continue
|
continue
|
||||||
if isinstance(k, six.string_types):
|
if isinstance(key, six.string_types):
|
||||||
catalog[k] = v
|
catalog[key] = value
|
||||||
elif isinstance(k, tuple):
|
elif isinstance(key, tuple):
|
||||||
msgid = k[0]
|
msgid = key[0]
|
||||||
cnt = k[1]
|
cnt = key[1]
|
||||||
maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0))
|
maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0))
|
||||||
pdict.setdefault(msgid, {})[cnt] = v
|
pdict.setdefault(msgid, {})[cnt] = value
|
||||||
else:
|
else:
|
||||||
raise TypeError(k)
|
raise TypeError(key)
|
||||||
for k, v in pdict.items():
|
for k, v in pdict.items():
|
||||||
catalog[k] = [v.get(i, '') for i in range(maxcnts[msgid] + 1)]
|
catalog[k] = [v.get(i, '') for i in range(maxcnts[msgid] + 1)]
|
||||||
|
|
||||||
|
@ -19,10 +19,12 @@ msgstr ""
|
|||||||
msgid "this is to be translated"
|
msgid "this is to be translated"
|
||||||
msgstr "il faut le traduire"
|
msgstr "il faut le traduire"
|
||||||
|
|
||||||
|
|
||||||
msgid "Choose a time"
|
msgid "Choose a time"
|
||||||
msgstr "Choisir une heure"
|
msgstr "Choisir une heure"
|
||||||
|
|
||||||
msgctxt "month name"
|
msgctxt "month name"
|
||||||
msgid "May"
|
msgid "May"
|
||||||
msgstr "mai"
|
msgstr "mai"
|
||||||
|
|
||||||
|
msgid "Untranslated string"
|
||||||
|
msgstr ""
|
||||||
|
@ -164,6 +164,7 @@ class JsI18NTests(SimpleTestCase):
|
|||||||
with self.settings(LANGUAGE_CODE='fr'), override('fi'):
|
with self.settings(LANGUAGE_CODE='fr'), override('fi'):
|
||||||
response = self.client.get('/jsi18n/')
|
response = self.client.get('/jsi18n/')
|
||||||
self.assertContains(response, 'il faut le traduire')
|
self.assertContains(response, 'il faut le traduire')
|
||||||
|
self.assertNotContains(response, "Untranslated string")
|
||||||
|
|
||||||
def test_i18n_language_non_english_default(self):
|
def test_i18n_language_non_english_default(self):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user