2010-05-13 13:29:31 +00:00
|
|
|
# -*- coding:utf-8 -*-
|
2012-03-30 08:02:08 +00:00
|
|
|
from __future__ import absolute_import
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2007-09-17 14:48:33 +00:00
|
|
|
import gettext
|
2011-02-07 18:48:40 +00:00
|
|
|
from os import path
|
2007-09-17 14:48:33 +00:00
|
|
|
|
|
|
|
from django.conf import settings
|
2012-08-03 00:03:52 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2007-09-17 14:48:33 +00:00
|
|
|
from django.test import TestCase
|
2011-11-11 12:25:53 +00:00
|
|
|
from django.utils.translation import override, activate, get_language
|
2009-12-30 22:11:11 +00:00
|
|
|
from django.utils.text import javascript_quote
|
2007-09-17 14:48:33 +00:00
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
from ..urls import locale_dir
|
|
|
|
|
2007-09-17 14:48:33 +00:00
|
|
|
|
|
|
|
class I18NTests(TestCase):
|
|
|
|
""" Tests django views in django/views/i18n.py """
|
|
|
|
|
|
|
|
def test_setlang(self):
|
|
|
|
"""The set_language view can be used to change the session language"""
|
|
|
|
for lang_code, lang_name in settings.LANGUAGES:
|
|
|
|
post_data = dict(language=lang_code, next='/views/')
|
|
|
|
response = self.client.post('/views/i18n/setlang/', data=post_data)
|
|
|
|
self.assertRedirects(response, 'http://testserver/views/')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.client.session['django_language'], lang_code)
|
2007-09-17 14:48:33 +00:00
|
|
|
|
2012-08-03 00:03:52 +00:00
|
|
|
def test_setlang_reversal(self):
|
|
|
|
self.assertEqual(reverse('set_language'), '/views/i18n/setlang/')
|
|
|
|
|
2007-09-17 14:48:33 +00:00
|
|
|
def test_jsi18n(self):
|
|
|
|
"""The javascript_catalog can be deployed with language settings"""
|
2011-11-11 12:25:53 +00:00
|
|
|
saved_lang = get_language()
|
2010-02-05 01:43:13 +00:00
|
|
|
for lang_code in ['es', 'fr', 'ru']:
|
2007-09-17 14:48:33 +00:00
|
|
|
activate(lang_code)
|
|
|
|
catalog = gettext.translation('djangojs', locale_dir, [lang_code])
|
|
|
|
trans_txt = catalog.ugettext('this is to be translated')
|
|
|
|
response = self.client.get('/views/jsi18n/')
|
|
|
|
# in response content must to be a line like that:
|
|
|
|
# catalog['this is to be translated'] = 'same_that_trans_txt'
|
2009-12-30 22:11:11 +00:00
|
|
|
# javascript_quote is used to be able to check unicode strings
|
|
|
|
self.assertContains(response, javascript_quote(trans_txt), 1)
|
2010-11-04 10:48:27 +00:00
|
|
|
if lang_code == 'fr':
|
|
|
|
# Message with context (msgctxt)
|
|
|
|
self.assertContains(response, "['month name\x04May'] = 'mai';", 1)
|
2011-11-11 12:25:53 +00:00
|
|
|
activate(saved_lang)
|
2010-02-05 01:43:13 +00:00
|
|
|
|
2010-05-13 13:29:31 +00:00
|
|
|
|
2010-02-05 01:43:13 +00:00
|
|
|
class JsI18NTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests django views in django/views/i18n.py that need to change
|
|
|
|
settings.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.
|
|
|
|
"""
|
2011-05-06 13:29:44 +00:00
|
|
|
with self.settings(LANGUAGE_CODE='es'):
|
|
|
|
with override('en-us'):
|
|
|
|
response = self.client.get('/views/jsi18n/')
|
|
|
|
self.assertNotContains(response, 'esto tiene que ser traducido')
|
2010-02-05 01:43:13 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2011-05-06 13:29:44 +00:00
|
|
|
with self.settings(LANGUAGE_CODE='fr'):
|
|
|
|
with override('fi'):
|
|
|
|
response = self.client.get('/views/jsi18n/')
|
|
|
|
self.assertContains(response, 'il faut le traduire')
|
2010-05-13 13:29:31 +00:00
|
|
|
|
|
|
|
def testI18NLanguageNonEnglishDefault(self):
|
|
|
|
"""
|
|
|
|
Check if the Javascript i18n view returns an empty language catalog
|
2010-12-13 13:52:52 +00:00
|
|
|
if the default language is non-English, the selected language
|
|
|
|
is English and there is not 'en' translation available. See #13388,
|
|
|
|
#3594 and #13726 for more details.
|
2010-05-13 13:29:31 +00:00
|
|
|
"""
|
2011-05-06 13:29:44 +00:00
|
|
|
with self.settings(LANGUAGE_CODE='fr'):
|
|
|
|
with override('en-us'):
|
|
|
|
response = self.client.get('/views/jsi18n/')
|
|
|
|
self.assertNotContains(response, 'Choisir une heure')
|
2010-12-13 13:52:52 +00:00
|
|
|
|
|
|
|
def test_nonenglish_default_english_userpref(self):
|
|
|
|
"""
|
|
|
|
Same as above with the difference that there IS an 'en' translation
|
|
|
|
available. The Javascript i18n view must return a NON empty language catalog
|
|
|
|
with the proper English translations. See #13726 for more details.
|
|
|
|
"""
|
2011-05-06 13:29:44 +00:00
|
|
|
extended_apps = list(settings.INSTALLED_APPS) + ['regressiontests.views.app0']
|
|
|
|
with self.settings(LANGUAGE_CODE='fr', INSTALLED_APPS=extended_apps):
|
|
|
|
with override('en-us'):
|
|
|
|
response = self.client.get('/views/jsi18n_english_translation/')
|
|
|
|
self.assertContains(response, javascript_quote('this app0 string is to be translated'))
|
2010-05-13 13:29:31 +00:00
|
|
|
|
|
|
|
def testI18NLanguageNonEnglishFallback(self):
|
|
|
|
"""
|
|
|
|
Makes sure that the fallback language is still working properly
|
|
|
|
in cases where the selected language cannot be found.
|
|
|
|
"""
|
2011-05-06 13:29:44 +00:00
|
|
|
with self.settings(LANGUAGE_CODE='fr'):
|
|
|
|
with override('none'):
|
|
|
|
response = self.client.get('/views/jsi18n/')
|
|
|
|
self.assertContains(response, 'Choisir une heure')
|
2010-05-13 13:29:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JsI18NTestsMultiPackage(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for django views in django/views/i18n.py that need to change
|
|
|
|
settings.LANGUAGE_CODE and merge JS translation from several packages.
|
|
|
|
"""
|
|
|
|
def testI18NLanguageEnglishDefault(self):
|
|
|
|
"""
|
|
|
|
Check if the JavaScript i18n view returns a complete language catalog
|
|
|
|
if the default language is en-us, the selected language has a
|
|
|
|
translation available and a catalog composed by djangojs domain
|
|
|
|
translations of multiple Python packages is requested. See #13388,
|
|
|
|
#3594 and #13514 for more details.
|
|
|
|
"""
|
2011-05-06 13:29:44 +00:00
|
|
|
extended_apps = list(settings.INSTALLED_APPS) + ['regressiontests.views.app1', 'regressiontests.views.app2']
|
|
|
|
with self.settings(LANGUAGE_CODE='en-us', INSTALLED_APPS=extended_apps):
|
|
|
|
with override('fr'):
|
|
|
|
response = self.client.get('/views/jsi18n_multi_packages1/')
|
|
|
|
self.assertContains(response, javascript_quote('il faut traduire cette chaîne de caractères de app1'))
|
2010-05-13 13:29:31 +00:00
|
|
|
|
|
|
|
def testI18NDifferentNonEnLangs(self):
|
|
|
|
"""
|
|
|
|
Similar to above but with neither default or requested language being
|
|
|
|
English.
|
|
|
|
"""
|
2011-05-06 13:29:44 +00:00
|
|
|
extended_apps = list(settings.INSTALLED_APPS) + ['regressiontests.views.app3', 'regressiontests.views.app4']
|
|
|
|
with self.settings(LANGUAGE_CODE='fr', INSTALLED_APPS=extended_apps):
|
|
|
|
with override('es-ar'):
|
|
|
|
response = self.client.get('/views/jsi18n_multi_packages2/')
|
|
|
|
self.assertContains(response, javascript_quote('este texto de app3 debe ser traducido'))
|
2011-02-07 18:48:40 +00:00
|
|
|
|
|
|
|
def testI18NWithLocalePaths(self):
|
2011-05-06 13:29:44 +00:00
|
|
|
extended_locale_paths = settings.LOCALE_PATHS + (
|
|
|
|
path.join(path.dirname(
|
|
|
|
path.dirname(path.abspath(__file__))), 'app3', 'locale'),)
|
|
|
|
with self.settings(LANGUAGE_CODE='es-ar', LOCALE_PATHS=extended_locale_paths):
|
|
|
|
with override('es-ar'):
|
|
|
|
response = self.client.get('/views/jsi18n/')
|
|
|
|
self.assertContains(response,
|
|
|
|
javascript_quote('este texto de app3 debe ser traducido'))
|