PageRenderTime 123ms CodeModel.GetById 61ms app.highlight 12ms RepoModel.GetById 48ms app.codeStats 0ms

/tests/regressiontests/views/tests/i18n.py

https://code.google.com/p/mango-py/
Python | 161 lines | 135 code | 11 blank | 15 comment | 8 complexity | a3f8e061330829802fad2a54d099a1d9 MD5 | raw file
  1# -*- coding:utf-8 -*-
  2import gettext
  3from os import path
  4
  5from django.conf import settings
  6from django.test import TestCase
  7from django.utils.translation import activate, deactivate
  8from django.utils.text import javascript_quote
  9
 10from regressiontests.views.urls import locale_dir
 11
 12class I18NTests(TestCase):
 13    """ Tests django views in django/views/i18n.py """
 14
 15    def test_setlang(self):
 16        """The set_language view can be used to change the session language"""
 17        for lang_code, lang_name in settings.LANGUAGES:
 18            post_data = dict(language=lang_code, next='/views/')
 19            response = self.client.post('/views/i18n/setlang/', data=post_data)
 20            self.assertRedirects(response, 'http://testserver/views/')
 21            self.assertEqual(self.client.session['django_language'], lang_code)
 22
 23    def test_jsi18n(self):
 24        """The javascript_catalog can be deployed with language settings"""
 25        for lang_code in ['es', 'fr', 'ru']:
 26            activate(lang_code)
 27            catalog = gettext.translation('djangojs', locale_dir, [lang_code])
 28            trans_txt = catalog.ugettext('this is to be translated')
 29            response = self.client.get('/views/jsi18n/')
 30            # in response content must to be a line like that:
 31            # catalog['this is to be translated'] = 'same_that_trans_txt'
 32            # javascript_quote is used to be able to check unicode strings
 33            self.assertContains(response, javascript_quote(trans_txt), 1)
 34            if lang_code == 'fr':
 35                # Message with context (msgctxt)
 36                self.assertContains(response, "['month name\x04May'] = 'mai';", 1)
 37
 38
 39class JsI18NTests(TestCase):
 40    """
 41    Tests django views in django/views/i18n.py that need to change
 42    settings.LANGUAGE_CODE.
 43    """
 44
 45    def setUp(self):
 46        self.old_language_code = settings.LANGUAGE_CODE
 47        self.old_installed_apps = settings.INSTALLED_APPS
 48
 49    def tearDown(self):
 50        deactivate()
 51        settings.LANGUAGE_CODE = self.old_language_code
 52        settings.INSTALLED_APPS = self.old_installed_apps
 53
 54    def test_jsi18n_with_missing_en_files(self):
 55        """
 56        The javascript_catalog shouldn't load the fallback language in the
 57        case that the current selected language is actually the one translated
 58        from, and hence missing translation files completely.
 59
 60        This happens easily when you're translating from English to other
 61        languages and you've set settings.LANGUAGE_CODE to some other language
 62        than English.
 63        """
 64        settings.LANGUAGE_CODE = 'es'
 65        activate('en-us')
 66        response = self.client.get('/views/jsi18n/')
 67        self.assertNotContains(response, 'esto tiene que ser traducido')
 68
 69    def test_jsi18n_fallback_language(self):
 70        """
 71        Let's make sure that the fallback language is still working properly
 72        in cases where the selected language cannot be found.
 73        """
 74        settings.LANGUAGE_CODE = 'fr'
 75        activate('fi')
 76        response = self.client.get('/views/jsi18n/')
 77        self.assertContains(response, 'il faut le traduire')
 78
 79    def testI18NLanguageNonEnglishDefault(self):
 80        """
 81        Check if the Javascript i18n view returns an empty language catalog
 82        if the default language is non-English, the selected language
 83        is English and there is not 'en' translation available. See #13388,
 84        #3594 and #13726 for more details.
 85        """
 86        settings.LANGUAGE_CODE = 'fr'
 87        activate('en-us')
 88        response = self.client.get('/views/jsi18n/')
 89        self.assertNotContains(response, 'Choisir une heure')
 90
 91    def test_nonenglish_default_english_userpref(self):
 92        """
 93        Same as above with the difference that there IS an 'en' translation
 94        available. The Javascript i18n view must return a NON empty language catalog
 95        with the proper English translations. See #13726 for more details.
 96        """
 97        settings.LANGUAGE_CODE = 'fr'
 98        settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) + ['regressiontests.views.app0']
 99        activate('en-us')
100        response = self.client.get('/views/jsi18n_english_translation/')
101        self.assertContains(response, javascript_quote('this app0 string is to be translated'))
102
103    def testI18NLanguageNonEnglishFallback(self):
104        """
105        Makes sure that the fallback language is still working properly
106        in cases where the selected language cannot be found.
107        """
108        settings.LANGUAGE_CODE = 'fr'
109        activate('none')
110        response = self.client.get('/views/jsi18n/')
111        self.assertContains(response, 'Choisir une heure')
112
113
114class JsI18NTestsMultiPackage(TestCase):
115    """
116    Tests for django views in django/views/i18n.py that need to change
117    settings.LANGUAGE_CODE and merge JS translation from several packages.
118    """
119
120    def setUp(self):
121        self.old_language_code = settings.LANGUAGE_CODE
122        self.old_installed_apps = settings.INSTALLED_APPS
123
124    def tearDown(self):
125        settings.LANGUAGE_CODE = self.old_language_code
126        settings.INSTALLED_APPS = self.old_installed_apps
127
128    def testI18NLanguageEnglishDefault(self):
129        """
130        Check if the JavaScript i18n view returns a complete language catalog
131        if the default language is en-us, the selected language has a
132        translation available and a catalog composed by djangojs domain
133        translations of multiple Python packages is requested. See #13388,
134        #3594 and #13514 for more details.
135        """
136        settings.LANGUAGE_CODE = 'en-us'
137        settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) + ['regressiontests.views.app1', 'regressiontests.views.app2']
138        activate('fr')
139        response = self.client.get('/views/jsi18n_multi_packages1/')
140        self.assertContains(response, javascript_quote('il faut traduire cette cha?始e de caract?ィres de app1'))
141        deactivate()
142
143    def testI18NDifferentNonEnLangs(self):
144        """
145        Similar to above but with neither default or requested language being
146        English.
147        """
148        settings.LANGUAGE_CODE = 'fr'
149        settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) + ['regressiontests.views.app3', 'regressiontests.views.app4']
150        activate('es-ar')
151        response = self.client.get('/views/jsi18n_multi_packages2/')
152        self.assertContains(response, javascript_quote('este texto de app3 debe ser traducido'))
153        deactivate()
154
155    def testI18NWithLocalePaths(self):
156        settings.LANGUAGE_CODE = 'es-ar'
157        self.old_locale_paths = settings.LOCALE_PATHS
158        settings.LOCALE_PATHS += (path.join(path.dirname(path.dirname(path.abspath(__file__))), 'app3', 'locale'),)
159        response = self.client.get('/views/jsi18n/')
160        self.assertContains(response, javascript_quote('este texto de app3 debe ser traducido'))
161        settings.LOCALE_PATHS = self.old_locale_paths