Skip to content

Commit

Permalink
Fixed #3594 - Added ability to discard the language catalog in the Ja…
Browse files Browse the repository at this point in the history
…vaScript i18n view in case the selected language is English but no English translation catalog actual exists, e.g. due to being the language translated from. Thanks to msaelices, aryx and Ramiro Morales.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12384 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jezdez committed Feb 5, 2010
1 parent cdeb90e commit 8600ad4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
30 changes: 22 additions & 8 deletions django/views/i18n.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def javascript_catalog(request, domain='djangojs', packages=None):
locale = to_locale(get_language()) locale = to_locale(get_language())
t = {} t = {}
paths = [] paths = []
en_catalog_missing = False
# first load all english languages files for defaults # first load all english languages files for defaults
for package in packages: for package in packages:
p = importlib.import_module(package) p = importlib.import_module(package)
Expand All @@ -186,7 +187,12 @@ def javascript_catalog(request, domain='djangojs', packages=None):
catalog = gettext_module.translation(domain, path, ['en']) catalog = gettext_module.translation(domain, path, ['en'])
t.update(catalog._catalog) t.update(catalog._catalog)
except IOError: except IOError:
# 'en' catalog was missing. This is harmless. # 'en' catalog was missing.
if locale.startswith('en'):
# If 'en' is the selected language this would cause issues
# later on if default_locale is something other than 'en'.
en_catalog_missing = True
# Otherwise it is harmless.
pass pass
# next load the settings.LANGUAGE_CODE translations if it isn't english # next load the settings.LANGUAGE_CODE translations if it isn't english
if default_locale != 'en': if default_locale != 'en':
Expand All @@ -199,13 +205,21 @@ def javascript_catalog(request, domain='djangojs', packages=None):
t.update(catalog._catalog) t.update(catalog._catalog)
# last load the currently selected language, if it isn't identical to the default. # last load the currently selected language, if it isn't identical to the default.
if locale != default_locale: if locale != default_locale:
for path in paths: # If the flag en_catalog_missing has been set, the currently
try: # selected language is English but it doesn't have a translation
catalog = gettext_module.translation(domain, path, [locale]) # catalog (presumably due to being the language translated from).
except IOError: # If that is the case, a wrong language catalog might have been
catalog = None # loaded in the previous step. It needs to be discarded.
if catalog is not None: if en_catalog_missing:
t.update(catalog._catalog) t = {}
else:
for path in paths:
try:
catalog = gettext_module.translation(domain, path, [locale])
except IOError:
catalog = None
if catalog is not None:
t.update(catalog._catalog)
src = [LibHead] src = [LibHead]
plural = None plural = None
if '' in t: if '' in t:
Expand Down
Binary file not shown.
20 changes: 0 additions & 20 deletions tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po

This file was deleted.

39 changes: 38 additions & 1 deletion tests/regressiontests/views/tests/i18n.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_setlang(self):


def test_jsi18n(self): def test_jsi18n(self):
"""The javascript_catalog can be deployed with language settings""" """The javascript_catalog can be deployed with language settings"""
for lang_code in ['es', 'fr', 'en', 'ru']: for lang_code in ['es', 'fr', 'ru']:
activate(lang_code) activate(lang_code)
catalog = gettext.translation('djangojs', locale_dir, [lang_code]) catalog = gettext.translation('djangojs', locale_dir, [lang_code])
trans_txt = catalog.ugettext('this is to be translated') trans_txt = catalog.ugettext('this is to be translated')
Expand All @@ -30,3 +30,40 @@ def test_jsi18n(self):
# catalog['this is to be translated'] = 'same_that_trans_txt' # catalog['this is to be translated'] = 'same_that_trans_txt'
# javascript_quote is used to be able to check unicode strings # javascript_quote is used to be able to check unicode strings
self.assertContains(response, javascript_quote(trans_txt), 1) self.assertContains(response, javascript_quote(trans_txt), 1)

class JsI18NTests(TestCase):
"""
Tests django views in django/views/i18n.py that need to change
settings.LANGUAGE_CODE.
"""

def setUp(self):
self.old_language_code = settings.LANGUAGE_CODE

def tearDown(self):
settings.LANGUAGE_CODE = self.old_language_code

def test_jsi18n_with_missing_en_files(self):
"""
The javascript_catalog shouldn't load the fallback language in the
case that the current selected language is actually the one translated
from, and hence missing translation files completely.
This happens easily when you're translating from English to other
languages and you've set settings.LANGUAGE_CODE to some other language
than English.
"""
settings.LANGUAGE_CODE = 'es'
activate('en-us')
response = self.client.get('/views/jsi18n/')
self.assertNotContains(response, 'esto tiene que ser traducido')

def test_jsi18n_fallback_language(self):
"""
Let's make sure that the fallback language is still working properly
in cases where the selected language cannot be found.
"""
settings.LANGUAGE_CODE = 'fr'
activate('fi')
response = self.client.get('/views/jsi18n/')
self.assertContains(response, 'il faut le traduire')

0 comments on commit 8600ad4

Please sign in to comment.