コード例 #1
0
 def test_verbose_name(self):
     company_type = ContentType.objects.get(app_label='i18n',
                                            model='company')
     with translation.override('en'):
         self.assertEqual(str(company_type), 'Company')
     with translation.override('fr'):
         self.assertEqual(str(company_type), 'Société')
コード例 #2
0
    def test_format_lazy(self):
        self.assertEqual('djmodels/test',
                         format_lazy('{}/{}', 'djmodels', lazystr('test')))
        self.assertEqual('djmodels/test',
                         format_lazy('{0}/{1}', *('djmodels', 'test')))
        self.assertEqual(
            'djmodels/test',
            format_lazy('{a}/{b}', **{
                'a': 'djmodels',
                'b': 'test'
            }))
        self.assertEqual('djmodels/test',
                         format_lazy('{a[0]}/{a[1]}', a=('djmodels', 'test')))

        t = {}
        s = format_lazy('{0[a]}-{p[a]}', t, p=t)
        t['a'] = lazystr('djmodels')
        self.assertEqual('djmodels-djmodels', s)
        t['a'] = 'update'
        self.assertEqual('update-update', s)

        # The format string can be lazy. (string comes from contrib.admin)
        s = format_lazy(
            gettext_lazy("Added {name} \"{object}\"."),
            name='article',
            object='My first try',
        )
        with override('fr'):
            self.assertEqual('Ajout de article «\xa0My first try\xa0».', s)
コード例 #3
0
 def test_apnumber(self):
     test_list = [str(x) for x in range(1, 11)]
     test_list.append(None)
     result_list = ('one', 'two', 'three', 'four', 'five', 'six', 'seven',
                    'eight', 'nine', '10', None)
     with translation.override('en'):
         self.humanize_tester(test_list, result_list, 'apnumber')
コード例 #4
0
 def test_intcomma(self):
     test_list = (
         100,
         1000,
         10123,
         10311,
         1000000,
         1234567.25,
         '100',
         '1000',
         '10123',
         '10311',
         '1000000',
         '1234567.1234567',
         Decimal('1234567.1234567'),
         None,
     )
     result_list = (
         '100',
         '1,000',
         '10,123',
         '10,311',
         '1,000,000',
         '1,234,567.25',
         '100',
         '1,000',
         '10,123',
         '10,311',
         '1,000,000',
         '1,234,567.1234567',
         '1,234,567.1234567',
         None,
     )
     with translation.override('en'):
         self.humanize_tester(test_list, result_list, 'intcomma')
コード例 #5
0
 def test_intword(self):
     test_list = (
         '100',
         '1000000',
         '1200000',
         '1290000',
         '1000000000',
         '2000000000',
         '6000000000000',
         '1300000000000000',
         '3500000000000000000000',
         '8100000000000000000000000000000000',
         None,
     )
     result_list = (
         '100',
         '1.0 million',
         '1.2 million',
         '1.3 million',
         '1.0 billion',
         '2.0 billion',
         '6.0 trillion',
         '1.3 quadrillion',
         '3.5 sextillion',
         '8.1 decillion',
         None,
     )
     with translation.override('en'):
         self.humanize_tester(test_list, result_list, 'intword')
コード例 #6
0
    def test_dative_inflection_for_timedelta(self):
        """Translation may differ depending on the string it is inserted in."""
        test_list = [
            now - datetime.timedelta(days=1),
            now - datetime.timedelta(days=2),
            now - datetime.timedelta(days=30),
            now - datetime.timedelta(days=60),
            now - datetime.timedelta(days=500),
            now - datetime.timedelta(days=865),
        ]
        result_list = [
            'vor 1\xa0Tag',
            'vor 2\xa0Tagen',
            'vor 1\xa0Monat',
            'vor 2\xa0Monaten',
            'vor 1\xa0Jahr, 4\xa0Monaten',
            'vor 2\xa0Jahren, 4\xa0Monaten',
        ]

        orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
        try:
            with translation.override('de'), self.settings(USE_L10N=True):
                self.humanize_tester(test_list, result_list, 'naturaltime')
        finally:
            humanize.datetime = orig_humanize_datetime
コード例 #7
0
 def test_i18n_intcomma(self):
     test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25, '100',
                  '1000', '10123', '10311', '1000000', None)
     result_list = ('100', '1.000', '10.123', '10.311', '1.000.000',
                    '1.234.567,25', '100', '1.000', '10.123', '10.311',
                    '1.000.000', None)
     with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
         with translation.override('de'):
             self.humanize_tester(test_list, result_list, 'intcomma')
コード例 #8
0
ファイル: test_json.py プロジェクト: iMerica/dj-models
 def test_lazy_string_encoding(self):
     self.assertEqual(
         json.dumps({'lang': gettext_lazy("French")},
                    cls=DjangoJSONEncoder), '{"lang": "French"}')
     with override('fr'):
         self.assertEqual(
             json.dumps({'lang': gettext_lazy("French")},
                        cls=DjangoJSONEncoder),
             '{"lang": "Fran\\u00e7ais"}')
コード例 #9
0
    def test_ordinal(self):
        test_list = ('1', '2', '3', '4', '11', '12', '13', '101', '102', '103',
                     '111', 'something else', None)
        result_list = ('1st', '2nd', '3rd', '4th', '11th', '12th', '13th',
                       '101st', '102nd', '103rd', '111th', 'something else',
                       None)

        with translation.override('en'):
            self.humanize_tester(test_list, result_list, 'ordinal')
コード例 #10
0
 def test_get_text_list(self):
     self.assertEqual(text.get_text_list(['a', 'b', 'c', 'd']),
                      'a, b, c or d')
     self.assertEqual(text.get_text_list(['a', 'b', 'c'], 'and'),
                      'a, b and c')
     self.assertEqual(text.get_text_list(['a', 'b'], 'and'), 'a and b')
     self.assertEqual(text.get_text_list(['a']), 'a')
     self.assertEqual(text.get_text_list([]), '')
     with override('ar'):
         self.assertEqual(text.get_text_list(['a', 'b', 'c']), "a، b أو c")
コード例 #11
0
ファイル: tests.py プロジェクト: iMerica/dj-models
 def test_no_translations_deactivate_translations(self):
     """
     When the Command handle method is decorated with @no_translations,
     translations are deactivated inside the command.
     """
     current_locale = translation.get_language()
     with translation.override('pl'):
         result = management.call_command('no_translations', stdout=StringIO())
         self.assertIsNone(result)
     self.assertEqual(translation.get_language(), current_locale)
コード例 #12
0
    def test_naturalday_uses_localtime(self):
        # Regression for #18504
        # This is 2012-03-08HT19:30:00-06:00 in America/Chicago
        dt = datetime.datetime(2012, 3, 9, 1, 30, tzinfo=utc)

        orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
        try:
            with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True):
                with translation.override('en'):
                    self.humanize_tester([dt], ['yesterday'], 'naturalday')
        finally:
            humanize.datetime = orig_humanize_datetime
コード例 #13
0
    def test_i18n_html_ordinal(self):
        """Allow html in output on i18n strings"""
        test_list = ('1', '2', '3', '4', '11', '12', '13', '101', '102', '103',
                     '111', 'something else', None)
        result_list = ('1<sup>er</sup>', '2<sup>e</sup>', '3<sup>e</sup>',
                       '4<sup>e</sup>', '11<sup>e</sup>', '12<sup>e</sup>',
                       '13<sup>e</sup>', '101<sup>er</sup>', '102<sup>e</sup>',
                       '103<sup>e</sup>', '111<sup>e</sup>', 'something else',
                       'None')

        with translation.override('fr-fr'):
            self.humanize_tester(test_list, result_list, 'ordinal',
                                 lambda x: x)
コード例 #14
0
ファイル: tests.py プロジェクト: iMerica/dj-models
    def test_translations(self):
        a1 = Article.objects.create(pub_date=datetime.date.today())
        at1_fi = ArticleTranslation(article=a1,
                                    lang='fi',
                                    title='Otsikko',
                                    body='Diipadaapa')
        at1_fi.save()
        at2_en = ArticleTranslation(article=a1,
                                    lang='en',
                                    title='Title',
                                    body='Lalalalala')
        at2_en.save()

        self.assertEqual(
            Article.objects.get(pk=a1.pk).active_translation, at1_fi)

        with self.assertNumQueries(1):
            fetched = Article.objects.select_related('active_translation').get(
                active_translation__title='Otsikko')
            self.assertEqual(fetched.active_translation.title, 'Otsikko')
        a2 = Article.objects.create(pub_date=datetime.date.today())
        at2_fi = ArticleTranslation(article=a2,
                                    lang='fi',
                                    title='Atsikko',
                                    body='Diipadaapa',
                                    abstract='dipad')
        at2_fi.save()
        a3 = Article.objects.create(pub_date=datetime.date.today())
        at3_en = ArticleTranslation(article=a3,
                                    lang='en',
                                    title='A title',
                                    body='lalalalala',
                                    abstract='lala')
        at3_en.save()
        # Test model initialization with active_translation field.
        a3 = Article(id=a3.id, pub_date=a3.pub_date, active_translation=at3_en)
        a3.save()
        self.assertEqual(
            list(Article.objects.filter(active_translation__abstract=None)),
            [a1, a3])
        self.assertEqual(
            list(
                Article.objects.filter(active_translation__abstract=None,
                                       active_translation__pk__isnull=False)),
            [a1])

        with translation.override('en'):
            self.assertEqual(
                list(
                    Article.objects.filter(active_translation__abstract=None)),
                [a1, a2])
コード例 #15
0
ファイル: test_forms.py プロジェクト: iMerica/dj-models
    def test_flatpage_admin_form_url_uniqueness_validation(self):
        "The flatpage admin form correctly enforces url uniqueness among flatpages of the same site"
        data = dict(url='/myflatpage1/', **self.form_data)

        FlatpageForm(data=data).save()

        f = FlatpageForm(data=data)

        with translation.override('en'):
            self.assertFalse(f.is_valid())

            self.assertEqual(
                f.errors,
                {'__all__': ['Flatpage with url /myflatpage1/ already exists for site example.com']})
コード例 #16
0
 def test_i18n_intword(self):
     test_list = (
         '100',
         '1000000',
         '1200000',
         '1290000',
         '1000000000',
         '2000000000',
         '6000000000000',
     )
     result_list = (
         '100',
         '1,0 Million',
         '1,2 Millionen',
         '1,3 Millionen',
         '1,0 Milliarde',
         '2,0 Milliarden',
         '6,0 Billionen',
     )
     with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
         with translation.override('de'):
             self.humanize_tester(test_list, result_list, 'intword')
コード例 #17
0
 def test_l10n_intcomma(self):
     test_list = (
         100,
         1000,
         10123,
         10311,
         1000000,
         1234567.25,
         '100',
         '1000',
         '10123',
         '10311',
         '1000000',
         '1234567.1234567',
         Decimal('1234567.1234567'),
         None,
     )
     result_list = (
         '100',
         '1,000',
         '10,123',
         '10,311',
         '1,000,000',
         '1,234,567.25',
         '100',
         '1,000',
         '10,123',
         '10,311',
         '1,000,000',
         '1,234,567.1234567',
         '1,234,567.1234567',
         None,
     )
     with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False):
         with translation.override('en'):
             self.humanize_tester(test_list, result_list, 'intcomma')
コード例 #18
0
ファイル: test_compilation.py プロジェクト: iMerica/dj-models
 def test_fuzzy_compiling(self):
     with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, 'locale')]):
         call_command('compilemessages', locale=[self.LOCALE], fuzzy=True, stdout=StringIO())
         with translation.override(self.LOCALE):
             self.assertEqual(gettext('Lenin'), 'Ленин')
             self.assertEqual(gettext('Vodka'), 'Водка')
コード例 #19
0
 def test_intcomma_without_number_grouping(self):
     # Regression for #17414
     with translation.override('ja'), self.settings(USE_L10N=True):
         self.humanize_tester([100], ['100'], 'intcomma')
コード例 #20
0
ファイル: options.py プロジェクト: iMerica/dj-models
 def verbose_name_raw(self):
     """Return the untranslated verbose name."""
     with override(None):
         return str(self.verbose_name)
コード例 #21
0
ファイル: tests.py プロジェクト: iMerica/dj-models
 def test_language_preserved(self):
     out = StringIO()
     with translation.override('fr'):
         management.call_command('dance', stdout=out)
         self.assertEqual(translation.get_language(), 'fr')
コード例 #22
0
ファイル: test_forms.py プロジェクト: iMerica/dj-models
 def test_flatpage_requires_trailing_slash_with_append_slash(self):
     form = FlatpageForm(data=dict(url='/no_trailing_slash', **self.form_data))
     with translation.override('en'):
         self.assertFalse(form.is_valid())
         self.assertEqual(form.errors['url'], ["URL is missing a trailing slash."])
コード例 #23
0
    def test_naturaltime(self):
        class naive(datetime.tzinfo):
            def utcoffset(self, dt):
                return None

        test_list = [
            now,
            now - datetime.timedelta(seconds=1),
            now - datetime.timedelta(seconds=30),
            now - datetime.timedelta(minutes=1, seconds=30),
            now - datetime.timedelta(minutes=2),
            now - datetime.timedelta(hours=1, minutes=30, seconds=30),
            now - datetime.timedelta(hours=23, minutes=50, seconds=50),
            now - datetime.timedelta(days=1),
            now - datetime.timedelta(days=500),
            now + datetime.timedelta(seconds=1),
            now + datetime.timedelta(seconds=30),
            now + datetime.timedelta(minutes=1, seconds=30),
            now + datetime.timedelta(minutes=2),
            now + datetime.timedelta(hours=1, minutes=30, seconds=30),
            now + datetime.timedelta(hours=23, minutes=50, seconds=50),
            now + datetime.timedelta(days=1),
            now + datetime.timedelta(days=2, hours=6),
            now + datetime.timedelta(days=500),
            now.replace(tzinfo=naive()),
            now.replace(tzinfo=utc),
        ]
        result_list = [
            'now',
            'a second ago',
            '30\xa0seconds ago',
            'a minute ago',
            '2\xa0minutes ago',
            'an hour ago',
            '23\xa0hours ago',
            '1\xa0day ago',
            '1\xa0year, 4\xa0months ago',
            'a second from now',
            '30\xa0seconds from now',
            'a minute from now',
            '2\xa0minutes from now',
            'an hour from now',
            '23\xa0hours from now',
            '1\xa0day from now',
            '2\xa0days, 6\xa0hours from now',
            '1\xa0year, 4\xa0months from now',
            'now',
            'now',
        ]
        # Because of the DST change, 2 days and 6 hours after the chosen
        # date in naive arithmetic is only 2 days and 5 hours after in
        # aware arithmetic.
        result_list_with_tz_support = result_list[:]
        assert result_list_with_tz_support[
            -4] == '2\xa0days, 6\xa0hours from now'
        result_list_with_tz_support[-4] == '2\xa0days, 5\xa0hours from now'

        orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
        try:
            with translation.override('en'):
                self.humanize_tester(test_list, result_list, 'naturaltime')
                with override_settings(USE_TZ=True):
                    self.humanize_tester(test_list,
                                         result_list_with_tz_support,
                                         'naturaltime')
        finally:
            humanize.datetime = orig_humanize_datetime