|
16 | 16 | from django.conf.urls.i18n import i18n_patterns
|
17 | 17 | from django.template import Context, Template
|
18 | 18 | from django.test import (
|
19 |
| - RequestFactory, SimpleTestCase, TestCase, override_settings, |
| 19 | + RequestFactory, SimpleTestCase, TestCase, ignore_warnings, |
| 20 | + override_settings, |
20 | 21 | )
|
21 | 22 | from django.utils import six, translation
|
22 | 23 | from django.utils._os import upath
|
| 24 | +from django.utils.deprecation import RemovedInDjango21Warning |
23 | 25 | from django.utils.formats import (
|
24 | 26 | date_format, get_format, get_format_modules, iter_format_modules, localize,
|
25 | 27 | localize_input, reset_format_cache, sanitize_separators, time_format,
|
26 | 28 | )
|
27 | 29 | from django.utils.numberformat import format as nformat
|
28 |
| -from django.utils.safestring import SafeBytes, SafeText |
| 30 | +from django.utils.safestring import SafeBytes, SafeString, SafeText, mark_safe |
29 | 31 | from django.utils.six import PY3
|
30 | 32 | from django.utils.translation import (
|
31 | 33 | LANGUAGE_SESSION_KEY, activate, check_for_language, deactivate,
|
32 |
| - get_language, get_language_from_request, get_language_info, gettext, |
33 |
| - gettext_lazy, ngettext_lazy, npgettext, npgettext_lazy, pgettext, |
34 |
| - pgettext_lazy, trans_real, ugettext, ugettext_lazy, ungettext, |
35 |
| - ungettext_lazy, |
| 34 | + get_language, get_language_bidi, get_language_from_request, |
| 35 | + get_language_info, gettext, gettext_lazy, ngettext_lazy, npgettext, |
| 36 | + npgettext_lazy, pgettext, pgettext_lazy, string_concat, to_locale, |
| 37 | + trans_real, ugettext, ugettext_lazy, ungettext, ungettext_lazy, |
36 | 38 | )
|
37 | 39 |
|
38 | 40 | from .forms import CompanyForm, I18nForm, SelectDateForm
|
@@ -262,6 +264,57 @@ def test_pgettext(self):
|
262 | 264 | self.assertEqual(pgettext("verb", "May"), "Kann")
|
263 | 265 | self.assertEqual(npgettext("search", "%d result", "%d results", 4) % 4, "4 Resultate")
|
264 | 266 |
|
| 267 | + @ignore_warnings(category=RemovedInDjango21Warning) |
| 268 | + def test_string_concat(self): |
| 269 | + self.assertEqual(str(string_concat('dja', 'ngo')), 'django') |
| 270 | + |
| 271 | + def test_empty_value(self): |
| 272 | + """Empty value must stay empty after being translated (#23196).""" |
| 273 | + with translation.override('de'): |
| 274 | + self.assertEqual('', gettext('')) |
| 275 | + s = mark_safe('') |
| 276 | + self.assertEqual(s, gettext(s)) |
| 277 | + |
| 278 | + def test_safe_status(self): |
| 279 | + """ |
| 280 | + Translating a string requiring no auto-escaping shouldn't change the |
| 281 | + "safe" status. |
| 282 | + """ |
| 283 | + s = mark_safe(str('Password')) |
| 284 | + self.assertIs(type(s), SafeString) |
| 285 | + with translation.override('de', deactivate=True): |
| 286 | + self.assertIs(type(ugettext(s)), SafeText) |
| 287 | + self.assertEqual('aPassword', SafeText('a') + s) |
| 288 | + self.assertEqual('Passworda', s + SafeText('a')) |
| 289 | + self.assertEqual('Passworda', s + mark_safe('a')) |
| 290 | + self.assertEqual('aPassword', mark_safe('a') + s) |
| 291 | + self.assertEqual('as', mark_safe('a') + mark_safe('s')) |
| 292 | + |
| 293 | + def test_maclines(self): |
| 294 | + """ |
| 295 | + Translations on files with Mac or DOS end of lines will be converted |
| 296 | + to unix EOF in .po catalogs. |
| 297 | + """ |
| 298 | + ca_translation = trans_real.translation('ca') |
| 299 | + ca_translation._catalog['Mac\nEOF\n'] = 'Catalan Mac\nEOF\n' |
| 300 | + ca_translation._catalog['Win\nEOF\n'] = 'Catalan Win\nEOF\n' |
| 301 | + with translation.override('ca', deactivate=True): |
| 302 | + self.assertEqual('Catalan Mac\nEOF\n', gettext('Mac\rEOF\r')) |
| 303 | + self.assertEqual('Catalan Win\nEOF\n', gettext('Win\r\nEOF\r\n')) |
| 304 | + |
| 305 | + def test_to_locale(self): |
| 306 | + self.assertEqual(to_locale('en-us'), 'en_US') |
| 307 | + self.assertEqual(to_locale('sr-lat'), 'sr_Lat') |
| 308 | + |
| 309 | + def test_to_language(self): |
| 310 | + self.assertEqual(trans_real.to_language('en_US'), 'en-us') |
| 311 | + self.assertEqual(trans_real.to_language('sr_Lat'), 'sr-lat') |
| 312 | + |
| 313 | + def test_language_bidi(self): |
| 314 | + self.assertIs(get_language_bidi(), False) |
| 315 | + with translation.override(None): |
| 316 | + self.assertIs(get_language_bidi(), False) |
| 317 | + |
265 | 318 |
|
266 | 319 | class TranslationThreadSafetyTests(SimpleTestCase):
|
267 | 320 |
|
|
0 commit comments