2010-04-19 20:40:46 +08:00
|
|
|
import datetime
|
|
|
|
import decimal
|
2014-05-01 00:04:30 +08:00
|
|
|
import gettext as gettext_module
|
2010-02-16 20:17:17 +08:00
|
|
|
import os
|
2010-04-19 20:40:46 +08:00
|
|
|
import pickle
|
2018-02-11 05:05:41 +08:00
|
|
|
import re
|
2018-02-18 23:35:06 +08:00
|
|
|
import tempfile
|
2015-01-28 20:35:27 +08:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from importlib import import_module
|
2019-01-14 09:33:47 +08:00
|
|
|
from pathlib import Path
|
2018-02-18 23:35:06 +08:00
|
|
|
from unittest import mock
|
2009-12-24 22:23:52 +08:00
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
from asgiref.local import Local
|
2019-01-14 09:33:47 +08:00
|
|
|
|
2015-01-26 11:28:57 +08:00
|
|
|
from django import forms
|
2018-02-18 23:35:06 +08:00
|
|
|
from django.apps import AppConfig
|
2009-12-24 22:23:52 +08:00
|
|
|
from django.conf import settings
|
2017-10-14 00:37:31 +08:00
|
|
|
from django.conf.locale import LANG_INFO
|
2016-02-29 16:24:19 +08:00
|
|
|
from django.conf.urls.i18n import i18n_patterns
|
2017-01-06 23:33:07 +08:00
|
|
|
from django.template import Context, Template
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import (
|
2017-09-03 07:54:21 +08:00
|
|
|
RequestFactory, SimpleTestCase, TestCase, override_settings,
|
2015-04-18 05:38:20 +08:00
|
|
|
)
|
2016-12-29 23:27:49 +08:00
|
|
|
from django.utils import translation
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.formats import (
|
2021-07-18 21:06:30 +08:00
|
|
|
date_format, get_format, iter_format_modules, localize, localize_input,
|
|
|
|
reset_format_cache, sanitize_separators, sanitize_strftime_format,
|
|
|
|
time_format,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
|
|
|
from django.utils.numberformat import format as nformat
|
2019-02-05 22:38:29 +08:00
|
|
|
from django.utils.safestring import SafeString, mark_safe
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.translation import (
|
2021-01-06 20:16:24 +08:00
|
|
|
activate, check_for_language, deactivate, get_language, get_language_bidi,
|
|
|
|
get_language_from_request, get_language_info, gettext, gettext_lazy,
|
|
|
|
ngettext, ngettext_lazy, npgettext, npgettext_lazy, pgettext,
|
|
|
|
round_away_from_one, to_language, to_locale, trans_null, trans_real,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2019-01-14 09:33:47 +08:00
|
|
|
from django.utils.translation.reloader import (
|
|
|
|
translation_file_changed, watch_for_translation_changes,
|
|
|
|
)
|
2010-09-13 04:03:39 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .forms import CompanyForm, I18nForm, SelectDateForm
|
2011-10-14 05:34:56 +08:00
|
|
|
from .models import Company, TestModel
|
2009-12-31 06:11:48 +08:00
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
2015-01-22 00:55:57 +08:00
|
|
|
extended_locale_paths = settings.LOCALE_PATHS + [
|
2012-10-22 20:45:41 +08:00
|
|
|
os.path.join(here, 'other', 'locale'),
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2011-09-08 21:24:41 +08:00
|
|
|
|
2014-03-02 04:46:23 +08:00
|
|
|
|
2018-02-18 23:35:06 +08:00
|
|
|
class AppModuleStub:
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.__dict__.update(kwargs)
|
|
|
|
|
|
|
|
|
2014-03-01 03:40:03 +08:00
|
|
|
@contextmanager
|
|
|
|
def patch_formats(lang, **settings):
|
|
|
|
from django.utils.formats import _format_cache
|
|
|
|
|
|
|
|
# Populate _format_cache with temporary values
|
|
|
|
for key, value in settings.items():
|
|
|
|
_format_cache[(key, lang)] = value
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
reset_format_cache()
|
|
|
|
|
2013-05-19 17:44:46 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class TranslationTests(SimpleTestCase):
|
2015-03-21 02:45:53 +08:00
|
|
|
@translation.override('fr')
|
|
|
|
def test_plural(self):
|
|
|
|
"""
|
2017-01-27 03:58:33 +08:00
|
|
|
Test plurals with ngettext. French differs from English in that 0 is singular.
|
2015-03-21 02:45:53 +08:00
|
|
|
"""
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertEqual(ngettext("%d year", "%d years", 0) % 0, "0 année")
|
|
|
|
self.assertEqual(ngettext("%d year", "%d years", 2) % 2, "2 années")
|
|
|
|
self.assertEqual(ngettext("%(size)d byte", "%(size)d bytes", 0) % {'size': 0}, "0 octet")
|
|
|
|
self.assertEqual(ngettext("%(size)d byte", "%(size)d bytes", 2) % {'size': 2}, "2 octets")
|
2015-03-21 02:45:53 +08:00
|
|
|
|
2018-05-08 22:18:33 +08:00
|
|
|
def test_plural_null(self):
|
|
|
|
g = trans_null.ngettext
|
|
|
|
self.assertEqual(g('%d year', '%d years', 0) % 0, '0 years')
|
|
|
|
self.assertEqual(g('%d year', '%d years', 1) % 1, '1 year')
|
|
|
|
self.assertEqual(g('%d year', '%d years', 2) % 2, '2 years')
|
|
|
|
|
2020-03-10 22:56:32 +08:00
|
|
|
@override_settings(LOCALE_PATHS=extended_locale_paths)
|
|
|
|
@translation.override('fr')
|
|
|
|
def test_multiple_plurals_per_language(self):
|
|
|
|
"""
|
|
|
|
Normally, French has 2 plurals. As other/locale/fr/LC_MESSAGES/django.po
|
|
|
|
has a different plural equation with 3 plurals, this tests if those
|
|
|
|
plural are honored.
|
|
|
|
"""
|
|
|
|
self.assertEqual(ngettext("%d singular", "%d plural", 0) % 0, "0 pluriel1")
|
|
|
|
self.assertEqual(ngettext("%d singular", "%d plural", 1) % 1, "1 singulier")
|
|
|
|
self.assertEqual(ngettext("%d singular", "%d plural", 2) % 2, "2 pluriel2")
|
|
|
|
french = trans_real.catalog()
|
|
|
|
# Internal _catalog can query subcatalogs (from different po files).
|
|
|
|
self.assertEqual(french._catalog[('%d singular', 0)], '%d singulier')
|
|
|
|
self.assertEqual(french._catalog[('%d hour', 0)], '%d heure')
|
|
|
|
|
2011-05-06 21:29:32 +08:00
|
|
|
def test_override(self):
|
|
|
|
activate('de')
|
2014-01-26 22:28:33 +08:00
|
|
|
try:
|
|
|
|
with translation.override('pl'):
|
|
|
|
self.assertEqual(get_language(), 'pl')
|
|
|
|
self.assertEqual(get_language(), 'de')
|
|
|
|
with translation.override(None):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(get_language())
|
2015-08-21 20:09:29 +08:00
|
|
|
with translation.override('pl'):
|
|
|
|
pass
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(get_language())
|
2014-01-26 22:28:33 +08:00
|
|
|
self.assertEqual(get_language(), 'de')
|
|
|
|
finally:
|
|
|
|
deactivate()
|
2011-05-06 21:29:32 +08:00
|
|
|
|
2014-08-20 00:44:10 +08:00
|
|
|
def test_override_decorator(self):
|
|
|
|
|
|
|
|
@translation.override('pl')
|
|
|
|
def func_pl():
|
|
|
|
self.assertEqual(get_language(), 'pl')
|
|
|
|
|
|
|
|
@translation.override(None)
|
|
|
|
def func_none():
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(get_language())
|
2014-08-20 00:44:10 +08:00
|
|
|
|
|
|
|
try:
|
2014-08-29 04:26:37 +08:00
|
|
|
activate('de')
|
2014-08-20 00:44:10 +08:00
|
|
|
func_pl()
|
|
|
|
self.assertEqual(get_language(), 'de')
|
|
|
|
func_none()
|
|
|
|
self.assertEqual(get_language(), 'de')
|
|
|
|
finally:
|
|
|
|
deactivate()
|
|
|
|
|
2014-08-29 04:26:37 +08:00
|
|
|
def test_override_exit(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The language restored is the one used when the function was
|
|
|
|
called, not the one used when the decorator was initialized (#23381).
|
2014-08-29 04:26:37 +08:00
|
|
|
"""
|
|
|
|
activate('fr')
|
2014-08-29 21:42:22 +08:00
|
|
|
|
2014-08-29 04:26:37 +08:00
|
|
|
@translation.override('pl')
|
|
|
|
def func_pl():
|
|
|
|
pass
|
|
|
|
deactivate()
|
|
|
|
|
|
|
|
try:
|
|
|
|
activate('en')
|
|
|
|
func_pl()
|
|
|
|
self.assertEqual(get_language(), 'en')
|
|
|
|
finally:
|
|
|
|
deactivate()
|
|
|
|
|
2009-12-23 01:58:49 +08:00
|
|
|
def test_lazy_objects(self):
|
|
|
|
"""
|
|
|
|
Format string interpolation should work with *_lazy objects.
|
|
|
|
"""
|
2017-01-27 03:58:33 +08:00
|
|
|
s = gettext_lazy('Add %(name)s')
|
2009-12-23 01:58:49 +08:00
|
|
|
d = {'name': 'Ringo'}
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('Add Ringo', s % d)
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('de', deactivate=True):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('Ringo hinzuf\xfcgen', s % d)
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('pl'):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('Dodaj Ringo', s % d)
|
2009-12-23 01:58:49 +08:00
|
|
|
|
|
|
|
# It should be possible to compare *_lazy objects.
|
2017-01-27 03:58:33 +08:00
|
|
|
s1 = gettext_lazy('Add %(name)s')
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertEqual(s, s1)
|
2009-12-23 01:58:49 +08:00
|
|
|
s2 = gettext_lazy('Add %(name)s')
|
|
|
|
s3 = gettext_lazy('Add %(name)s')
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertEqual(s2, s3)
|
|
|
|
self.assertEqual(s, s2)
|
2017-01-27 03:58:33 +08:00
|
|
|
s4 = gettext_lazy('Some other string')
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertNotEqual(s, s4)
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2010-04-19 20:40:46 +08:00
|
|
|
def test_lazy_pickle(self):
|
2017-01-27 03:58:33 +08:00
|
|
|
s1 = gettext_lazy("test")
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertEqual(str(s1), "test")
|
2010-04-19 20:40:46 +08:00
|
|
|
s2 = pickle.loads(pickle.dumps(s1))
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertEqual(str(s2), "test")
|
2010-04-19 20:40:46 +08:00
|
|
|
|
2013-01-31 03:28:16 +08:00
|
|
|
@override_settings(LOCALE_PATHS=extended_locale_paths)
|
2017-01-27 03:58:33 +08:00
|
|
|
def test_ngettext_lazy(self):
|
|
|
|
simple_with_format = ngettext_lazy('%d good result', '%d good results')
|
2013-02-02 17:56:41 +08:00
|
|
|
simple_context_with_format = npgettext_lazy('Exclamation', '%d good result', '%d good results')
|
2017-01-27 03:58:33 +08:00
|
|
|
simple_without_format = ngettext_lazy('good result', 'good results')
|
2013-01-31 03:28:16 +08:00
|
|
|
with translation.override('de'):
|
2013-02-02 17:56:41 +08:00
|
|
|
self.assertEqual(simple_with_format % 1, '1 gutes Resultat')
|
|
|
|
self.assertEqual(simple_with_format % 4, '4 guten Resultate')
|
|
|
|
self.assertEqual(simple_context_with_format % 1, '1 gutes Resultat!')
|
|
|
|
self.assertEqual(simple_context_with_format % 4, '4 guten Resultate!')
|
|
|
|
self.assertEqual(simple_without_format % 1, 'gutes Resultat')
|
|
|
|
self.assertEqual(simple_without_format % 4, 'guten Resultate')
|
2013-01-31 03:28:16 +08:00
|
|
|
|
2017-01-27 03:58:33 +08:00
|
|
|
complex_nonlazy = ngettext_lazy('Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 4)
|
|
|
|
complex_deferred = ngettext_lazy(
|
2015-09-12 07:33:12 +08:00
|
|
|
'Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 'num'
|
|
|
|
)
|
|
|
|
complex_context_nonlazy = npgettext_lazy(
|
|
|
|
'Greeting', 'Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 4
|
|
|
|
)
|
|
|
|
complex_context_deferred = npgettext_lazy(
|
|
|
|
'Greeting', 'Hi %(name)s, %(num)d good result', 'Hi %(name)s, %(num)d good results', 'num'
|
|
|
|
)
|
2013-01-31 03:28:16 +08:00
|
|
|
with translation.override('de'):
|
2013-02-02 17:56:41 +08:00
|
|
|
self.assertEqual(complex_nonlazy % {'num': 4, 'name': 'Jim'}, 'Hallo Jim, 4 guten Resultate')
|
|
|
|
self.assertEqual(complex_deferred % {'name': 'Jim', 'num': 1}, 'Hallo Jim, 1 gutes Resultat')
|
|
|
|
self.assertEqual(complex_deferred % {'name': 'Jim', 'num': 5}, 'Hallo Jim, 5 guten Resultate')
|
2016-12-08 06:42:31 +08:00
|
|
|
with self.assertRaisesMessage(KeyError, 'Your dictionary lacks key'):
|
2013-02-02 17:56:41 +08:00
|
|
|
complex_deferred % {'name': 'Jim'}
|
|
|
|
self.assertEqual(complex_context_nonlazy % {'num': 4, 'name': 'Jim'}, 'Willkommen Jim, 4 guten Resultate')
|
|
|
|
self.assertEqual(complex_context_deferred % {'name': 'Jim', 'num': 1}, 'Willkommen Jim, 1 gutes Resultat')
|
|
|
|
self.assertEqual(complex_context_deferred % {'name': 'Jim', 'num': 5}, 'Willkommen Jim, 5 guten Resultate')
|
2016-12-08 06:42:31 +08:00
|
|
|
with self.assertRaisesMessage(KeyError, 'Your dictionary lacks key'):
|
2013-02-02 17:56:41 +08:00
|
|
|
complex_context_deferred % {'name': 'Jim'}
|
2013-01-31 03:28:16 +08:00
|
|
|
|
2018-11-26 05:50:34 +08:00
|
|
|
@override_settings(LOCALE_PATHS=extended_locale_paths)
|
|
|
|
def test_ngettext_lazy_format_style(self):
|
|
|
|
simple_with_format = ngettext_lazy('{} good result', '{} good results')
|
|
|
|
simple_context_with_format = npgettext_lazy('Exclamation', '{} good result', '{} good results')
|
|
|
|
|
|
|
|
with translation.override('de'):
|
|
|
|
self.assertEqual(simple_with_format.format(1), '1 gutes Resultat')
|
|
|
|
self.assertEqual(simple_with_format.format(4), '4 guten Resultate')
|
|
|
|
self.assertEqual(simple_context_with_format.format(1), '1 gutes Resultat!')
|
|
|
|
self.assertEqual(simple_context_with_format.format(4), '4 guten Resultate!')
|
|
|
|
|
|
|
|
complex_nonlazy = ngettext_lazy('Hi {name}, {num} good result', 'Hi {name}, {num} good results', 4)
|
|
|
|
complex_deferred = ngettext_lazy(
|
|
|
|
'Hi {name}, {num} good result', 'Hi {name}, {num} good results', 'num'
|
|
|
|
)
|
|
|
|
complex_context_nonlazy = npgettext_lazy(
|
|
|
|
'Greeting', 'Hi {name}, {num} good result', 'Hi {name}, {num} good results', 4
|
|
|
|
)
|
|
|
|
complex_context_deferred = npgettext_lazy(
|
|
|
|
'Greeting', 'Hi {name}, {num} good result', 'Hi {name}, {num} good results', 'num'
|
|
|
|
)
|
|
|
|
with translation.override('de'):
|
|
|
|
self.assertEqual(complex_nonlazy.format(num=4, name='Jim'), 'Hallo Jim, 4 guten Resultate')
|
|
|
|
self.assertEqual(complex_deferred.format(name='Jim', num=1), 'Hallo Jim, 1 gutes Resultat')
|
|
|
|
self.assertEqual(complex_deferred.format(name='Jim', num=5), 'Hallo Jim, 5 guten Resultate')
|
|
|
|
with self.assertRaisesMessage(KeyError, 'Your dictionary lacks key'):
|
|
|
|
complex_deferred.format(name='Jim')
|
|
|
|
self.assertEqual(complex_context_nonlazy.format(num=4, name='Jim'), 'Willkommen Jim, 4 guten Resultate')
|
|
|
|
self.assertEqual(complex_context_deferred.format(name='Jim', num=1), 'Willkommen Jim, 1 gutes Resultat')
|
|
|
|
self.assertEqual(complex_context_deferred.format(name='Jim', num=5), 'Willkommen Jim, 5 guten Resultate')
|
|
|
|
with self.assertRaisesMessage(KeyError, 'Your dictionary lacks key'):
|
|
|
|
complex_context_deferred.format(name='Jim')
|
|
|
|
|
2017-01-27 03:58:33 +08:00
|
|
|
def test_ngettext_lazy_bool(self):
|
|
|
|
self.assertTrue(ngettext_lazy('%d good result', '%d good results'))
|
|
|
|
self.assertFalse(ngettext_lazy('', ''))
|
2015-10-20 03:46:55 +08:00
|
|
|
|
2017-01-27 03:58:33 +08:00
|
|
|
def test_ngettext_lazy_pickle(self):
|
|
|
|
s1 = ngettext_lazy('%d good result', '%d good results')
|
2016-02-12 09:12:54 +08:00
|
|
|
self.assertEqual(s1 % 1, '1 good result')
|
|
|
|
self.assertEqual(s1 % 8, '8 good results')
|
|
|
|
s2 = pickle.loads(pickle.dumps(s1))
|
|
|
|
self.assertEqual(s2 % 1, '1 good result')
|
|
|
|
self.assertEqual(s2 % 8, '8 good results')
|
|
|
|
|
2012-10-22 20:45:41 +08:00
|
|
|
@override_settings(LOCALE_PATHS=extended_locale_paths)
|
2010-11-04 18:48:27 +08:00
|
|
|
def test_pgettext(self):
|
2019-04-12 21:15:18 +08:00
|
|
|
trans_real._active = Local()
|
2012-10-22 20:45:41 +08:00
|
|
|
trans_real._translations = {}
|
|
|
|
with translation.override('de'):
|
|
|
|
self.assertEqual(pgettext("unexisting", "May"), "May")
|
|
|
|
self.assertEqual(pgettext("month name", "May"), "Mai")
|
|
|
|
self.assertEqual(pgettext("verb", "May"), "Kann")
|
|
|
|
self.assertEqual(npgettext("search", "%d result", "%d results", 4) % 4, "4 Resultate")
|
2010-11-04 18:48:27 +08:00
|
|
|
|
2017-06-15 04:58:25 +08:00
|
|
|
def test_empty_value(self):
|
|
|
|
"""Empty value must stay empty after being translated (#23196)."""
|
|
|
|
with translation.override('de'):
|
|
|
|
self.assertEqual('', gettext(''))
|
|
|
|
s = mark_safe('')
|
|
|
|
self.assertEqual(s, gettext(s))
|
|
|
|
|
2017-06-15 14:47:07 +08:00
|
|
|
@override_settings(LOCALE_PATHS=extended_locale_paths)
|
2017-06-15 04:58:25 +08:00
|
|
|
def test_safe_status(self):
|
|
|
|
"""
|
2017-06-15 14:47:07 +08:00
|
|
|
Translating a string requiring no auto-escaping with gettext or pgettext
|
|
|
|
shouldn't change the "safe" status.
|
2017-06-15 04:58:25 +08:00
|
|
|
"""
|
2019-04-12 21:15:18 +08:00
|
|
|
trans_real._active = Local()
|
2017-06-15 14:47:07 +08:00
|
|
|
trans_real._translations = {}
|
|
|
|
s1 = mark_safe('Password')
|
|
|
|
s2 = mark_safe('May')
|
2017-06-15 04:58:25 +08:00
|
|
|
with translation.override('de', deactivate=True):
|
2019-02-05 22:38:29 +08:00
|
|
|
self.assertIs(type(gettext(s1)), SafeString)
|
|
|
|
self.assertIs(type(pgettext('month name', s2)), SafeString)
|
|
|
|
self.assertEqual('aPassword', SafeString('a') + s1)
|
|
|
|
self.assertEqual('Passworda', s1 + SafeString('a'))
|
2017-06-15 14:47:07 +08:00
|
|
|
self.assertEqual('Passworda', s1 + mark_safe('a'))
|
|
|
|
self.assertEqual('aPassword', mark_safe('a') + s1)
|
2017-06-15 04:58:25 +08:00
|
|
|
self.assertEqual('as', mark_safe('a') + mark_safe('s'))
|
|
|
|
|
|
|
|
def test_maclines(self):
|
|
|
|
"""
|
|
|
|
Translations on files with Mac or DOS end of lines will be converted
|
|
|
|
to unix EOF in .po catalogs.
|
|
|
|
"""
|
|
|
|
ca_translation = trans_real.translation('ca')
|
|
|
|
ca_translation._catalog['Mac\nEOF\n'] = 'Catalan Mac\nEOF\n'
|
|
|
|
ca_translation._catalog['Win\nEOF\n'] = 'Catalan Win\nEOF\n'
|
|
|
|
with translation.override('ca', deactivate=True):
|
|
|
|
self.assertEqual('Catalan Mac\nEOF\n', gettext('Mac\rEOF\r'))
|
|
|
|
self.assertEqual('Catalan Win\nEOF\n', gettext('Win\r\nEOF\r\n'))
|
|
|
|
|
|
|
|
def test_to_locale(self):
|
2017-09-07 21:53:16 +08:00
|
|
|
tests = (
|
|
|
|
('en', 'en'),
|
|
|
|
('EN', 'en'),
|
|
|
|
('en-us', 'en_US'),
|
|
|
|
('EN-US', 'en_US'),
|
2021-03-22 02:03:57 +08:00
|
|
|
('en_US', 'en_US'),
|
2017-09-07 21:53:16 +08:00
|
|
|
# With > 2 characters after the dash.
|
|
|
|
('sr-latn', 'sr_Latn'),
|
|
|
|
('sr-LATN', 'sr_Latn'),
|
2021-03-22 02:03:57 +08:00
|
|
|
('sr_Latn', 'sr_Latn'),
|
2021-03-22 13:54:26 +08:00
|
|
|
# 3-char language codes.
|
|
|
|
('ber-MA', 'ber_MA'),
|
|
|
|
('BER-MA', 'ber_MA'),
|
2021-03-22 02:03:57 +08:00
|
|
|
('BER_MA', 'ber_MA'),
|
|
|
|
('ber_MA', 'ber_MA'),
|
2017-09-07 21:53:16 +08:00
|
|
|
# With private use subtag (x-informal).
|
2017-09-07 22:09:07 +08:00
|
|
|
('nl-nl-x-informal', 'nl_NL-x-informal'),
|
|
|
|
('NL-NL-X-INFORMAL', 'nl_NL-x-informal'),
|
2017-09-07 21:53:16 +08:00
|
|
|
('sr-latn-x-informal', 'sr_Latn-x-informal'),
|
|
|
|
('SR-LATN-X-INFORMAL', 'sr_Latn-x-informal'),
|
|
|
|
)
|
|
|
|
for lang, locale in tests:
|
|
|
|
with self.subTest(lang=lang):
|
|
|
|
self.assertEqual(to_locale(lang), locale)
|
2017-06-15 04:58:25 +08:00
|
|
|
|
|
|
|
def test_to_language(self):
|
2018-05-13 03:03:38 +08:00
|
|
|
self.assertEqual(to_language('en_US'), 'en-us')
|
|
|
|
self.assertEqual(to_language('sr_Lat'), 'sr-lat')
|
2017-06-15 04:58:25 +08:00
|
|
|
|
|
|
|
def test_language_bidi(self):
|
|
|
|
self.assertIs(get_language_bidi(), False)
|
|
|
|
with translation.override(None):
|
|
|
|
self.assertIs(get_language_bidi(), False)
|
|
|
|
|
2018-05-11 03:48:00 +08:00
|
|
|
def test_language_bidi_null(self):
|
|
|
|
self.assertIs(trans_null.get_language_bidi(), False)
|
|
|
|
with override_settings(LANGUAGE_CODE='he'):
|
|
|
|
self.assertIs(get_language_bidi(), True)
|
|
|
|
|
2010-01-02 05:38:34 +08:00
|
|
|
|
2020-05-28 16:26:41 +08:00
|
|
|
class TranslationLoadingTests(SimpleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
"""Clear translation state."""
|
|
|
|
self._old_language = get_language()
|
|
|
|
self._old_translations = trans_real._translations
|
|
|
|
deactivate()
|
|
|
|
trans_real._translations = {}
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
trans_real._translations = self._old_translations
|
|
|
|
activate(self._old_language)
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
USE_I18N=True,
|
|
|
|
LANGUAGE_CODE='en',
|
|
|
|
LANGUAGES=[
|
|
|
|
('en', 'English'),
|
|
|
|
('en-ca', 'English (Canada)'),
|
|
|
|
('en-nz', 'English (New Zealand)'),
|
|
|
|
('en-au', 'English (Australia)'),
|
|
|
|
],
|
|
|
|
LOCALE_PATHS=[os.path.join(here, 'loading')],
|
|
|
|
INSTALLED_APPS=['i18n.loading_app'],
|
|
|
|
)
|
|
|
|
def test_translation_loading(self):
|
|
|
|
"""
|
|
|
|
"loading_app" does not have translations for all languages provided by
|
|
|
|
"loading". Catalogs are merged correctly.
|
|
|
|
"""
|
|
|
|
tests = [
|
|
|
|
('en', 'local country person'),
|
|
|
|
('en_AU', 'aussie'),
|
|
|
|
('en_NZ', 'kiwi'),
|
|
|
|
('en_CA', 'canuck'),
|
|
|
|
]
|
|
|
|
# Load all relevant translations.
|
|
|
|
for language, _ in tests:
|
|
|
|
activate(language)
|
|
|
|
# Catalogs are merged correctly.
|
|
|
|
for language, nickname in tests:
|
|
|
|
with self.subTest(language=language):
|
|
|
|
activate(language)
|
|
|
|
self.assertEqual(gettext('local country person'), nickname)
|
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class TranslationThreadSafetyTests(SimpleTestCase):
|
2013-05-19 17:44:46 +08:00
|
|
|
|
2013-05-18 23:36:31 +08:00
|
|
|
def setUp(self):
|
|
|
|
self._old_language = get_language()
|
|
|
|
self._translations = trans_real._translations
|
|
|
|
|
|
|
|
# here we rely on .split() being called inside the _fetch()
|
|
|
|
# in trans_real.translation()
|
|
|
|
class sideeffect_str(str):
|
|
|
|
def split(self, *args, **kwargs):
|
|
|
|
res = str.split(self, *args, **kwargs)
|
|
|
|
trans_real._translations['en-YY'] = None
|
|
|
|
return res
|
|
|
|
|
|
|
|
trans_real._translations = {sideeffect_str('en-XX'): None}
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
trans_real._translations = self._translations
|
|
|
|
activate(self._old_language)
|
|
|
|
|
|
|
|
def test_bug14894_translation_activate_thread_safety(self):
|
|
|
|
translation_count = len(trans_real._translations)
|
2016-06-28 23:21:26 +08:00
|
|
|
# May raise RuntimeError if translation.activate() isn't thread-safe.
|
|
|
|
translation.activate('pl')
|
2013-05-18 23:36:31 +08:00
|
|
|
# make sure sideeffect_str actually added a new translation
|
|
|
|
self.assertLess(translation_count, len(trans_real._translations))
|
|
|
|
|
|
|
|
|
2013-02-15 16:36:07 +08:00
|
|
|
@override_settings(USE_L10N=True)
|
2015-04-18 05:38:20 +08:00
|
|
|
class FormattingTests(SimpleTestCase):
|
2009-12-24 22:23:52 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUp()
|
2009-12-24 22:23:52 +08:00
|
|
|
self.n = decimal.Decimal('66666.666')
|
|
|
|
self.f = 99999.999
|
|
|
|
self.d = datetime.date(2009, 12, 31)
|
|
|
|
self.dt = datetime.datetime(2009, 12, 31, 20, 50)
|
2010-01-02 05:36:36 +08:00
|
|
|
self.t = datetime.time(10, 15, 48)
|
2016-12-01 18:38:01 +08:00
|
|
|
self.long = 10000
|
2009-12-24 22:23:52 +08:00
|
|
|
self.ctxt = Context({
|
|
|
|
'n': self.n,
|
2010-01-02 05:36:36 +08:00
|
|
|
't': self.t,
|
2009-12-24 22:23:52 +08:00
|
|
|
'd': self.d,
|
|
|
|
'dt': self.dt,
|
2010-09-28 00:21:16 +08:00
|
|
|
'f': self.f,
|
2016-11-15 06:40:28 +08:00
|
|
|
'l': self.long,
|
2009-12-24 22:23:52 +08:00
|
|
|
})
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2017-10-14 00:37:31 +08:00
|
|
|
def test_all_format_strings(self):
|
|
|
|
all_locales = LANG_INFO.keys()
|
2017-10-15 02:46:57 +08:00
|
|
|
some_date = datetime.date(2017, 10, 14)
|
|
|
|
some_datetime = datetime.datetime(2017, 10, 14, 10, 23)
|
2017-10-14 00:37:31 +08:00
|
|
|
for locale in all_locales:
|
|
|
|
with self.subTest(locale=locale), translation.override(locale):
|
2017-10-15 02:46:57 +08:00
|
|
|
self.assertIn('2017', date_format(some_date)) # Uses DATE_FORMAT by default
|
|
|
|
self.assertIn('23', time_format(some_datetime)) # Uses TIME_FORMAT by default
|
|
|
|
self.assertIn('2017', date_format(some_datetime, format=get_format('DATETIME_FORMAT')))
|
|
|
|
self.assertIn('2017', date_format(some_date, format=get_format('YEAR_MONTH_FORMAT')))
|
|
|
|
self.assertIn('14', date_format(some_date, format=get_format('MONTH_DAY_FORMAT')))
|
|
|
|
self.assertIn('2017', date_format(some_date, format=get_format('SHORT_DATE_FORMAT')))
|
|
|
|
self.assertIn('2017', date_format(some_datetime, format=get_format('SHORT_DATETIME_FORMAT')))
|
2017-10-14 00:37:31 +08:00
|
|
|
|
2009-12-24 22:23:52 +08:00
|
|
|
def test_locale_independent(self):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-01-02 05:36:36 +08:00
|
|
|
Localization of numbers
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2013-02-15 16:36:07 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=False):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('66666.66', nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=','))
|
|
|
|
self.assertEqual('66666A6', nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B'))
|
|
|
|
self.assertEqual('66666', nformat(self.n, decimal_sep='X', decimal_pos=0, grouping=1, thousand_sep='Y'))
|
2011-05-06 21:29:44 +08:00
|
|
|
|
2013-02-15 16:36:07 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'66,666.66',
|
|
|
|
nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=',')
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
'6B6B6B6B6A6',
|
|
|
|
nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B')
|
|
|
|
)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('-66666.6', nformat(-66666.666, decimal_sep='.', decimal_pos=1))
|
|
|
|
self.assertEqual('-66666.0', nformat(int('-66666'), decimal_sep='.', decimal_pos=1))
|
2016-11-15 06:40:28 +08:00
|
|
|
self.assertEqual('10000.0', nformat(self.long, decimal_sep='.', decimal_pos=1))
|
2016-06-03 03:13:47 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'10,00,00,000.00',
|
|
|
|
nformat(100000000.00, decimal_sep='.', decimal_pos=2, grouping=(3, 2, 0), thousand_sep=',')
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
'1,0,00,000,0000.00',
|
|
|
|
nformat(10000000000.00, decimal_sep='.', decimal_pos=2, grouping=(4, 3, 2, 1, 0), thousand_sep=',')
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
'10000,00,000.00',
|
|
|
|
nformat(1000000000.00, decimal_sep='.', decimal_pos=2, grouping=(3, 2, -1), thousand_sep=',')
|
|
|
|
)
|
2011-12-24 19:15:26 +08:00
|
|
|
# This unusual grouping/force_grouping combination may be triggered by the intcomma filter (#17414)
|
2016-11-15 06:40:28 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'10000',
|
|
|
|
nformat(self.long, decimal_sep='.', decimal_pos=0, grouping=0, force_grouping=True)
|
|
|
|
)
|
2011-05-06 21:29:44 +08:00
|
|
|
# date filter
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('31.12.2009 в 20:50', Template('{{ dt|date:"d.m.Y в H:i" }}').render(self.ctxt))
|
|
|
|
self.assertEqual('⌚ 10:15', Template('{{ t|time:"⌚ H:i" }}').render(self.ctxt))
|
2010-01-02 05:38:58 +08:00
|
|
|
|
2013-02-15 16:36:07 +08:00
|
|
|
@override_settings(USE_L10N=False)
|
2009-12-24 22:23:52 +08:00
|
|
|
def test_l10n_disabled(self):
|
|
|
|
"""
|
|
|
|
Catalan locale with format i18n disabled translations will be used,
|
|
|
|
but not formats
|
|
|
|
"""
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('ca', deactivate=True):
|
2013-11-01 00:27:21 +08:00
|
|
|
self.maxDiff = 3000
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('N j, Y', get_format('DATE_FORMAT'))
|
2009-12-24 22:23:52 +08:00
|
|
|
self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('.', get_format('DECIMAL_SEPARATOR'))
|
|
|
|
self.assertEqual('10:15 a.m.', time_format(self.t))
|
2021-04-06 12:17:47 +08:00
|
|
|
self.assertEqual('Des. 31, 2009', date_format(self.d))
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('desembre 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
|
|
|
|
self.assertEqual('12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
|
|
|
|
self.assertEqual('No localizable', localize('No localizable'))
|
|
|
|
self.assertEqual('66666.666', localize(self.n))
|
|
|
|
self.assertEqual('99999.999', localize(self.f))
|
2016-11-15 06:40:28 +08:00
|
|
|
self.assertEqual('10000', localize(self.long))
|
2021-04-06 12:17:47 +08:00
|
|
|
self.assertEqual('Des. 31, 2009', localize(self.d))
|
|
|
|
self.assertEqual('Des. 31, 2009, 8:50 p.m.', localize(self.dt))
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('66666.666', Template('{{ n }}').render(self.ctxt))
|
|
|
|
self.assertEqual('99999.999', Template('{{ f }}').render(self.ctxt))
|
2021-04-06 12:17:47 +08:00
|
|
|
self.assertEqual('Des. 31, 2009', Template('{{ d }}').render(self.ctxt))
|
|
|
|
self.assertEqual('Des. 31, 2009, 8:50 p.m.', Template('{{ dt }}').render(self.ctxt))
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
|
|
|
|
self.assertEqual('100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
|
2020-10-12 11:34:32 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'66666.67',
|
|
|
|
Template('{{ n|floatformat:"2g" }}').render(self.ctxt),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
'100000.0',
|
|
|
|
Template('{{ f|floatformat:"g" }}').render(self.ctxt),
|
|
|
|
)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('10:15 a.m.', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
|
|
|
|
self.assertEqual('12/31/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'12/31/2009 8:50 p.m.', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt)
|
|
|
|
)
|
2009-12-24 22:23:52 +08:00
|
|
|
|
|
|
|
form = I18nForm({
|
2012-06-08 00:08:47 +08:00
|
|
|
'decimal_field': '66666,666',
|
|
|
|
'float_field': '99999,999',
|
|
|
|
'date_field': '31/12/2009',
|
|
|
|
'datetime_field': '31/12/2009 20:50',
|
|
|
|
'time_field': '20:50',
|
|
|
|
'integer_field': '1.234',
|
2009-12-24 22:23:52 +08:00
|
|
|
})
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertFalse(form.is_valid())
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(['Introdu\xefu un n\xfamero.'], form.errors['float_field'])
|
|
|
|
self.assertEqual(['Introdu\xefu un n\xfamero.'], form.errors['decimal_field'])
|
|
|
|
self.assertEqual(['Introdu\xefu una data v\xe0lida.'], form.errors['date_field'])
|
|
|
|
self.assertEqual(['Introdu\xefu una data/hora v\xe0lides.'], form.errors['datetime_field'])
|
2021-04-06 12:17:47 +08:00
|
|
|
self.assertEqual(['Introdu\xefu un n\xfamero enter.'], form.errors['integer_field'])
|
2009-12-24 22:23:52 +08:00
|
|
|
|
|
|
|
form2 = SelectDateForm({
|
2012-06-08 00:08:47 +08:00
|
|
|
'date_field_month': '12',
|
|
|
|
'date_field_day': '31',
|
|
|
|
'date_field_year': '2009'
|
2009-12-24 22:23:52 +08:00
|
|
|
})
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertTrue(form2.is_valid())
|
2009-12-24 22:23:52 +08:00
|
|
|
self.assertEqual(datetime.date(2009, 12, 31), form2.cleaned_data['date_field'])
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
'<select name="mydate_month" id="id_mydate_month">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="1">gener</option>'
|
|
|
|
'<option value="2">febrer</option>'
|
|
|
|
'<option value="3">mar\xe7</option>'
|
|
|
|
'<option value="4">abril</option>'
|
|
|
|
'<option value="5">maig</option>'
|
|
|
|
'<option value="6">juny</option>'
|
|
|
|
'<option value="7">juliol</option>'
|
|
|
|
'<option value="8">agost</option>'
|
|
|
|
'<option value="9">setembre</option>'
|
|
|
|
'<option value="10">octubre</option>'
|
|
|
|
'<option value="11">novembre</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="12" selected>desembre</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'</select>'
|
|
|
|
'<select name="mydate_day" id="id_mydate_day">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="1">1</option>'
|
|
|
|
'<option value="2">2</option>'
|
|
|
|
'<option value="3">3</option>'
|
|
|
|
'<option value="4">4</option>'
|
|
|
|
'<option value="5">5</option>'
|
|
|
|
'<option value="6">6</option>'
|
|
|
|
'<option value="7">7</option>'
|
|
|
|
'<option value="8">8</option>'
|
|
|
|
'<option value="9">9</option>'
|
|
|
|
'<option value="10">10</option>'
|
|
|
|
'<option value="11">11</option>'
|
|
|
|
'<option value="12">12</option>'
|
|
|
|
'<option value="13">13</option>'
|
|
|
|
'<option value="14">14</option>'
|
|
|
|
'<option value="15">15</option>'
|
|
|
|
'<option value="16">16</option>'
|
|
|
|
'<option value="17">17</option>'
|
|
|
|
'<option value="18">18</option>'
|
|
|
|
'<option value="19">19</option>'
|
|
|
|
'<option value="20">20</option>'
|
|
|
|
'<option value="21">21</option>'
|
|
|
|
'<option value="22">22</option>'
|
|
|
|
'<option value="23">23</option>'
|
|
|
|
'<option value="24">24</option>'
|
|
|
|
'<option value="25">25</option>'
|
|
|
|
'<option value="26">26</option>'
|
|
|
|
'<option value="27">27</option>'
|
|
|
|
'<option value="28">28</option>'
|
|
|
|
'<option value="29">29</option>'
|
|
|
|
'<option value="30">30</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="31" selected>31</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'</select>'
|
|
|
|
'<select name="mydate_year" id="id_mydate_year">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="2009" selected>2009</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="2010">2010</option>'
|
|
|
|
'<option value="2011">2011</option>'
|
|
|
|
'<option value="2012">2012</option>'
|
|
|
|
'<option value="2013">2013</option>'
|
|
|
|
'<option value="2014">2014</option>'
|
|
|
|
'<option value="2015">2015</option>'
|
|
|
|
'<option value="2016">2016</option>'
|
|
|
|
'<option value="2017">2017</option>'
|
|
|
|
'<option value="2018">2018</option>'
|
|
|
|
'</select>',
|
2015-01-26 11:28:57 +08:00
|
|
|
forms.SelectDateWidget(years=range(2009, 2019)).render('mydate', datetime.date(2009, 12, 31))
|
2009-12-24 22:23:52 +08:00
|
|
|
)
|
2010-03-20 23:26:41 +08:00
|
|
|
|
|
|
|
# We shouldn't change the behavior of the floatformat filter re:
|
|
|
|
# thousand separator and grouping when USE_L10N is False even
|
|
|
|
# if the USE_THOUSAND_SEPARATOR, NUMBER_GROUPING and
|
|
|
|
# THOUSAND_SEPARATOR settings are specified
|
2016-04-08 10:04:45 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True, NUMBER_GROUPING=1, THOUSAND_SEPARATOR='!'):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
|
|
|
|
self.assertEqual('100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
|
2009-12-24 22:23:52 +08:00
|
|
|
|
2011-10-20 20:14:06 +08:00
|
|
|
def test_false_like_locale_formats(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The active locale's formats take precedence over the default settings
|
|
|
|
even if they would be interpreted as False in a conditional test
|
|
|
|
(e.g. 0 or empty string) (#16938).
|
2011-10-20 20:14:06 +08:00
|
|
|
"""
|
2016-12-02 04:22:16 +08:00
|
|
|
with translation.override('fr'):
|
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True, THOUSAND_SEPARATOR='!'):
|
|
|
|
self.assertEqual('\xa0', get_format('THOUSAND_SEPARATOR'))
|
|
|
|
# Even a second time (after the format has been cached)...
|
|
|
|
self.assertEqual('\xa0', get_format('THOUSAND_SEPARATOR'))
|
|
|
|
|
|
|
|
with self.settings(FIRST_DAY_OF_WEEK=0):
|
|
|
|
self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK'))
|
|
|
|
# Even a second time (after the format has been cached)...
|
|
|
|
self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK'))
|
2011-10-20 20:14:06 +08:00
|
|
|
|
2009-12-24 22:23:52 +08:00
|
|
|
def test_l10n_enabled(self):
|
2013-11-01 00:27:21 +08:00
|
|
|
self.maxDiff = 3000
|
2011-02-04 23:45:52 +08:00
|
|
|
# Catalan locale
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('ca', deactivate=True):
|
2016-09-17 00:15:00 +08:00
|
|
|
self.assertEqual(r'j \d\e F \d\e Y', get_format('DATE_FORMAT'))
|
2009-12-24 22:23:52 +08:00
|
|
|
self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK'))
|
|
|
|
self.assertEqual(',', get_format('DECIMAL_SEPARATOR'))
|
2014-09-11 20:43:49 +08:00
|
|
|
self.assertEqual('10:15', time_format(self.t))
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('31 de desembre de 2009', date_format(self.d))
|
|
|
|
self.assertEqual('desembre del 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
|
|
|
|
self.assertEqual('31/12/2009 20:50', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
|
2009-12-24 22:23:52 +08:00
|
|
|
self.assertEqual('No localizable', localize('No localizable'))
|
|
|
|
|
2013-02-15 16:36:07 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
|
|
|
self.assertEqual('66.666,666', localize(self.n))
|
|
|
|
self.assertEqual('99.999,999', localize(self.f))
|
2016-11-15 06:40:28 +08:00
|
|
|
self.assertEqual('10.000', localize(self.long))
|
2013-02-15 16:36:07 +08:00
|
|
|
self.assertEqual('True', localize(True))
|
|
|
|
|
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=False):
|
|
|
|
self.assertEqual('66666,666', localize(self.n))
|
|
|
|
self.assertEqual('99999,999', localize(self.f))
|
2016-11-15 06:40:28 +08:00
|
|
|
self.assertEqual('10000', localize(self.long))
|
2013-02-15 16:36:07 +08:00
|
|
|
self.assertEqual('31 de desembre de 2009', localize(self.d))
|
|
|
|
self.assertEqual('31 de desembre de 2009 a les 20:50', localize(self.dt))
|
2009-12-24 22:23:52 +08:00
|
|
|
|
2013-02-15 16:36:07 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
|
|
|
self.assertEqual('66.666,666', Template('{{ n }}').render(self.ctxt))
|
|
|
|
self.assertEqual('99.999,999', Template('{{ f }}').render(self.ctxt))
|
|
|
|
self.assertEqual('10.000', Template('{{ l }}').render(self.ctxt))
|
2009-12-24 22:23:52 +08:00
|
|
|
|
2013-02-15 16:36:07 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
|
|
|
form3 = I18nForm({
|
|
|
|
'decimal_field': '66.666,666',
|
|
|
|
'float_field': '99.999,999',
|
|
|
|
'date_field': '31/12/2009',
|
|
|
|
'datetime_field': '31/12/2009 20:50',
|
|
|
|
'time_field': '20:50',
|
|
|
|
'integer_field': '1.234',
|
|
|
|
})
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertTrue(form3.is_valid())
|
2013-02-15 16:36:07 +08:00
|
|
|
self.assertEqual(decimal.Decimal('66666.666'), form3.cleaned_data['decimal_field'])
|
|
|
|
self.assertEqual(99999.999, form3.cleaned_data['float_field'])
|
|
|
|
self.assertEqual(datetime.date(2009, 12, 31), form3.cleaned_data['date_field'])
|
|
|
|
self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), form3.cleaned_data['datetime_field'])
|
|
|
|
self.assertEqual(datetime.time(20, 50), form3.cleaned_data['time_field'])
|
|
|
|
self.assertEqual(1234, form3.cleaned_data['integer_field'])
|
|
|
|
|
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=False):
|
|
|
|
self.assertEqual('66666,666', Template('{{ n }}').render(self.ctxt))
|
|
|
|
self.assertEqual('99999,999', Template('{{ f }}').render(self.ctxt))
|
|
|
|
self.assertEqual('31 de desembre de 2009', Template('{{ d }}').render(self.ctxt))
|
|
|
|
self.assertEqual('31 de desembre de 2009 a les 20:50', Template('{{ dt }}').render(self.ctxt))
|
|
|
|
self.assertEqual('66666,67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
|
|
|
|
self.assertEqual('100000,0', Template('{{ f|floatformat }}').render(self.ctxt))
|
2020-10-12 11:34:32 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'66.666,67',
|
|
|
|
Template('{{ n|floatformat:"2g" }}').render(self.ctxt),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
'100.000,0',
|
|
|
|
Template('{{ f|floatformat:"g" }}').render(self.ctxt),
|
|
|
|
)
|
2014-09-11 20:43:49 +08:00
|
|
|
self.assertEqual('10:15', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
|
2013-02-15 16:36:07 +08:00
|
|
|
self.assertEqual('31/12/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'31/12/2009 20:50',
|
|
|
|
Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt)
|
|
|
|
)
|
2013-02-15 16:36:07 +08:00
|
|
|
self.assertEqual(date_format(datetime.datetime.now(), "DATE_FORMAT"),
|
|
|
|
Template('{% now "DATE_FORMAT" %}').render(self.ctxt))
|
|
|
|
|
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=False):
|
|
|
|
form4 = I18nForm({
|
|
|
|
'decimal_field': '66666,666',
|
|
|
|
'float_field': '99999,999',
|
|
|
|
'date_field': '31/12/2009',
|
|
|
|
'datetime_field': '31/12/2009 20:50',
|
|
|
|
'time_field': '20:50',
|
|
|
|
'integer_field': '1234',
|
|
|
|
})
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertTrue(form4.is_valid())
|
2013-02-15 16:36:07 +08:00
|
|
|
self.assertEqual(decimal.Decimal('66666.666'), form4.cleaned_data['decimal_field'])
|
|
|
|
self.assertEqual(99999.999, form4.cleaned_data['float_field'])
|
|
|
|
self.assertEqual(datetime.date(2009, 12, 31), form4.cleaned_data['date_field'])
|
|
|
|
self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), form4.cleaned_data['datetime_field'])
|
|
|
|
self.assertEqual(datetime.time(20, 50), form4.cleaned_data['time_field'])
|
|
|
|
self.assertEqual(1234, form4.cleaned_data['integer_field'])
|
2009-12-24 22:23:52 +08:00
|
|
|
|
2010-03-01 18:19:24 +08:00
|
|
|
form5 = SelectDateForm({
|
2012-06-08 00:08:47 +08:00
|
|
|
'date_field_month': '12',
|
|
|
|
'date_field_day': '31',
|
|
|
|
'date_field_year': '2009'
|
2009-12-24 22:23:52 +08:00
|
|
|
})
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertTrue(form5.is_valid())
|
2010-03-01 18:19:24 +08:00
|
|
|
self.assertEqual(datetime.date(2009, 12, 31), form5.cleaned_data['date_field'])
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
'<select name="mydate_day" id="id_mydate_day">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="1">1</option>'
|
|
|
|
'<option value="2">2</option>'
|
|
|
|
'<option value="3">3</option>'
|
|
|
|
'<option value="4">4</option>'
|
|
|
|
'<option value="5">5</option>'
|
|
|
|
'<option value="6">6</option>'
|
|
|
|
'<option value="7">7</option>'
|
|
|
|
'<option value="8">8</option>'
|
|
|
|
'<option value="9">9</option>'
|
|
|
|
'<option value="10">10</option>'
|
|
|
|
'<option value="11">11</option>'
|
|
|
|
'<option value="12">12</option>'
|
|
|
|
'<option value="13">13</option>'
|
|
|
|
'<option value="14">14</option>'
|
|
|
|
'<option value="15">15</option>'
|
|
|
|
'<option value="16">16</option>'
|
|
|
|
'<option value="17">17</option>'
|
|
|
|
'<option value="18">18</option>'
|
|
|
|
'<option value="19">19</option>'
|
|
|
|
'<option value="20">20</option>'
|
|
|
|
'<option value="21">21</option>'
|
|
|
|
'<option value="22">22</option>'
|
|
|
|
'<option value="23">23</option>'
|
|
|
|
'<option value="24">24</option>'
|
|
|
|
'<option value="25">25</option>'
|
|
|
|
'<option value="26">26</option>'
|
|
|
|
'<option value="27">27</option>'
|
|
|
|
'<option value="28">28</option>'
|
|
|
|
'<option value="29">29</option>'
|
|
|
|
'<option value="30">30</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="31" selected>31</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'</select>'
|
|
|
|
'<select name="mydate_month" id="id_mydate_month">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="1">gener</option>'
|
|
|
|
'<option value="2">febrer</option>'
|
|
|
|
'<option value="3">mar\xe7</option>'
|
|
|
|
'<option value="4">abril</option>'
|
|
|
|
'<option value="5">maig</option>'
|
|
|
|
'<option value="6">juny</option>'
|
|
|
|
'<option value="7">juliol</option>'
|
|
|
|
'<option value="8">agost</option>'
|
|
|
|
'<option value="9">setembre</option>'
|
|
|
|
'<option value="10">octubre</option>'
|
|
|
|
'<option value="11">novembre</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="12" selected>desembre</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'</select>'
|
|
|
|
'<select name="mydate_year" id="id_mydate_year">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="2009" selected>2009</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="2010">2010</option>'
|
|
|
|
'<option value="2011">2011</option>'
|
|
|
|
'<option value="2012">2012</option>'
|
|
|
|
'<option value="2013">2013</option>'
|
|
|
|
'<option value="2014">2014</option>'
|
|
|
|
'<option value="2015">2015</option>'
|
|
|
|
'<option value="2016">2016</option>'
|
|
|
|
'<option value="2017">2017</option>'
|
|
|
|
'<option value="2018">2018</option>'
|
|
|
|
'</select>',
|
2015-01-26 11:28:57 +08:00
|
|
|
forms.SelectDateWidget(years=range(2009, 2019)).render('mydate', datetime.date(2009, 12, 31))
|
2009-12-24 22:23:52 +08:00
|
|
|
)
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2010-12-22 08:44:54 +08:00
|
|
|
# Russian locale (with E as month)
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('ru', deactivate=True):
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
'<select name="mydate_day" id="id_mydate_day">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="1">1</option>'
|
|
|
|
'<option value="2">2</option>'
|
|
|
|
'<option value="3">3</option>'
|
|
|
|
'<option value="4">4</option>'
|
|
|
|
'<option value="5">5</option>'
|
|
|
|
'<option value="6">6</option>'
|
|
|
|
'<option value="7">7</option>'
|
|
|
|
'<option value="8">8</option>'
|
|
|
|
'<option value="9">9</option>'
|
|
|
|
'<option value="10">10</option>'
|
|
|
|
'<option value="11">11</option>'
|
|
|
|
'<option value="12">12</option>'
|
|
|
|
'<option value="13">13</option>'
|
|
|
|
'<option value="14">14</option>'
|
|
|
|
'<option value="15">15</option>'
|
|
|
|
'<option value="16">16</option>'
|
|
|
|
'<option value="17">17</option>'
|
|
|
|
'<option value="18">18</option>'
|
|
|
|
'<option value="19">19</option>'
|
|
|
|
'<option value="20">20</option>'
|
|
|
|
'<option value="21">21</option>'
|
|
|
|
'<option value="22">22</option>'
|
|
|
|
'<option value="23">23</option>'
|
|
|
|
'<option value="24">24</option>'
|
|
|
|
'<option value="25">25</option>'
|
|
|
|
'<option value="26">26</option>'
|
|
|
|
'<option value="27">27</option>'
|
|
|
|
'<option value="28">28</option>'
|
|
|
|
'<option value="29">29</option>'
|
|
|
|
'<option value="30">30</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="31" selected>31</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'</select>'
|
|
|
|
'<select name="mydate_month" id="id_mydate_month">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="1">\u042f\u043d\u0432\u0430\u0440\u044c</option>'
|
|
|
|
'<option value="2">\u0424\u0435\u0432\u0440\u0430\u043b\u044c</option>'
|
|
|
|
'<option value="3">\u041c\u0430\u0440\u0442</option>'
|
|
|
|
'<option value="4">\u0410\u043f\u0440\u0435\u043b\u044c</option>'
|
|
|
|
'<option value="5">\u041c\u0430\u0439</option>'
|
|
|
|
'<option value="6">\u0418\u044e\u043d\u044c</option>'
|
|
|
|
'<option value="7">\u0418\u044e\u043b\u044c</option>'
|
|
|
|
'<option value="8">\u0410\u0432\u0433\u0443\u0441\u0442</option>'
|
|
|
|
'<option value="9">\u0421\u0435\u043d\u0442\u044f\u0431\u0440\u044c</option>'
|
|
|
|
'<option value="10">\u041e\u043a\u0442\u044f\u0431\u0440\u044c</option>'
|
|
|
|
'<option value="11">\u041d\u043e\u044f\u0431\u0440\u044c</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="12" selected>\u0414\u0435\u043a\u0430\u0431\u0440\u044c</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'</select>'
|
|
|
|
'<select name="mydate_year" id="id_mydate_year">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="2009" selected>2009</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="2010">2010</option>'
|
|
|
|
'<option value="2011">2011</option>'
|
|
|
|
'<option value="2012">2012</option>'
|
|
|
|
'<option value="2013">2013</option>'
|
|
|
|
'<option value="2014">2014</option>'
|
|
|
|
'<option value="2015">2015</option>'
|
|
|
|
'<option value="2016">2016</option>'
|
|
|
|
'<option value="2017">2017</option>'
|
|
|
|
'<option value="2018">2018</option>'
|
|
|
|
'</select>',
|
2015-01-26 11:28:57 +08:00
|
|
|
forms.SelectDateWidget(years=range(2009, 2019)).render('mydate', datetime.date(2009, 12, 31))
|
2011-02-04 23:45:52 +08:00
|
|
|
)
|
2010-12-22 08:44:54 +08:00
|
|
|
|
2009-12-23 01:58:49 +08:00
|
|
|
# English locale
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('en', deactivate=True):
|
2009-12-24 22:23:52 +08:00
|
|
|
self.assertEqual('N j, Y', get_format('DATE_FORMAT'))
|
|
|
|
self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
|
|
|
|
self.assertEqual('.', get_format('DECIMAL_SEPARATOR'))
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('Dec. 31, 2009', date_format(self.d))
|
|
|
|
self.assertEqual('December 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
|
|
|
|
self.assertEqual('12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
|
|
|
|
self.assertEqual('No localizable', localize('No localizable'))
|
2009-12-24 22:23:52 +08:00
|
|
|
|
2013-02-15 16:36:07 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
|
|
|
self.assertEqual('66,666.666', localize(self.n))
|
|
|
|
self.assertEqual('99,999.999', localize(self.f))
|
2016-11-15 06:40:28 +08:00
|
|
|
self.assertEqual('10,000', localize(self.long))
|
2009-12-24 22:23:52 +08:00
|
|
|
|
2013-02-15 16:36:07 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=False):
|
|
|
|
self.assertEqual('66666.666', localize(self.n))
|
|
|
|
self.assertEqual('99999.999', localize(self.f))
|
2016-11-15 06:40:28 +08:00
|
|
|
self.assertEqual('10000', localize(self.long))
|
2013-02-15 16:36:07 +08:00
|
|
|
self.assertEqual('Dec. 31, 2009', localize(self.d))
|
|
|
|
self.assertEqual('Dec. 31, 2009, 8:50 p.m.', localize(self.dt))
|
2009-12-24 22:23:52 +08:00
|
|
|
|
2013-02-15 16:36:07 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
|
|
|
self.assertEqual('66,666.666', Template('{{ n }}').render(self.ctxt))
|
|
|
|
self.assertEqual('99,999.999', Template('{{ f }}').render(self.ctxt))
|
|
|
|
self.assertEqual('10,000', Template('{{ l }}').render(self.ctxt))
|
|
|
|
|
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=False):
|
|
|
|
self.assertEqual('66666.666', Template('{{ n }}').render(self.ctxt))
|
|
|
|
self.assertEqual('99999.999', Template('{{ f }}').render(self.ctxt))
|
|
|
|
self.assertEqual('Dec. 31, 2009', Template('{{ d }}').render(self.ctxt))
|
|
|
|
self.assertEqual('Dec. 31, 2009, 8:50 p.m.', Template('{{ dt }}').render(self.ctxt))
|
|
|
|
self.assertEqual('66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
|
|
|
|
self.assertEqual('100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
|
2020-10-12 11:34:32 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'66,666.67',
|
|
|
|
Template('{{ n|floatformat:"2g" }}').render(self.ctxt),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
'100,000.0',
|
|
|
|
Template('{{ f|floatformat:"g" }}').render(self.ctxt),
|
|
|
|
)
|
2013-02-15 16:36:07 +08:00
|
|
|
self.assertEqual('12/31/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'12/31/2009 8:50 p.m.',
|
|
|
|
Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt)
|
|
|
|
)
|
2009-12-24 22:23:52 +08:00
|
|
|
|
|
|
|
form5 = I18nForm({
|
2012-06-08 00:08:47 +08:00
|
|
|
'decimal_field': '66666.666',
|
|
|
|
'float_field': '99999.999',
|
|
|
|
'date_field': '12/31/2009',
|
|
|
|
'datetime_field': '12/31/2009 20:50',
|
|
|
|
'time_field': '20:50',
|
|
|
|
'integer_field': '1234',
|
2009-12-24 22:23:52 +08:00
|
|
|
})
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertTrue(form5.is_valid())
|
2009-12-24 22:23:52 +08:00
|
|
|
self.assertEqual(decimal.Decimal('66666.666'), form5.cleaned_data['decimal_field'])
|
|
|
|
self.assertEqual(99999.999, form5.cleaned_data['float_field'])
|
|
|
|
self.assertEqual(datetime.date(2009, 12, 31), form5.cleaned_data['date_field'])
|
|
|
|
self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), form5.cleaned_data['datetime_field'])
|
|
|
|
self.assertEqual(datetime.time(20, 50), form5.cleaned_data['time_field'])
|
2010-03-01 18:19:24 +08:00
|
|
|
self.assertEqual(1234, form5.cleaned_data['integer_field'])
|
2009-12-24 22:23:52 +08:00
|
|
|
|
|
|
|
form6 = SelectDateForm({
|
2012-06-08 00:08:47 +08:00
|
|
|
'date_field_month': '12',
|
|
|
|
'date_field_day': '31',
|
|
|
|
'date_field_year': '2009'
|
2009-12-24 22:23:52 +08:00
|
|
|
})
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertTrue(form6.is_valid())
|
2009-12-24 22:23:52 +08:00
|
|
|
self.assertEqual(datetime.date(2009, 12, 31), form6.cleaned_data['date_field'])
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
'<select name="mydate_month" id="id_mydate_month">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="1">January</option>'
|
|
|
|
'<option value="2">February</option>'
|
|
|
|
'<option value="3">March</option>'
|
|
|
|
'<option value="4">April</option>'
|
|
|
|
'<option value="5">May</option>'
|
|
|
|
'<option value="6">June</option>'
|
|
|
|
'<option value="7">July</option>'
|
|
|
|
'<option value="8">August</option>'
|
|
|
|
'<option value="9">September</option>'
|
|
|
|
'<option value="10">October</option>'
|
|
|
|
'<option value="11">November</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="12" selected>December</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'</select>'
|
|
|
|
'<select name="mydate_day" id="id_mydate_day">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="1">1</option>'
|
|
|
|
'<option value="2">2</option>'
|
|
|
|
'<option value="3">3</option>'
|
|
|
|
'<option value="4">4</option>'
|
|
|
|
'<option value="5">5</option>'
|
|
|
|
'<option value="6">6</option>'
|
|
|
|
'<option value="7">7</option>'
|
|
|
|
'<option value="8">8</option>'
|
|
|
|
'<option value="9">9</option>'
|
|
|
|
'<option value="10">10</option>'
|
|
|
|
'<option value="11">11</option>'
|
|
|
|
'<option value="12">12</option>'
|
|
|
|
'<option value="13">13</option>'
|
|
|
|
'<option value="14">14</option>'
|
|
|
|
'<option value="15">15</option>'
|
|
|
|
'<option value="16">16</option>'
|
|
|
|
'<option value="17">17</option>'
|
|
|
|
'<option value="18">18</option>'
|
|
|
|
'<option value="19">19</option>'
|
|
|
|
'<option value="20">20</option>'
|
|
|
|
'<option value="21">21</option>'
|
|
|
|
'<option value="22">22</option>'
|
|
|
|
'<option value="23">23</option>'
|
|
|
|
'<option value="24">24</option>'
|
|
|
|
'<option value="25">25</option>'
|
|
|
|
'<option value="26">26</option>'
|
|
|
|
'<option value="27">27</option>'
|
|
|
|
'<option value="28">28</option>'
|
|
|
|
'<option value="29">29</option>'
|
|
|
|
'<option value="30">30</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="31" selected>31</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'</select>'
|
|
|
|
'<select name="mydate_year" id="id_mydate_year">'
|
2018-01-30 17:33:13 +08:00
|
|
|
'<option value="">---</option>'
|
2016-09-22 06:12:13 +08:00
|
|
|
'<option value="2009" selected>2009</option>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<option value="2010">2010</option>'
|
|
|
|
'<option value="2011">2011</option>'
|
|
|
|
'<option value="2012">2012</option>'
|
|
|
|
'<option value="2013">2013</option>'
|
|
|
|
'<option value="2014">2014</option>'
|
|
|
|
'<option value="2015">2015</option>'
|
|
|
|
'<option value="2016">2016</option>'
|
|
|
|
'<option value="2017">2017</option>'
|
|
|
|
'<option value="2018">2018</option>'
|
|
|
|
'</select>',
|
2015-01-26 11:28:57 +08:00
|
|
|
forms.SelectDateWidget(years=range(2009, 2019)).render('mydate', datetime.date(2009, 12, 31))
|
2009-12-24 22:23:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_sub_locales(self):
|
|
|
|
"""
|
|
|
|
Check if sublocales fall back to the main locale
|
|
|
|
"""
|
2013-02-15 16:36:07 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('de-at', deactivate=True):
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual('66.666,666', Template('{{ n }}').render(self.ctxt))
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('es-us', deactivate=True):
|
2013-01-18 05:58:53 +08:00
|
|
|
self.assertEqual('31 de Diciembre de 2009', date_format(self.d))
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2009-12-31 06:11:48 +08:00
|
|
|
def test_localized_input(self):
|
|
|
|
"""
|
|
|
|
Tests if form input is correctly localized
|
|
|
|
"""
|
2013-02-23 16:45:56 +08:00
|
|
|
self.maxDiff = 1200
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('de-at', deactivate=True):
|
2009-12-31 06:11:48 +08:00
|
|
|
form6 = CompanyForm({
|
2012-06-08 00:08:47 +08:00
|
|
|
'name': 'acme',
|
2009-12-31 06:11:48 +08:00
|
|
|
'date_added': datetime.datetime(2009, 12, 31, 6, 0, 0),
|
2011-12-04 01:34:52 +08:00
|
|
|
'cents_paid': decimal.Decimal('59.47'),
|
2010-03-01 18:19:24 +08:00
|
|
|
'products_delivered': 12000,
|
2009-12-31 06:11:48 +08:00
|
|
|
})
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertTrue(form6.is_valid())
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2009-12-31 06:11:48 +08:00
|
|
|
form6.as_ul(),
|
2015-09-12 07:33:12 +08:00
|
|
|
'<li><label for="id_name">Name:</label>'
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input id="id_name" type="text" name="name" value="acme" maxlength="50" required></li>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<li><label for="id_date_added">Date added:</label>'
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="date_added" value="31.12.2009 06:00:00" id="id_date_added" required></li>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<li><label for="id_cents_paid">Cents paid:</label>'
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="cents_paid" value="59,47" id="id_cents_paid" required></li>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<li><label for="id_products_delivered">Products delivered:</label>'
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="products_delivered" value="12000" id="id_products_delivered" required>'
|
2016-03-29 02:02:04 +08:00
|
|
|
'</li>'
|
2009-12-31 06:11:48 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(localize_input(datetime.datetime(2009, 12, 31, 6, 0, 0)), '31.12.2009 06:00:00')
|
|
|
|
self.assertEqual(datetime.datetime(2009, 12, 31, 6, 0, 0), form6.cleaned_data['date_added'])
|
2011-05-06 21:29:44 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
|
|
|
# Checking for the localized "products_delivered" field
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertInHTML(
|
2016-03-29 02:02:04 +08:00
|
|
|
'<input type="text" name="products_delivered" '
|
2018-01-21 15:09:10 +08:00
|
|
|
'value="12.000" id="id_products_delivered" required>',
|
2015-09-12 07:33:12 +08:00
|
|
|
form6.as_ul()
|
|
|
|
)
|
2009-12-31 06:11:48 +08:00
|
|
|
|
2016-02-03 22:59:55 +08:00
|
|
|
def test_localized_input_func(self):
|
2018-07-27 04:38:08 +08:00
|
|
|
tests = (
|
|
|
|
(True, 'True'),
|
|
|
|
(datetime.date(1, 1, 1), '0001-01-01'),
|
|
|
|
(datetime.datetime(1, 1, 1), '0001-01-01 00:00:00'),
|
|
|
|
)
|
2016-02-03 22:59:55 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
2018-07-27 04:38:08 +08:00
|
|
|
for value, expected in tests:
|
|
|
|
with self.subTest(value=value):
|
|
|
|
self.assertEqual(localize_input(value), expected)
|
2016-02-03 22:59:55 +08:00
|
|
|
|
2021-05-10 21:39:50 +08:00
|
|
|
def test_sanitize_strftime_format(self):
|
|
|
|
for year in (1, 99, 999, 1000):
|
|
|
|
dt = datetime.date(year, 1, 1)
|
|
|
|
for fmt, expected in [
|
|
|
|
('%C', '%02d' % (year // 100)),
|
|
|
|
('%F', '%04d-01-01' % year),
|
|
|
|
('%G', '%04d' % year),
|
|
|
|
('%Y', '%04d' % year),
|
|
|
|
]:
|
|
|
|
with self.subTest(year=year, fmt=fmt):
|
|
|
|
fmt = sanitize_strftime_format(fmt)
|
|
|
|
self.assertEqual(dt.strftime(fmt), expected)
|
|
|
|
|
|
|
|
def test_sanitize_strftime_format_with_escaped_percent(self):
|
|
|
|
dt = datetime.date(1, 1, 1)
|
|
|
|
for fmt, expected in [
|
|
|
|
('%%C', '%C'),
|
|
|
|
('%%F', '%F'),
|
|
|
|
('%%G', '%G'),
|
|
|
|
('%%Y', '%Y'),
|
|
|
|
('%%%%C', '%%C'),
|
|
|
|
('%%%%F', '%%F'),
|
|
|
|
('%%%%G', '%%G'),
|
|
|
|
('%%%%Y', '%%Y'),
|
|
|
|
]:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
fmt = sanitize_strftime_format(fmt)
|
|
|
|
self.assertEqual(dt.strftime(fmt), expected)
|
|
|
|
|
|
|
|
for year in (1, 99, 999, 1000):
|
|
|
|
dt = datetime.date(year, 1, 1)
|
|
|
|
for fmt, expected in [
|
|
|
|
('%%%C', '%%%02d' % (year // 100)),
|
|
|
|
('%%%F', '%%%04d-01-01' % year),
|
|
|
|
('%%%G', '%%%04d' % year),
|
|
|
|
('%%%Y', '%%%04d' % year),
|
|
|
|
('%%%%%C', '%%%%%02d' % (year // 100)),
|
|
|
|
('%%%%%F', '%%%%%04d-01-01' % year),
|
|
|
|
('%%%%%G', '%%%%%04d' % year),
|
|
|
|
('%%%%%Y', '%%%%%04d' % year),
|
|
|
|
]:
|
|
|
|
with self.subTest(year=year, fmt=fmt):
|
|
|
|
fmt = sanitize_strftime_format(fmt)
|
|
|
|
self.assertEqual(dt.strftime(fmt), expected)
|
|
|
|
|
2013-02-15 23:37:52 +08:00
|
|
|
def test_sanitize_separators(self):
|
|
|
|
"""
|
|
|
|
Tests django.utils.formats.sanitize_separators.
|
|
|
|
"""
|
|
|
|
# Non-strings are untouched
|
|
|
|
self.assertEqual(sanitize_separators(123), 123)
|
|
|
|
|
|
|
|
with translation.override('ru', deactivate=True):
|
|
|
|
# Russian locale has non-breaking space (\xa0) as thousand separator
|
2016-10-27 15:53:39 +08:00
|
|
|
# Usual space is accepted too when sanitizing inputs
|
2013-02-15 23:37:52 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
|
|
|
self.assertEqual(sanitize_separators('1\xa0234\xa0567'), '1234567')
|
|
|
|
self.assertEqual(sanitize_separators('77\xa0777,777'), '77777.777')
|
|
|
|
self.assertEqual(sanitize_separators('12 345'), '12345')
|
|
|
|
self.assertEqual(sanitize_separators('77 777,777'), '77777.777')
|
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True, USE_L10N=False):
|
|
|
|
self.assertEqual(sanitize_separators('12\xa0345'), '12\xa0345')
|
|
|
|
|
2016-12-02 04:22:16 +08:00
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
|
|
|
with patch_formats(get_language(), THOUSAND_SEPARATOR='.', DECIMAL_SEPARATOR=','):
|
2014-08-19 01:57:50 +08:00
|
|
|
self.assertEqual(sanitize_separators('10.234'), '10234')
|
|
|
|
# Suspicion that user entered dot as decimal separator (#22171)
|
|
|
|
self.assertEqual(sanitize_separators('10.10'), '10.10')
|
|
|
|
|
2017-05-06 06:45:07 +08:00
|
|
|
with self.settings(USE_L10N=False, DECIMAL_SEPARATOR=','):
|
|
|
|
self.assertEqual(sanitize_separators('1001,10'), '1001.10')
|
|
|
|
self.assertEqual(sanitize_separators('1001.10'), '1001.10')
|
|
|
|
|
|
|
|
with self.settings(
|
|
|
|
USE_L10N=False, DECIMAL_SEPARATOR=',', USE_THOUSAND_SEPARATOR=True,
|
|
|
|
THOUSAND_SEPARATOR='.'
|
|
|
|
):
|
|
|
|
self.assertEqual(sanitize_separators('1.001,10'), '1001.10')
|
|
|
|
self.assertEqual(sanitize_separators('1001,10'), '1001.10')
|
|
|
|
self.assertEqual(sanitize_separators('1001.10'), '1001.10')
|
|
|
|
self.assertEqual(sanitize_separators('1,001.10'), '1.001.10') # Invalid output
|
|
|
|
|
2010-09-27 23:25:08 +08:00
|
|
|
def test_iter_format_modules(self):
|
|
|
|
"""
|
|
|
|
Tests the iter_format_modules function.
|
|
|
|
"""
|
2014-05-19 13:23:45 +08:00
|
|
|
# Importing some format modules so that we can compare the returned
|
|
|
|
# modules with these expected modules
|
|
|
|
default_mod = import_module('django.conf.locale.de.formats')
|
|
|
|
test_mod = import_module('i18n.other.locale.de.formats')
|
|
|
|
test_mod2 = import_module('i18n.other2.locale.de.formats')
|
|
|
|
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('de-at', deactivate=True):
|
2014-05-19 13:23:45 +08:00
|
|
|
# Should return the correct default module when no setting is set
|
|
|
|
self.assertEqual(list(iter_format_modules('de')), [default_mod])
|
|
|
|
|
|
|
|
# When the setting is a string, should return the given module and
|
|
|
|
# the default module
|
|
|
|
self.assertEqual(
|
|
|
|
list(iter_format_modules('de', 'i18n.other.locale')),
|
|
|
|
[test_mod, default_mod])
|
|
|
|
|
|
|
|
# When setting is a list of strings, should return the given
|
|
|
|
# modules and the default module
|
|
|
|
self.assertEqual(
|
|
|
|
list(iter_format_modules('de', ['i18n.other.locale', 'i18n.other2.locale'])),
|
|
|
|
[test_mod, test_mod2, default_mod])
|
2010-09-27 23:25:08 +08:00
|
|
|
|
2011-01-13 11:02:32 +08:00
|
|
|
def test_iter_format_modules_stability(self):
|
|
|
|
"""
|
|
|
|
Tests the iter_format_modules function always yields format modules in
|
|
|
|
a stable and correct order in presence of both base ll and ll_CC formats.
|
|
|
|
"""
|
2011-02-04 23:45:52 +08:00
|
|
|
en_format_mod = import_module('django.conf.locale.en.formats')
|
|
|
|
en_gb_format_mod = import_module('django.conf.locale.en_GB.formats')
|
|
|
|
self.assertEqual(list(iter_format_modules('en-gb')), [en_gb_format_mod, en_format_mod])
|
2011-01-13 11:02:32 +08:00
|
|
|
|
2011-09-22 23:04:27 +08:00
|
|
|
def test_get_format_modules_lang(self):
|
2013-02-15 16:36:07 +08:00
|
|
|
with translation.override('de', deactivate=True):
|
|
|
|
self.assertEqual('.', get_format('DECIMAL_SEPARATOR', lang='en'))
|
2011-09-22 23:04:27 +08:00
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def test_localize_templatetag_and_filter(self):
|
|
|
|
"""
|
2017-10-22 17:32:06 +08:00
|
|
|
Test the {% localize %} templatetag and the localize/unlocalize filters.
|
2010-10-30 00:48:58 +08:00
|
|
|
"""
|
2018-07-20 04:44:40 +08:00
|
|
|
context = Context({'int': 1455, 'float': 3.14, 'date': datetime.date(2016, 12, 31)})
|
2015-09-12 07:33:12 +08:00
|
|
|
template1 = Template(
|
2018-07-20 04:44:40 +08:00
|
|
|
'{% load l10n %}{% localize %}{{ int }}/{{ float }}/{{ date }}{% endlocalize %}; '
|
|
|
|
'{% localize on %}{{ int }}/{{ float }}/{{ date }}{% endlocalize %}'
|
2015-09-12 07:33:12 +08:00
|
|
|
)
|
2017-10-22 17:32:06 +08:00
|
|
|
template2 = Template(
|
2018-07-20 04:44:40 +08:00
|
|
|
'{% load l10n %}{{ int }}/{{ float }}/{{ date }}; '
|
|
|
|
'{% localize off %}{{ int }}/{{ float }}/{{ date }};{% endlocalize %} '
|
|
|
|
'{{ int }}/{{ float }}/{{ date }}'
|
2017-10-22 17:32:06 +08:00
|
|
|
)
|
|
|
|
template3 = Template(
|
2018-07-20 04:44:40 +08:00
|
|
|
'{% load l10n %}{{ int }}/{{ float }}/{{ date }}; '
|
|
|
|
'{{ int|unlocalize }}/{{ float|unlocalize }}/{{ date|unlocalize }}'
|
2017-10-22 17:32:06 +08:00
|
|
|
)
|
|
|
|
template4 = Template(
|
2018-07-20 04:44:40 +08:00
|
|
|
'{% load l10n %}{{ int }}/{{ float }}/{{ date }}; '
|
|
|
|
'{{ int|localize }}/{{ float|localize }}/{{ date|localize }}'
|
2017-10-22 17:32:06 +08:00
|
|
|
)
|
2018-07-20 04:44:40 +08:00
|
|
|
expected_localized = '1.455/3,14/31. Dezember 2016'
|
|
|
|
expected_unlocalized = '1455/3.14/Dez. 31, 2016'
|
2017-10-22 17:32:06 +08:00
|
|
|
output1 = '; '.join([expected_localized, expected_localized])
|
|
|
|
output2 = '; '.join([expected_localized, expected_unlocalized, expected_localized])
|
|
|
|
output3 = '; '.join([expected_localized, expected_unlocalized])
|
|
|
|
output4 = '; '.join([expected_unlocalized, expected_localized])
|
2011-05-06 21:29:44 +08:00
|
|
|
with translation.override('de', deactivate=True):
|
2018-07-20 04:44:40 +08:00
|
|
|
with self.settings(USE_L10N=False, USE_THOUSAND_SEPARATOR=True):
|
2011-05-06 21:29:44 +08:00
|
|
|
self.assertEqual(template1.render(context), output1)
|
|
|
|
self.assertEqual(template4.render(context), output4)
|
2018-07-20 04:44:40 +08:00
|
|
|
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
|
2011-05-06 21:29:44 +08:00
|
|
|
self.assertEqual(template1.render(context), output1)
|
|
|
|
self.assertEqual(template2.render(context), output2)
|
|
|
|
self.assertEqual(template3.render(context), output3)
|
2010-10-30 00:48:58 +08:00
|
|
|
|
2020-06-04 15:13:40 +08:00
|
|
|
def test_localized_off_numbers(self):
|
|
|
|
"""A string representation is returned for unlocalized numbers."""
|
|
|
|
template = Template(
|
|
|
|
'{% load l10n %}{% localize off %}'
|
|
|
|
'{{ int }}/{{ float }}/{{ decimal }}{% endlocalize %}'
|
|
|
|
)
|
|
|
|
context = Context(
|
|
|
|
{'int': 1455, 'float': 3.14, 'decimal': decimal.Decimal('24.1567')}
|
|
|
|
)
|
|
|
|
for use_l10n in [True, False]:
|
|
|
|
with self.subTest(use_l10n=use_l10n), self.settings(
|
|
|
|
USE_L10N=use_l10n,
|
2020-06-04 15:11:37 +08:00
|
|
|
DECIMAL_SEPARATOR=',',
|
2020-06-04 15:13:40 +08:00
|
|
|
USE_THOUSAND_SEPARATOR=True,
|
|
|
|
THOUSAND_SEPARATOR='°',
|
|
|
|
NUMBER_GROUPING=2,
|
|
|
|
):
|
|
|
|
self.assertEqual(template.render(context), '1455/3.14/24.1567')
|
|
|
|
|
2013-02-24 21:57:29 +08:00
|
|
|
def test_localized_as_text_as_hidden_input(self):
|
|
|
|
"""
|
|
|
|
Tests if form input with 'as_hidden' or 'as_text' is correctly localized. Ticket #18777
|
|
|
|
"""
|
|
|
|
self.maxDiff = 1200
|
|
|
|
|
|
|
|
with translation.override('de-at', deactivate=True):
|
|
|
|
template = Template('{% load l10n %}{{ form.date_added }}; {{ form.cents_paid }}')
|
|
|
|
template_as_text = Template('{% load l10n %}{{ form.date_added.as_text }}; {{ form.cents_paid.as_text }}')
|
2015-09-12 07:33:12 +08:00
|
|
|
template_as_hidden = Template(
|
|
|
|
'{% load l10n %}{{ form.date_added.as_hidden }}; {{ form.cents_paid.as_hidden }}'
|
|
|
|
)
|
2013-02-24 21:57:29 +08:00
|
|
|
form = CompanyForm({
|
|
|
|
'name': 'acme',
|
|
|
|
'date_added': datetime.datetime(2009, 12, 31, 6, 0, 0),
|
|
|
|
'cents_paid': decimal.Decimal('59.47'),
|
|
|
|
'products_delivered': 12000,
|
2013-10-18 17:02:43 +08:00
|
|
|
})
|
2013-10-15 03:13:14 +08:00
|
|
|
context = Context({'form': form})
|
2013-02-24 21:57:29 +08:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
template.render(context),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input id="id_date_added" name="date_added" type="text" value="31.12.2009 06:00:00" required>;'
|
|
|
|
'<input id="id_cents_paid" name="cents_paid" type="text" value="59,47" required>'
|
2013-02-24 21:57:29 +08:00
|
|
|
)
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
template_as_text.render(context),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input id="id_date_added" name="date_added" type="text" value="31.12.2009 06:00:00" required>;'
|
|
|
|
' <input id="id_cents_paid" name="cents_paid" type="text" value="59,47" required>'
|
2013-02-24 21:57:29 +08:00
|
|
|
)
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
template_as_hidden.render(context),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input id="id_date_added" name="date_added" type="hidden" value="31.12.2009 06:00:00">;'
|
|
|
|
'<input id="id_cents_paid" name="cents_paid" type="hidden" value="59,47">'
|
2013-02-24 21:57:29 +08:00
|
|
|
)
|
|
|
|
|
2015-11-12 03:10:55 +08:00
|
|
|
def test_format_arbitrary_settings(self):
|
|
|
|
self.assertEqual(get_format('DEBUG'), 'DEBUG')
|
|
|
|
|
2015-11-25 19:17:59 +08:00
|
|
|
def test_get_custom_format(self):
|
2021-04-23 01:57:27 +08:00
|
|
|
reset_format_cache()
|
2015-11-25 19:17:59 +08:00
|
|
|
with self.settings(FORMAT_MODULE_PATH='i18n.other.locale'):
|
|
|
|
with translation.override('fr', deactivate=True):
|
|
|
|
self.assertEqual('d/m/Y CUSTOM', get_format('CUSTOM_DAY_FORMAT'))
|
|
|
|
|
2018-02-11 05:05:41 +08:00
|
|
|
def test_admin_javascript_supported_input_formats(self):
|
|
|
|
"""
|
|
|
|
The first input format for DATE_INPUT_FORMATS, TIME_INPUT_FORMATS, and
|
|
|
|
DATETIME_INPUT_FORMATS must not contain %f since that's unsupported by
|
|
|
|
the admin's time picker widget.
|
|
|
|
"""
|
|
|
|
regex = re.compile('%([^BcdHImMpSwxXyY%])')
|
|
|
|
for language_code, language_name in settings.LANGUAGES:
|
|
|
|
for format_name in ('DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS', 'DATETIME_INPUT_FORMATS'):
|
|
|
|
with self.subTest(language=language_code, format=format_name):
|
|
|
|
formatter = get_format(format_name, lang=language_code)[0]
|
|
|
|
self.assertEqual(
|
|
|
|
regex.findall(formatter), [],
|
|
|
|
"%s locale's %s uses an unsupported format code." % (language_code, format_name)
|
|
|
|
)
|
|
|
|
|
2013-02-24 21:57:29 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class MiscTests(SimpleTestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
rf = RequestFactory()
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2015-02-28 03:17:04 +08:00
|
|
|
@override_settings(LANGUAGE_CODE='de')
|
|
|
|
def test_english_fallback(self):
|
|
|
|
"""
|
|
|
|
With a non-English LANGUAGE_CODE and if the active language is English
|
|
|
|
or one of its variants, the untranslated string should be returned
|
|
|
|
(instead of falling back to LANGUAGE_CODE) (See #24413).
|
|
|
|
"""
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertEqual(gettext("Image"), "Bild")
|
2015-02-28 03:17:04 +08:00
|
|
|
with translation.override('en'):
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertEqual(gettext("Image"), "Image")
|
2015-02-28 03:17:04 +08:00
|
|
|
with translation.override('en-us'):
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertEqual(gettext("Image"), "Image")
|
2015-02-28 03:17:04 +08:00
|
|
|
with translation.override('en-ca'):
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertEqual(gettext("Image"), "Image")
|
2015-02-28 03:17:04 +08:00
|
|
|
|
2009-12-23 01:58:49 +08:00
|
|
|
def test_parse_spec_http_header(self):
|
|
|
|
"""
|
|
|
|
Testing HTTP header parsing. First, we test that we can parse the
|
|
|
|
values according to the spec (and that we extract all the pieces in
|
|
|
|
the right order).
|
|
|
|
"""
|
2017-09-28 04:31:03 +08:00
|
|
|
tests = [
|
|
|
|
# Good headers
|
|
|
|
('de', [('de', 1.0)]),
|
|
|
|
('en-AU', [('en-au', 1.0)]),
|
|
|
|
('es-419', [('es-419', 1.0)]),
|
|
|
|
('*;q=1.00', [('*', 1.0)]),
|
|
|
|
('en-AU;q=0.123', [('en-au', 0.123)]),
|
|
|
|
('en-au;q=0.5', [('en-au', 0.5)]),
|
|
|
|
('en-au;q=1.0', [('en-au', 1.0)]),
|
|
|
|
('da, en-gb;q=0.25, en;q=0.5', [('da', 1.0), ('en', 0.5), ('en-gb', 0.25)]),
|
|
|
|
('en-au-xx', [('en-au-xx', 1.0)]),
|
|
|
|
('de,en-au;q=0.75,en-us;q=0.5,en;q=0.25,es;q=0.125,fa;q=0.125',
|
|
|
|
[('de', 1.0), ('en-au', 0.75), ('en-us', 0.5), ('en', 0.25), ('es', 0.125), ('fa', 0.125)]),
|
|
|
|
('*', [('*', 1.0)]),
|
|
|
|
('de;q=0.', [('de', 0.0)]),
|
|
|
|
('en; q=1,', [('en', 1.0)]),
|
|
|
|
('en; q=1.0, * ; q=0.5', [('en', 1.0), ('*', 0.5)]),
|
|
|
|
# Bad headers
|
|
|
|
('en-gb;q=1.0000', []),
|
2017-12-29 04:07:29 +08:00
|
|
|
('en;q=0.1234', []),
|
2017-09-28 04:31:03 +08:00
|
|
|
('en;q=.2', []),
|
|
|
|
('abcdefghi-au', []),
|
|
|
|
('**', []),
|
|
|
|
('en,,gb', []),
|
|
|
|
('en-au;q=0.1.0', []),
|
|
|
|
(('X' * 97) + 'Z,en', []),
|
|
|
|
('da, en-gb;q=0.8, en;q=0.7,#', []),
|
|
|
|
('de;q=2.0', []),
|
|
|
|
('de;q=0.a', []),
|
|
|
|
('12-345', []),
|
|
|
|
('', []),
|
|
|
|
('en;q=1e0', []),
|
|
|
|
]
|
|
|
|
for value, expected in tests:
|
|
|
|
with self.subTest(value=value):
|
2017-09-20 06:16:48 +08:00
|
|
|
self.assertEqual(trans_real.parse_accept_lang_header(value), tuple(expected))
|
2009-12-23 01:58:49 +08:00
|
|
|
|
|
|
|
def test_parse_literal_http_header(self):
|
|
|
|
"""
|
|
|
|
Now test that we parse a literal HTTP header correctly.
|
|
|
|
"""
|
|
|
|
g = get_language_from_request
|
2011-06-16 01:29:10 +08:00
|
|
|
r = self.rf.get('/')
|
2009-12-23 01:58:49 +08:00
|
|
|
r.COOKIES = {}
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt-br'}
|
|
|
|
self.assertEqual('pt-br', g(r))
|
|
|
|
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt'}
|
|
|
|
self.assertEqual('pt', g(r))
|
|
|
|
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'es,de'}
|
|
|
|
self.assertEqual('es', g(r))
|
|
|
|
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-ar,de'}
|
|
|
|
self.assertEqual('es-ar', g(r))
|
|
|
|
|
2011-03-28 10:27:43 +08:00
|
|
|
# This test assumes there won't be a Django translation to a US
|
|
|
|
# variation of the Spanish language, a safe assumption. When the
|
|
|
|
# user sets it as the preferred language, the main 'es'
|
|
|
|
# translation should be selected instead.
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'es-us'}
|
|
|
|
self.assertEqual(g(r), 'es')
|
2009-12-23 01:58:49 +08:00
|
|
|
|
|
|
|
# This tests the following scenario: there isn't a main language (zh)
|
2015-01-10 04:08:16 +08:00
|
|
|
# translation of Django but there is a translation to variation (zh-hans)
|
|
|
|
# the user sets zh-hans as the preferred language, it should be selected
|
2009-12-23 01:58:49 +08:00
|
|
|
# by Django without falling back nor ignoring it.
|
2015-01-10 04:08:16 +08:00
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-hans,de'}
|
|
|
|
self.assertEqual(g(r), 'zh-hans')
|
2013-11-05 01:31:34 +08:00
|
|
|
|
2013-11-12 14:54:01 +08:00
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'NL'}
|
|
|
|
self.assertEqual('nl', g(r))
|
|
|
|
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'fy'}
|
|
|
|
self.assertEqual('fy', g(r))
|
|
|
|
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'ia'}
|
|
|
|
self.assertEqual('ia', g(r))
|
|
|
|
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'sr-latn'}
|
|
|
|
self.assertEqual('sr-latn', g(r))
|
|
|
|
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-hans'}
|
|
|
|
self.assertEqual('zh-hans', g(r))
|
|
|
|
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-hant'}
|
|
|
|
self.assertEqual('zh-hant', g(r))
|
|
|
|
|
2013-11-05 01:31:34 +08:00
|
|
|
@override_settings(
|
2015-01-22 00:55:57 +08:00
|
|
|
LANGUAGES=[
|
2013-11-05 01:31:34 +08:00
|
|
|
('en', 'English'),
|
|
|
|
('zh-hans', 'Simplified Chinese'),
|
|
|
|
('zh-hant', 'Traditional Chinese'),
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2013-11-05 01:31:34 +08:00
|
|
|
)
|
|
|
|
def test_support_for_deprecated_chinese_language_codes(self):
|
|
|
|
"""
|
2016-01-11 00:48:16 +08:00
|
|
|
Some browsers (Firefox, IE, etc.) use deprecated language codes. As these
|
2013-11-05 01:31:34 +08:00
|
|
|
language codes will be removed in Django 1.9, these will be incorrectly
|
|
|
|
matched. For example zh-tw (traditional) will be interpreted as zh-hans
|
|
|
|
(simplified), which is wrong. So we should also accept these deprecated
|
|
|
|
language codes.
|
|
|
|
|
|
|
|
refs #18419 -- this is explicitly for browser compatibility
|
|
|
|
"""
|
|
|
|
g = get_language_from_request
|
|
|
|
r = self.rf.get('/')
|
|
|
|
r.COOKIES = {}
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-cn,en'}
|
|
|
|
self.assertEqual(g(r), 'zh-hans')
|
2013-11-05 17:54:23 +08:00
|
|
|
|
2013-11-05 01:31:34 +08:00
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-tw,en'}
|
|
|
|
self.assertEqual(g(r), 'zh-hant')
|
2013-11-05 17:54:23 +08:00
|
|
|
|
2014-07-12 03:01:37 +08:00
|
|
|
def test_special_fallback_language(self):
|
|
|
|
"""
|
|
|
|
Some languages may have special fallbacks that don't follow the simple
|
|
|
|
'fr-ca' -> 'fr' logic (notably Chinese codes).
|
|
|
|
"""
|
|
|
|
r = self.rf.get('/')
|
|
|
|
r.COOKIES = {}
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'zh-my,en'}
|
|
|
|
self.assertEqual(get_language_from_request(r), 'zh-hans')
|
|
|
|
|
2021-04-30 06:40:06 +08:00
|
|
|
def test_subsequent_code_fallback_language(self):
|
|
|
|
"""
|
|
|
|
Subsequent language codes should be used when the language code is not
|
|
|
|
supported.
|
|
|
|
"""
|
|
|
|
tests = [
|
|
|
|
('zh-Hans-CN', 'zh-hans'),
|
|
|
|
('zh-hans-mo', 'zh-hans'),
|
|
|
|
('zh-hans-HK', 'zh-hans'),
|
|
|
|
('zh-Hant-HK', 'zh-hant'),
|
|
|
|
('zh-hant-tw', 'zh-hant'),
|
|
|
|
('zh-hant-SG', 'zh-hant'),
|
|
|
|
]
|
|
|
|
r = self.rf.get('/')
|
|
|
|
r.COOKIES = {}
|
|
|
|
for value, expected in tests:
|
|
|
|
with self.subTest(value=value):
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': f'{value},en'}
|
|
|
|
self.assertEqual(get_language_from_request(r), expected)
|
|
|
|
|
2010-02-16 20:13:48 +08:00
|
|
|
def test_parse_language_cookie(self):
|
|
|
|
"""
|
|
|
|
Now test that we parse language preferences stored in a cookie correctly.
|
|
|
|
"""
|
|
|
|
g = get_language_from_request
|
2011-06-16 01:29:10 +08:00
|
|
|
r = self.rf.get('/')
|
2010-02-16 20:13:48 +08:00
|
|
|
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt-br'}
|
|
|
|
r.META = {}
|
|
|
|
self.assertEqual('pt-br', g(r))
|
|
|
|
|
|
|
|
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'pt'}
|
|
|
|
r.META = {}
|
|
|
|
self.assertEqual('pt', g(r))
|
|
|
|
|
|
|
|
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es'}
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
|
|
|
|
self.assertEqual('es', g(r))
|
|
|
|
|
2011-03-28 10:27:43 +08:00
|
|
|
# This test assumes there won't be a Django translation to a US
|
|
|
|
# variation of the Spanish language, a safe assumption. When the
|
|
|
|
# user sets it as the preferred language, the main 'es'
|
|
|
|
# translation should be selected instead.
|
|
|
|
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'es-us'}
|
|
|
|
r.META = {}
|
|
|
|
self.assertEqual(g(r), 'es')
|
2010-02-16 20:13:48 +08:00
|
|
|
|
|
|
|
# This tests the following scenario: there isn't a main language (zh)
|
2015-01-10 04:08:16 +08:00
|
|
|
# translation of Django but there is a translation to variation (zh-hans)
|
|
|
|
# the user sets zh-hans as the preferred language, it should be selected
|
2010-02-16 20:13:48 +08:00
|
|
|
# by Django without falling back nor ignoring it.
|
2015-01-10 04:08:16 +08:00
|
|
|
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'zh-hans'}
|
2010-02-16 20:13:48 +08:00
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'}
|
2015-01-10 04:08:16 +08:00
|
|
|
self.assertEqual(g(r), 'zh-hans')
|
2010-02-16 20:17:17 +08:00
|
|
|
|
2017-06-28 07:50:26 +08:00
|
|
|
@override_settings(
|
|
|
|
USE_I18N=True,
|
|
|
|
LANGUAGES=[
|
|
|
|
('en', 'English'),
|
|
|
|
('de', 'German'),
|
|
|
|
('de-at', 'Austrian German'),
|
|
|
|
('pt-br', 'Portuguese (Brazil)'),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_get_supported_language_variant_real(self):
|
|
|
|
g = trans_real.get_supported_language_variant
|
|
|
|
self.assertEqual(g('en'), 'en')
|
|
|
|
self.assertEqual(g('en-gb'), 'en')
|
|
|
|
self.assertEqual(g('de'), 'de')
|
|
|
|
self.assertEqual(g('de-at'), 'de-at')
|
|
|
|
self.assertEqual(g('de-ch'), 'de')
|
|
|
|
self.assertEqual(g('pt-br'), 'pt-br')
|
|
|
|
self.assertEqual(g('pt'), 'pt-br')
|
|
|
|
self.assertEqual(g('pt-pt'), 'pt-br')
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('pt', strict=True)
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('pt-pt', strict=True)
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('xyz')
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('xy-zz')
|
|
|
|
|
|
|
|
def test_get_supported_language_variant_null(self):
|
|
|
|
g = trans_null.get_supported_language_variant
|
|
|
|
self.assertEqual(g(settings.LANGUAGE_CODE), settings.LANGUAGE_CODE)
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('pt')
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('de')
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('de-at')
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('de', strict=True)
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('de-at', strict=True)
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
g('xyz')
|
|
|
|
|
2016-10-27 21:31:15 +08:00
|
|
|
@override_settings(
|
|
|
|
LANGUAGES=[
|
|
|
|
('en', 'English'),
|
|
|
|
('de', 'German'),
|
|
|
|
('de-at', 'Austrian German'),
|
|
|
|
('pl', 'Polish'),
|
|
|
|
],
|
|
|
|
)
|
2012-08-23 17:18:41 +08:00
|
|
|
def test_get_language_from_path_real(self):
|
2012-10-22 20:45:41 +08:00
|
|
|
g = trans_real.get_language_from_path
|
2012-02-05 02:43:20 +08:00
|
|
|
self.assertEqual(g('/pl/'), 'pl')
|
|
|
|
self.assertEqual(g('/pl'), 'pl')
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(g('/xyz/'))
|
2016-10-27 21:31:15 +08:00
|
|
|
self.assertEqual(g('/en/'), 'en')
|
|
|
|
self.assertEqual(g('/en-gb/'), 'en')
|
|
|
|
self.assertEqual(g('/de/'), 'de')
|
|
|
|
self.assertEqual(g('/de-at/'), 'de-at')
|
|
|
|
self.assertEqual(g('/de-ch/'), 'de')
|
2016-10-28 17:36:53 +08:00
|
|
|
self.assertIsNone(g('/de-simple-page/'))
|
2012-02-05 02:43:20 +08:00
|
|
|
|
2012-08-23 17:18:41 +08:00
|
|
|
def test_get_language_from_path_null(self):
|
2018-02-21 21:38:00 +08:00
|
|
|
g = trans_null.get_language_from_path
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(g('/pl/'))
|
|
|
|
self.assertIsNone(g('/pl'))
|
|
|
|
self.assertIsNone(g('/xyz/'))
|
2012-02-05 02:43:20 +08:00
|
|
|
|
2013-10-28 23:57:56 +08:00
|
|
|
def test_cache_resetting(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
After setting LANGUAGE, the cache should be cleared and languages
|
|
|
|
previously valid should not be used (#14170).
|
2013-10-28 23:57:56 +08:00
|
|
|
"""
|
|
|
|
g = get_language_from_request
|
|
|
|
r = self.rf.get('/')
|
|
|
|
r.COOKIES = {}
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt-br'}
|
|
|
|
self.assertEqual('pt-br', g(r))
|
2015-01-22 00:55:57 +08:00
|
|
|
with self.settings(LANGUAGES=[('en', 'English')]):
|
2013-10-28 23:57:56 +08:00
|
|
|
self.assertNotEqual('pt-br', g(r))
|
|
|
|
|
2016-07-24 22:00:39 +08:00
|
|
|
def test_i18n_patterns_returns_list(self):
|
|
|
|
with override_settings(USE_I18N=False):
|
|
|
|
self.assertIsInstance(i18n_patterns([]), list)
|
|
|
|
with override_settings(USE_I18N=True):
|
|
|
|
self.assertIsInstance(i18n_patterns([]), list)
|
|
|
|
|
2011-09-08 21:25:41 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class ResolutionOrderI18NTests(SimpleTestCase):
|
2010-02-16 20:17:17 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUp()
|
2010-02-16 20:17:17 +08:00
|
|
|
activate('de')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
deactivate()
|
2017-01-21 21:13:44 +08:00
|
|
|
super().tearDown()
|
2010-02-16 20:17:17 +08:00
|
|
|
|
2017-01-27 03:58:33 +08:00
|
|
|
def assertGettext(self, msgid, msgstr):
|
|
|
|
result = gettext(msgid)
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertIn(
|
|
|
|
msgstr, result,
|
|
|
|
"The string '%s' isn't in the translation of '%s'; the actual result is '%s'."
|
|
|
|
% (msgstr, msgid, result)
|
|
|
|
)
|
2010-02-16 20:17:17 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-02-16 20:17:17 +08:00
|
|
|
class AppResolutionOrderI18NTests(ResolutionOrderI18NTests):
|
|
|
|
|
2014-01-26 22:28:33 +08:00
|
|
|
@override_settings(LANGUAGE_CODE='de')
|
2010-02-16 20:17:17 +08:00
|
|
|
def test_app_translation(self):
|
2013-12-19 22:57:23 +08:00
|
|
|
# Original translation.
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertGettext('Date/time', 'Datum/Zeit')
|
2013-12-19 22:57:23 +08:00
|
|
|
|
|
|
|
# Different translation.
|
2013-12-23 17:37:34 +08:00
|
|
|
with self.modify_settings(INSTALLED_APPS={'append': 'i18n.resolution'}):
|
2014-01-26 22:28:33 +08:00
|
|
|
# Force refreshing translations.
|
2013-12-19 22:57:23 +08:00
|
|
|
activate('de')
|
|
|
|
|
|
|
|
# Doesn't work because it's added later in the list.
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertGettext('Date/time', 'Datum/Zeit')
|
2013-12-19 22:57:23 +08:00
|
|
|
|
2014-01-25 05:43:00 +08:00
|
|
|
with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.admin.apps.SimpleAdminConfig'}):
|
2014-01-26 22:28:33 +08:00
|
|
|
# Force refreshing translations.
|
2013-12-19 22:57:23 +08:00
|
|
|
activate('de')
|
|
|
|
|
|
|
|
# Unless the original is removed from the list.
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertGettext('Date/time', 'Datum/Zeit (APP)')
|
2010-02-16 20:17:17 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-10-22 20:45:41 +08:00
|
|
|
@override_settings(LOCALE_PATHS=extended_locale_paths)
|
2010-02-16 20:17:17 +08:00
|
|
|
class LocalePathsResolutionOrderI18NTests(ResolutionOrderI18NTests):
|
|
|
|
|
|
|
|
def test_locale_paths_translation(self):
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertGettext('Time', 'LOCALE_PATHS')
|
2010-02-16 20:17:17 +08:00
|
|
|
|
2011-02-08 02:48:40 +08:00
|
|
|
def test_locale_paths_override_app_translation(self):
|
2013-12-23 17:37:34 +08:00
|
|
|
with self.settings(INSTALLED_APPS=['i18n.resolution']):
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertGettext('Time', 'LOCALE_PATHS')
|
2011-02-08 02:48:40 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-02-16 20:17:17 +08:00
|
|
|
class DjangoFallbackResolutionOrderI18NTests(ResolutionOrderI18NTests):
|
|
|
|
|
|
|
|
def test_django_fallback(self):
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertEqual(gettext('Date/time'), 'Datum/Zeit')
|
2010-09-13 04:03:39 +08:00
|
|
|
|
|
|
|
|
2018-02-20 02:09:33 +08:00
|
|
|
@override_settings(INSTALLED_APPS=['i18n.territorial_fallback'])
|
|
|
|
class TranslationFallbackI18NTests(ResolutionOrderI18NTests):
|
|
|
|
|
|
|
|
def test_sparse_territory_catalog(self):
|
|
|
|
"""
|
|
|
|
Untranslated strings for territorial language variants use the
|
|
|
|
translations of the generic language. In this case, the de-de
|
|
|
|
translation falls back to de.
|
|
|
|
"""
|
|
|
|
with translation.override('de-de'):
|
|
|
|
self.assertGettext('Test 1 (en)', '(de-de)')
|
|
|
|
self.assertGettext('Test 2 (en)', '(de)')
|
|
|
|
|
|
|
|
|
2010-09-13 04:03:39 +08:00
|
|
|
class TestModels(TestCase):
|
|
|
|
def test_lazy(self):
|
|
|
|
tm = TestModel()
|
|
|
|
tm.save()
|
|
|
|
|
|
|
|
def test_safestr(self):
|
2011-12-04 01:34:52 +08:00
|
|
|
c = Company(cents_paid=12, products_delivered=1)
|
2019-02-05 22:38:29 +08:00
|
|
|
c.name = SafeString('Iñtërnâtiônàlizætiøn1')
|
2010-09-13 04:03:39 +08:00
|
|
|
c.save()
|
2010-12-13 07:02:45 +08:00
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class TestLanguageInfo(SimpleTestCase):
|
2010-12-13 07:02:45 +08:00
|
|
|
def test_localized_language_info(self):
|
|
|
|
li = get_language_info('de')
|
|
|
|
self.assertEqual(li['code'], 'de')
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(li['name_local'], 'Deutsch')
|
2010-12-13 07:02:45 +08:00
|
|
|
self.assertEqual(li['name'], 'German')
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(li['bidi'], False)
|
2011-01-26 23:12:18 +08:00
|
|
|
|
2013-02-24 00:02:07 +08:00
|
|
|
def test_unknown_language_code(self):
|
2016-12-08 06:42:31 +08:00
|
|
|
with self.assertRaisesMessage(KeyError, "Unknown language code xx"):
|
|
|
|
get_language_info('xx')
|
2016-01-07 01:33:29 +08:00
|
|
|
with translation.override('xx'):
|
|
|
|
# A language with no translation catalogs should fallback to the
|
|
|
|
# untranslated string.
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertEqual(gettext("Title"), "Title")
|
2013-02-24 00:02:07 +08:00
|
|
|
|
|
|
|
def test_unknown_only_country_code(self):
|
|
|
|
li = get_language_info('de-xx')
|
|
|
|
self.assertEqual(li['code'], 'de')
|
|
|
|
self.assertEqual(li['name_local'], 'Deutsch')
|
|
|
|
self.assertEqual(li['name'], 'German')
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(li['bidi'], False)
|
2013-02-24 00:02:07 +08:00
|
|
|
|
|
|
|
def test_unknown_language_code_and_country_code(self):
|
2016-12-08 06:42:31 +08:00
|
|
|
with self.assertRaisesMessage(KeyError, "Unknown language code xx-xx and xx"):
|
|
|
|
get_language_info('xx-xx')
|
2013-02-24 00:02:07 +08:00
|
|
|
|
2014-07-12 03:01:37 +08:00
|
|
|
def test_fallback_language_code(self):
|
|
|
|
"""
|
|
|
|
get_language_info return the first fallback language info if the lang_info
|
|
|
|
struct does not contain the 'name' key.
|
|
|
|
"""
|
|
|
|
li = get_language_info('zh-my')
|
|
|
|
self.assertEqual(li['code'], 'zh-hans')
|
2015-01-10 04:08:16 +08:00
|
|
|
li = get_language_info('zh-hans')
|
|
|
|
self.assertEqual(li['code'], 'zh-hans')
|
2014-07-12 03:01:37 +08:00
|
|
|
|
2011-01-26 23:12:18 +08:00
|
|
|
|
2013-02-28 20:45:21 +08:00
|
|
|
@override_settings(
|
|
|
|
USE_I18N=True,
|
2015-01-22 00:55:57 +08:00
|
|
|
LANGUAGES=[
|
2013-02-28 20:45:21 +08:00
|
|
|
('en', 'English'),
|
|
|
|
('fr', 'French'),
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2013-02-28 20:45:21 +08:00
|
|
|
'django.middleware.locale.LocaleMiddleware',
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2014-04-05 14:04:46 +08:00
|
|
|
ROOT_URLCONF='i18n.urls',
|
2013-02-28 20:45:21 +08:00
|
|
|
)
|
2014-01-26 22:28:33 +08:00
|
|
|
class LocaleMiddlewareTests(TestCase):
|
2013-02-28 20:45:21 +08:00
|
|
|
|
|
|
|
def test_streaming_response(self):
|
|
|
|
# Regression test for #5241
|
|
|
|
response = self.client.get('/fr/streaming/')
|
|
|
|
self.assertContains(response, "Oui/Non")
|
|
|
|
response = self.client.get('/en/streaming/')
|
|
|
|
self.assertContains(response, "Yes/No")
|
2013-05-18 20:37:04 +08:00
|
|
|
|
2013-03-25 22:45:24 +08:00
|
|
|
|
2016-02-29 16:24:19 +08:00
|
|
|
@override_settings(
|
|
|
|
USE_I18N=True,
|
|
|
|
LANGUAGES=[
|
|
|
|
('en', 'English'),
|
2016-10-28 17:36:53 +08:00
|
|
|
('de', 'German'),
|
2016-02-29 16:24:19 +08:00
|
|
|
('fr', 'French'),
|
|
|
|
],
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2016-02-29 16:24:19 +08:00
|
|
|
'django.middleware.locale.LocaleMiddleware',
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
],
|
|
|
|
ROOT_URLCONF='i18n.urls_default_unprefixed',
|
|
|
|
LANGUAGE_CODE='en',
|
|
|
|
)
|
|
|
|
class UnprefixedDefaultLanguageTests(SimpleTestCase):
|
|
|
|
def test_default_lang_without_prefix(self):
|
|
|
|
"""
|
|
|
|
With i18n_patterns(..., prefix_default_language=False), the default
|
|
|
|
language (settings.LANGUAGE_CODE) should be accessible without a prefix.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/simple/')
|
|
|
|
self.assertEqual(response.content, b'Yes')
|
|
|
|
|
|
|
|
def test_other_lang_with_prefix(self):
|
|
|
|
response = self.client.get('/fr/simple/')
|
|
|
|
self.assertEqual(response.content, b'Oui')
|
|
|
|
|
2016-03-10 22:08:53 +08:00
|
|
|
def test_unprefixed_language_other_than_accept_language(self):
|
|
|
|
response = self.client.get('/simple/', HTTP_ACCEPT_LANGUAGE='fr')
|
|
|
|
self.assertEqual(response.content, b'Yes')
|
|
|
|
|
2016-10-28 17:36:53 +08:00
|
|
|
def test_page_with_dash(self):
|
2017-07-11 00:08:57 +08:00
|
|
|
# A page starting with /de* shouldn't match the 'de' language code.
|
2016-10-28 17:36:53 +08:00
|
|
|
response = self.client.get('/de-simple-page/')
|
|
|
|
self.assertEqual(response.content, b'Yes')
|
|
|
|
|
2016-11-05 19:56:34 +08:00
|
|
|
def test_no_redirect_on_404(self):
|
|
|
|
"""
|
|
|
|
A request for a nonexistent URL shouldn't cause a redirect to
|
2019-07-02 15:36:17 +08:00
|
|
|
/<default_language>/<request_url> when prefix_default_language=False and
|
2016-11-05 19:56:34 +08:00
|
|
|
/<default_language>/<request_url> has a URL match (#27402).
|
|
|
|
"""
|
|
|
|
# A match for /group1/group2/ must exist for this to act as a
|
|
|
|
# regression test.
|
|
|
|
response = self.client.get('/group1/group2/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
response = self.client.get('/nonexistent/')
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
2016-02-29 16:24:19 +08:00
|
|
|
|
2013-05-18 20:37:04 +08:00
|
|
|
@override_settings(
|
|
|
|
USE_I18N=True,
|
2015-01-22 00:55:57 +08:00
|
|
|
LANGUAGES=[
|
2013-05-18 20:37:04 +08:00
|
|
|
('bg', 'Bulgarian'),
|
|
|
|
('en-us', 'English'),
|
2015-11-03 17:55:10 +08:00
|
|
|
('pt-br', 'Portuguese (Brazil)'),
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2013-05-18 20:37:04 +08:00
|
|
|
'django.middleware.locale.LocaleMiddleware',
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2014-04-05 14:04:46 +08:00
|
|
|
ROOT_URLCONF='i18n.urls'
|
2013-05-18 20:37:04 +08:00
|
|
|
)
|
2015-04-18 05:38:20 +08:00
|
|
|
class CountrySpecificLanguageTests(SimpleTestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
rf = RequestFactory()
|
2013-05-18 20:37:04 +08:00
|
|
|
|
|
|
|
def test_check_for_language(self):
|
|
|
|
self.assertTrue(check_for_language('en'))
|
|
|
|
self.assertTrue(check_for_language('en-us'))
|
|
|
|
self.assertTrue(check_for_language('en-US'))
|
2018-09-03 16:43:55 +08:00
|
|
|
self.assertFalse(check_for_language('en_US'))
|
2014-12-30 22:41:31 +08:00
|
|
|
self.assertTrue(check_for_language('be'))
|
|
|
|
self.assertTrue(check_for_language('be@latin'))
|
|
|
|
self.assertTrue(check_for_language('sr-RS@latin'))
|
|
|
|
self.assertTrue(check_for_language('sr-RS@12345'))
|
2013-11-20 23:31:53 +08:00
|
|
|
self.assertFalse(check_for_language('en-ü'))
|
|
|
|
self.assertFalse(check_for_language('en\x00'))
|
2015-04-03 18:11:54 +08:00
|
|
|
self.assertFalse(check_for_language(None))
|
2014-12-30 22:41:31 +08:00
|
|
|
self.assertFalse(check_for_language('be@ '))
|
|
|
|
# Specifying encoding is not supported (Django enforces UTF-8)
|
|
|
|
self.assertFalse(check_for_language('tr-TR.UTF-8'))
|
|
|
|
self.assertFalse(check_for_language('tr-TR.UTF8'))
|
|
|
|
self.assertFalse(check_for_language('de-DE.utf-8'))
|
2013-05-18 20:37:04 +08:00
|
|
|
|
2018-05-11 03:48:00 +08:00
|
|
|
def test_check_for_language_null(self):
|
|
|
|
self.assertIs(trans_null.check_for_language('en'), True)
|
|
|
|
|
2013-05-18 20:37:04 +08:00
|
|
|
def test_get_language_from_request(self):
|
2013-05-19 17:44:46 +08:00
|
|
|
# issue 19919
|
2013-05-18 20:37:04 +08:00
|
|
|
r = self.rf.get('/')
|
|
|
|
r.COOKIES = {}
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,bg;q=0.6,ru;q=0.4'}
|
|
|
|
lang = get_language_from_request(r)
|
|
|
|
self.assertEqual('en-us', lang)
|
|
|
|
r = self.rf.get('/')
|
|
|
|
r.COOKIES = {}
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'bg-bg,en-US;q=0.8,en;q=0.6,ru;q=0.4'}
|
|
|
|
lang = get_language_from_request(r)
|
|
|
|
self.assertEqual('bg', lang)
|
2013-05-19 18:43:34 +08:00
|
|
|
|
2018-05-11 03:48:00 +08:00
|
|
|
def test_get_language_from_request_null(self):
|
|
|
|
lang = trans_null.get_language_from_request(None)
|
|
|
|
self.assertEqual(lang, 'en')
|
|
|
|
with override_settings(LANGUAGE_CODE='de'):
|
|
|
|
lang = trans_null.get_language_from_request(None)
|
|
|
|
self.assertEqual(lang, 'de')
|
|
|
|
|
2013-05-19 18:43:34 +08:00
|
|
|
def test_specific_language_codes(self):
|
|
|
|
# issue 11915
|
|
|
|
r = self.rf.get('/')
|
|
|
|
r.COOKIES = {}
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt,en-US;q=0.8,en;q=0.6,ru;q=0.4'}
|
|
|
|
lang = get_language_from_request(r)
|
|
|
|
self.assertEqual('pt-br', lang)
|
|
|
|
r = self.rf.get('/')
|
|
|
|
r.COOKIES = {}
|
|
|
|
r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt-pt,en-US;q=0.8,en;q=0.6,ru;q=0.4'}
|
|
|
|
lang = get_language_from_request(r)
|
|
|
|
self.assertEqual('pt-br', lang)
|
2014-05-01 00:04:30 +08:00
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class TranslationFilesMissing(SimpleTestCase):
|
2014-05-01 00:04:30 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUp()
|
2014-05-01 00:04:30 +08:00
|
|
|
self.gettext_find_builtin = gettext_module.find
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
gettext_module.find = self.gettext_find_builtin
|
2017-01-21 21:13:44 +08:00
|
|
|
super().tearDown()
|
2014-05-01 00:04:30 +08:00
|
|
|
|
|
|
|
def patchGettextFind(self):
|
|
|
|
gettext_module.find = lambda *args, **kw: None
|
|
|
|
|
|
|
|
def test_failure_finding_default_mo_files(self):
|
2019-01-28 23:01:35 +08:00
|
|
|
"""OSError is raised if the default language is unparseable."""
|
2014-05-01 00:04:30 +08:00
|
|
|
self.patchGettextFind()
|
|
|
|
trans_real._translations = {}
|
2019-01-28 23:01:35 +08:00
|
|
|
with self.assertRaises(OSError):
|
2016-01-17 19:26:39 +08:00
|
|
|
activate('en')
|
2015-12-19 00:00:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
class NonDjangoLanguageTests(SimpleTestCase):
|
|
|
|
"""
|
|
|
|
A language non present in default Django languages can still be
|
|
|
|
installed/used by a Django project.
|
|
|
|
"""
|
|
|
|
@override_settings(
|
|
|
|
USE_I18N=True,
|
|
|
|
LANGUAGES=[
|
|
|
|
('en-us', 'English'),
|
|
|
|
('xxx', 'Somelanguage'),
|
|
|
|
],
|
|
|
|
LANGUAGE_CODE='xxx',
|
|
|
|
LOCALE_PATHS=[os.path.join(here, 'commands', 'locale')],
|
|
|
|
)
|
|
|
|
def test_non_django_language(self):
|
|
|
|
self.assertEqual(get_language(), 'xxx')
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertEqual(gettext("year"), "reay")
|
2016-06-07 21:02:42 +08:00
|
|
|
|
2018-02-18 23:35:06 +08:00
|
|
|
@override_settings(USE_I18N=True)
|
2019-04-27 13:47:42 +08:00
|
|
|
def test_check_for_language(self):
|
2018-02-18 23:35:06 +08:00
|
|
|
with tempfile.TemporaryDirectory() as app_dir:
|
|
|
|
os.makedirs(os.path.join(app_dir, 'locale', 'dummy_Lang', 'LC_MESSAGES'))
|
|
|
|
open(os.path.join(app_dir, 'locale', 'dummy_Lang', 'LC_MESSAGES', 'django.mo'), 'w').close()
|
|
|
|
app_config = AppConfig('dummy_app', AppModuleStub(__path__=[app_dir]))
|
|
|
|
with mock.patch('django.apps.apps.get_app_configs', return_value=[app_config]):
|
|
|
|
self.assertIs(check_for_language('dummy-lang'), True)
|
|
|
|
|
2016-06-07 21:02:42 +08:00
|
|
|
@override_settings(
|
|
|
|
USE_I18N=True,
|
|
|
|
LANGUAGES=[
|
|
|
|
('en-us', 'English'),
|
|
|
|
# xyz language has no locale files
|
|
|
|
('xyz', 'XYZ'),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@translation.override('xyz')
|
|
|
|
def test_plural_non_django_language(self):
|
|
|
|
self.assertEqual(get_language(), 'xyz')
|
2017-01-27 03:58:33 +08:00
|
|
|
self.assertEqual(ngettext('year', 'years', 2), 'years')
|
2019-01-14 09:33:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(USE_I18N=True)
|
|
|
|
class WatchForTranslationChangesTests(SimpleTestCase):
|
|
|
|
@override_settings(USE_I18N=False)
|
|
|
|
def test_i18n_disabled(self):
|
|
|
|
mocked_sender = mock.MagicMock()
|
|
|
|
watch_for_translation_changes(mocked_sender)
|
|
|
|
mocked_sender.watch_dir.assert_not_called()
|
|
|
|
|
|
|
|
def test_i18n_enabled(self):
|
|
|
|
mocked_sender = mock.MagicMock()
|
|
|
|
watch_for_translation_changes(mocked_sender)
|
|
|
|
self.assertGreater(mocked_sender.watch_dir.call_count, 1)
|
|
|
|
|
|
|
|
def test_i18n_locale_paths(self):
|
|
|
|
mocked_sender = mock.MagicMock()
|
|
|
|
with tempfile.TemporaryDirectory() as app_dir:
|
|
|
|
with self.settings(LOCALE_PATHS=[app_dir]):
|
|
|
|
watch_for_translation_changes(mocked_sender)
|
|
|
|
mocked_sender.watch_dir.assert_any_call(Path(app_dir), '**/*.mo')
|
|
|
|
|
|
|
|
def test_i18n_app_dirs(self):
|
|
|
|
mocked_sender = mock.MagicMock()
|
|
|
|
with self.settings(INSTALLED_APPS=['tests.i18n.sampleproject']):
|
|
|
|
watch_for_translation_changes(mocked_sender)
|
|
|
|
project_dir = Path(__file__).parent / 'sampleproject' / 'locale'
|
|
|
|
mocked_sender.watch_dir.assert_any_call(project_dir, '**/*.mo')
|
|
|
|
|
2020-04-19 04:32:19 +08:00
|
|
|
def test_i18n_app_dirs_ignore_django_apps(self):
|
|
|
|
mocked_sender = mock.MagicMock()
|
|
|
|
with self.settings(INSTALLED_APPS=['django.contrib.admin']):
|
|
|
|
watch_for_translation_changes(mocked_sender)
|
|
|
|
mocked_sender.watch_dir.assert_called_once_with(Path('locale'), '**/*.mo')
|
|
|
|
|
2019-01-14 09:33:47 +08:00
|
|
|
def test_i18n_local_locale(self):
|
|
|
|
mocked_sender = mock.MagicMock()
|
|
|
|
watch_for_translation_changes(mocked_sender)
|
|
|
|
locale_dir = Path(__file__).parent / 'locale'
|
|
|
|
mocked_sender.watch_dir.assert_any_call(locale_dir, '**/*.mo')
|
|
|
|
|
|
|
|
|
|
|
|
class TranslationFileChangedTests(SimpleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.gettext_translations = gettext_module._translations.copy()
|
|
|
|
self.trans_real_translations = trans_real._translations.copy()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
gettext._translations = self.gettext_translations
|
|
|
|
trans_real._translations = self.trans_real_translations
|
|
|
|
|
|
|
|
def test_ignores_non_mo_files(self):
|
|
|
|
gettext_module._translations = {'foo': 'bar'}
|
|
|
|
path = Path('test.py')
|
|
|
|
self.assertIsNone(translation_file_changed(None, path))
|
|
|
|
self.assertEqual(gettext_module._translations, {'foo': 'bar'})
|
|
|
|
|
|
|
|
def test_resets_cache_with_mo_files(self):
|
|
|
|
gettext_module._translations = {'foo': 'bar'}
|
|
|
|
trans_real._translations = {'foo': 'bar'}
|
|
|
|
trans_real._default = 1
|
|
|
|
trans_real._active = False
|
|
|
|
path = Path('test.mo')
|
|
|
|
self.assertIs(translation_file_changed(None, path), True)
|
|
|
|
self.assertEqual(gettext_module._translations, {})
|
|
|
|
self.assertEqual(trans_real._translations, {})
|
|
|
|
self.assertIsNone(trans_real._default)
|
2019-04-12 21:15:18 +08:00
|
|
|
self.assertIsInstance(trans_real._active, Local)
|
2019-06-10 03:48:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
class UtilsTests(SimpleTestCase):
|
|
|
|
def test_round_away_from_one(self):
|
|
|
|
tests = [
|
|
|
|
(0, 0),
|
|
|
|
(0., 0),
|
|
|
|
(0.25, 0),
|
|
|
|
(0.5, 0),
|
|
|
|
(0.75, 0),
|
|
|
|
(1, 1),
|
|
|
|
(1., 1),
|
|
|
|
(1.25, 2),
|
|
|
|
(1.5, 2),
|
|
|
|
(1.75, 2),
|
|
|
|
(-0., 0),
|
|
|
|
(-0.25, -1),
|
|
|
|
(-0.5, -1),
|
|
|
|
(-0.75, -1),
|
|
|
|
(-1, -1),
|
|
|
|
(-1., -1),
|
|
|
|
(-1.25, -2),
|
|
|
|
(-1.5, -2),
|
|
|
|
(-1.75, -2),
|
|
|
|
]
|
|
|
|
for value, expected in tests:
|
|
|
|
with self.subTest(value=value):
|
|
|
|
self.assertEqual(round_away_from_one(value), expected)
|