2015-04-16 05:01:11 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
from django.template import Context, Template
|
|
|
|
from django.test import SimpleTestCase, override_settings
|
|
|
|
from django.utils.translation import activate, get_language, trans_real
|
|
|
|
|
2016-06-09 21:08:31 +08:00
|
|
|
from .utils import POFileAssertionMixin
|
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
SAMPLEPROJECT_DIR = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)), "sampleproject"
|
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
SAMPLEPROJECT_LOCALE = os.path.join(SAMPLEPROJECT_DIR, "locale")
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=[SAMPLEPROJECT_LOCALE])
|
|
|
|
class FrenchTestCase(SimpleTestCase):
|
|
|
|
"""Tests using the French translations of the sampleproject."""
|
|
|
|
|
|
|
|
PO_FILE = os.path.join(SAMPLEPROJECT_LOCALE, "fr", "LC_MESSAGES", "django.po")
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self._language = get_language()
|
|
|
|
self._translations = trans_real._translations
|
|
|
|
activate("fr")
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
trans_real._translations = self._translations
|
|
|
|
activate(self._language)
|
|
|
|
|
|
|
|
|
2016-05-29 22:25:05 +08:00
|
|
|
class ExtractingStringsWithPercentSigns(POFileAssertionMixin, FrenchTestCase):
|
2015-04-16 05:01:11 +08:00
|
|
|
"""
|
|
|
|
Tests the extracted string found in the gettext catalog.
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
Percent signs are python formatted.
|
2015-04-16 05:01:11 +08:00
|
|
|
|
|
|
|
These tests should all have an analogous translation tests below, ensuring
|
2018-10-09 21:26:07 +08:00
|
|
|
the Python formatting does not persist through to a rendered template.
|
2015-04-16 05:01:11 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUp()
|
2019-01-28 00:30:20 +08:00
|
|
|
with open(self.PO_FILE) as fp:
|
2017-01-24 19:22:42 +08:00
|
|
|
self.po_contents = fp.read()
|
2015-04-16 05:01:11 +08:00
|
|
|
|
|
|
|
def test_trans_tag_with_percent_symbol_at_the_end(self):
|
|
|
|
self.assertMsgId(
|
|
|
|
"Literal with a percent symbol at the end %%", self.po_contents
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_trans_tag_with_percent_symbol_in_the_middle(self):
|
|
|
|
self.assertMsgId(
|
|
|
|
"Literal with a percent %% symbol in the middle", self.po_contents
|
|
|
|
)
|
|
|
|
self.assertMsgId("It is 100%%", self.po_contents)
|
|
|
|
|
|
|
|
def test_trans_tag_with_string_that_look_like_fmt_spec(self):
|
|
|
|
self.assertMsgId(
|
|
|
|
"Looks like a str fmt spec %%s but should not be interpreted as such",
|
|
|
|
self.po_contents,
|
|
|
|
)
|
|
|
|
self.assertMsgId(
|
|
|
|
"Looks like a str fmt spec %% o but should not be interpreted as such",
|
|
|
|
self.po_contents,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_adds_python_format_to_all_percent_signs(self):
|
|
|
|
self.assertMsgId(
|
|
|
|
"1 percent sign %%, 2 percent signs %%%%, 3 percent signs %%%%%%",
|
|
|
|
self.po_contents,
|
|
|
|
)
|
|
|
|
self.assertMsgId(
|
|
|
|
"%(name)s says: 1 percent sign %%, 2 percent signs %%%%", self.po_contents
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RenderingTemplatesWithPercentSigns(FrenchTestCase):
|
|
|
|
"""
|
|
|
|
Test rendering of templates that use percent signs.
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
Ensures both translate and blocktranslate tags behave consistently.
|
2015-04-16 05:01:11 +08:00
|
|
|
|
|
|
|
Refs #11240, #11966, #24257
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_translates_with_a_percent_symbol_at_the_end(self):
|
|
|
|
expected = "Littérale avec un symbole de pour cent à la fin %"
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
trans_tpl = Template(
|
|
|
|
"{% load i18n %}"
|
|
|
|
'{% translate "Literal with a percent symbol at the end %" %}'
|
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
self.assertEqual(trans_tpl.render(Context({})), expected)
|
|
|
|
|
|
|
|
block_tpl = Template(
|
2019-09-23 23:31:21 +08:00
|
|
|
"{% load i18n %}{% blocktranslate %}Literal with a percent symbol at "
|
|
|
|
"the end %{% endblocktranslate %}"
|
2015-04-16 05:01:11 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(block_tpl.render(Context({})), expected)
|
|
|
|
|
|
|
|
def test_translates_with_percent_symbol_in_the_middle(self):
|
|
|
|
expected = "Pour cent littérale % avec un symbole au milieu"
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
trans_tpl = Template(
|
|
|
|
"{% load i18n %}"
|
|
|
|
'{% translate "Literal with a percent % symbol in the middle" %}'
|
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
self.assertEqual(trans_tpl.render(Context({})), expected)
|
|
|
|
|
|
|
|
block_tpl = Template(
|
2019-09-23 23:31:21 +08:00
|
|
|
"{% load i18n %}{% blocktranslate %}Literal with a percent % symbol "
|
|
|
|
"in the middle{% endblocktranslate %}"
|
2015-04-16 05:01:11 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(block_tpl.render(Context({})), expected)
|
|
|
|
|
|
|
|
def test_translates_with_percent_symbol_using_context(self):
|
2019-09-23 23:31:21 +08:00
|
|
|
trans_tpl = Template('{% load i18n %}{% translate "It is 100%" %}')
|
2015-04-16 05:01:11 +08:00
|
|
|
self.assertEqual(trans_tpl.render(Context({})), "Il est de 100%")
|
2019-09-23 23:31:21 +08:00
|
|
|
trans_tpl = Template(
|
|
|
|
'{% load i18n %}{% translate "It is 100%" context "female" %}'
|
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
self.assertEqual(trans_tpl.render(Context({})), "Elle est de 100%")
|
|
|
|
|
2019-09-23 23:31:21 +08:00
|
|
|
block_tpl = Template(
|
|
|
|
"{% load i18n %}{% blocktranslate %}It is 100%{% endblocktranslate %}"
|
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
self.assertEqual(block_tpl.render(Context({})), "Il est de 100%")
|
2019-09-23 23:31:21 +08:00
|
|
|
block_tpl = Template(
|
|
|
|
"{% load i18n %}"
|
|
|
|
'{% blocktranslate context "female" %}It is 100%{% endblocktranslate %}'
|
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
self.assertEqual(block_tpl.render(Context({})), "Elle est de 100%")
|
|
|
|
|
|
|
|
def test_translates_with_string_that_look_like_fmt_spec_with_trans(self):
|
|
|
|
# tests "%s"
|
|
|
|
expected = (
|
|
|
|
"On dirait un spec str fmt %s mais ne devrait pas être interprété comme "
|
|
|
|
"plus disponible"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
trans_tpl = Template(
|
2019-09-23 23:31:21 +08:00
|
|
|
'{% load i18n %}{% translate "Looks like a str fmt spec %s but '
|
2015-04-16 05:01:11 +08:00
|
|
|
'should not be interpreted as such" %}'
|
|
|
|
)
|
|
|
|
self.assertEqual(trans_tpl.render(Context({})), expected)
|
|
|
|
block_tpl = Template(
|
2019-09-23 23:31:21 +08:00
|
|
|
"{% load i18n %}{% blocktranslate %}Looks like a str fmt spec %s but "
|
|
|
|
"should not be interpreted as such{% endblocktranslate %}"
|
2015-04-16 05:01:11 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(block_tpl.render(Context({})), expected)
|
|
|
|
|
|
|
|
# tests "% o"
|
|
|
|
expected = (
|
|
|
|
"On dirait un spec str fmt % o mais ne devrait pas être interprété comme "
|
|
|
|
"plus disponible"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
trans_tpl = Template(
|
2019-09-23 23:31:21 +08:00
|
|
|
"{% load i18n %}"
|
|
|
|
'{% translate "Looks like a str fmt spec % o but should not be '
|
2015-04-16 05:01:11 +08:00
|
|
|
'interpreted as such" %}'
|
|
|
|
)
|
|
|
|
self.assertEqual(trans_tpl.render(Context({})), expected)
|
|
|
|
block_tpl = Template(
|
2019-09-23 23:31:21 +08:00
|
|
|
"{% load i18n %}"
|
|
|
|
"{% blocktranslate %}Looks like a str fmt spec % o but should not be "
|
|
|
|
"interpreted as such{% endblocktranslate %}"
|
2015-04-16 05:01:11 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(block_tpl.render(Context({})), expected)
|
|
|
|
|
|
|
|
def test_translates_multiple_percent_signs(self):
|
|
|
|
expected = (
|
|
|
|
"1 % signe pour cent, signes %% 2 pour cent, trois signes de pourcentage "
|
|
|
|
"%%%"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2015-04-16 05:01:11 +08:00
|
|
|
trans_tpl = Template(
|
2019-09-23 23:31:21 +08:00
|
|
|
'{% load i18n %}{% translate "1 percent sign %, 2 percent signs %%, '
|
2015-04-16 05:01:11 +08:00
|
|
|
'3 percent signs %%%" %}'
|
|
|
|
)
|
|
|
|
self.assertEqual(trans_tpl.render(Context({})), expected)
|
|
|
|
block_tpl = Template(
|
2019-09-23 23:31:21 +08:00
|
|
|
"{% load i18n %}{% blocktranslate %}1 percent sign %, 2 percent signs "
|
|
|
|
"%%, 3 percent signs %%%{% endblocktranslate %}"
|
2015-04-16 05:01:11 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(block_tpl.render(Context({})), expected)
|
|
|
|
|
|
|
|
block_tpl = Template(
|
2019-09-23 23:31:21 +08:00
|
|
|
"{% load i18n %}{% blocktranslate %}{{name}} says: 1 percent sign %, "
|
|
|
|
"2 percent signs %%{% endblocktranslate %}"
|
2015-04-16 05:01:11 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
block_tpl.render(Context({"name": "Django"})),
|
|
|
|
"Django dit: 1 pour cent signe %, deux signes de pourcentage %%",
|
|
|
|
)
|