Debian Codegraph @

tests/i18n/test_percents.py

raw
1 import os
2
3 from django.template import Context, Template
4 from django.test import SimpleTestCase, override_settings
5 from django.utils.translation import activate, get_language, trans_real
6
7 from .utils import POFileAssertionMixin
8
9 SAMPLEPROJECT_DIR = os.path.join(
10 os.path.dirname(os.path.abspath(__file__)), "sampleproject"
11 )
12 SAMPLEPROJECT_LOCALE = os.path.join(SAMPLEPROJECT_DIR, "locale")
13
14
15 @override_settings(LOCALE_PATHS=[SAMPLEPROJECT_LOCALE])
16 class FrenchTestCase(SimpleTestCase):
17 """Tests using the French translations of the sampleproject."""
18
19 PO_FILE = os.path.join(SAMPLEPROJECT_LOCALE, "fr", "LC_MESSAGES", "django.po")
20
21 def setUp(self):
22 self._language = get_language()
23 self._translations = trans_real._translations
24 activate("fr")
25
26 def tearDown(self):
27 trans_real._translations = self._translations
28 activate(self._language)
29
30
31 class ExtractingStringsWithPercentSigns(POFileAssertionMixin, FrenchTestCase):
32 """
33 Tests the extracted string found in the gettext catalog.
34
35 Percent signs are python formatted.
36
37 These tests should all have an analogous translation tests below, ensuring
38 the Python formatting does not persist through to a rendered template.
39 """
40
41 def setUp(self):
42 super().setUp()
43 with open(self.PO_FILE) as fp:
44 self.po_contents = fp.read()
45
46 def test_trans_tag_with_percent_symbol_at_the_end(self):
47 self.assertMsgId(
48 "Literal with a percent symbol at the end %%", self.po_contents
49 )
50
51 def test_trans_tag_with_percent_symbol_in_the_middle(self):
52 self.assertMsgId(
53 "Literal with a percent %% symbol in the middle", self.po_contents
54 )
55 self.assertMsgId("It is 100%%", self.po_contents)
56
57 def test_trans_tag_with_string_that_look_like_fmt_spec(self):
58 self.assertMsgId(
59 "Looks like a str fmt spec %%s but should not be interpreted as such",
60 self.po_contents,
61 )
62 self.assertMsgId(
63 "Looks like a str fmt spec %% o but should not be interpreted as such",
64 self.po_contents,
65 )
66
67 def test_adds_python_format_to_all_percent_signs(self):
68 self.assertMsgId(
69 "1 percent sign %%, 2 percent signs %%%%, 3 percent signs %%%%%%",
70 self.po_contents,
71 )
72 self.assertMsgId(
73 "%(name)s says: 1 percent sign %%, 2 percent signs %%%%", self.po_contents
74 )
75
76
77 class RenderingTemplatesWithPercentSigns(FrenchTestCase):
78 """
79 Test rendering of templates that use percent signs.
80
81 Ensures both translate and blocktranslate tags behave consistently.
82
83 Refs #11240, #11966, #24257
84 """
85
86 def test_translates_with_a_percent_symbol_at_the_end(self):
87 expected = "Littérale avec un symbole de pour cent à la fin %"
88
89 trans_tpl = Template(
90 "{% load i18n %}"
91 '{% translate "Literal with a percent symbol at the end %" %}'
92 )
93 self.assertEqual(trans_tpl.render(Context({})), expected)
94
95 block_tpl = Template(
96 "{% load i18n %}{% blocktranslate %}Literal with a percent symbol at "
97 "the end %{% endblocktranslate %}"
98 )
99 self.assertEqual(block_tpl.render(Context({})), expected)
100
101 def test_translates_with_percent_symbol_in_the_middle(self):
102 expected = "Pour cent littérale % avec un symbole au milieu"
103
104 trans_tpl = Template(
105 "{% load i18n %}"
106 '{% translate "Literal with a percent % symbol in the middle" %}'
107 )
108 self.assertEqual(trans_tpl.render(Context({})), expected)
109
110 block_tpl = Template(
111 "{% load i18n %}{% blocktranslate %}Literal with a percent % symbol "
112 "in the middle{% endblocktranslate %}"
113 )
114 self.assertEqual(block_tpl.render(Context({})), expected)
115
116 def test_translates_with_percent_symbol_using_context(self):
117 trans_tpl = Template('{% load i18n %}{% translate "It is 100%" %}')
118 self.assertEqual(trans_tpl.render(Context({})), "Il est de 100%")
119 trans_tpl = Template(
120 '{% load i18n %}{% translate "It is 100%" context "female" %}'
121 )
122 self.assertEqual(trans_tpl.render(Context({})), "Elle est de 100%")
123
124 block_tpl = Template(
125 "{% load i18n %}{% blocktranslate %}It is 100%{% endblocktranslate %}"
126 )
127 self.assertEqual(block_tpl.render(Context({})), "Il est de 100%")
128 block_tpl = Template(
129 "{% load i18n %}"
130 '{% blocktranslate context "female" %}It is 100%{% endblocktranslate %}'
131 )
132 self.assertEqual(block_tpl.render(Context({})), "Elle est de 100%")
133
134 def test_translates_with_string_that_look_like_fmt_spec_with_trans(self):
135 # tests "%s"
136 expected = (
137 "On dirait un spec str fmt %s mais ne devrait pas être interprété comme "
138 "plus disponible"
139 )
140 trans_tpl = Template(
141 '{% load i18n %}{% translate "Looks like a str fmt spec %s but '
142 'should not be interpreted as such" %}'
143 )
144 self.assertEqual(trans_tpl.render(Context({})), expected)
145 block_tpl = Template(
146 "{% load i18n %}{% blocktranslate %}Looks like a str fmt spec %s but "
147 "should not be interpreted as such{% endblocktranslate %}"
148 )
149 self.assertEqual(block_tpl.render(Context({})), expected)
150
151 # tests "% o"
152 expected = (
153 "On dirait un spec str fmt % o mais ne devrait pas être interprété comme "
154 "plus disponible"
155 )
156 trans_tpl = Template(
157 "{% load i18n %}"
158 '{% translate "Looks like a str fmt spec % o but should not be '
159 'interpreted as such" %}'
160 )
161 self.assertEqual(trans_tpl.render(Context({})), expected)
162 block_tpl = Template(
163 "{% load i18n %}"
164 "{% blocktranslate %}Looks like a str fmt spec % o but should not be "
165 "interpreted as such{% endblocktranslate %}"
166 )
167 self.assertEqual(block_tpl.render(Context({})), expected)
168
169 def test_translates_multiple_percent_signs(self):
170 expected = (
171 "1 % signe pour cent, signes %% 2 pour cent, trois signes de pourcentage "
172 "%%%"
173 )
174 trans_tpl = Template(
175 '{% load i18n %}{% translate "1 percent sign %, 2 percent signs %%, '
176 '3 percent signs %%%" %}'
177 )
178 self.assertEqual(trans_tpl.render(Context({})), expected)
179 block_tpl = Template(
180 "{% load i18n %}{% blocktranslate %}1 percent sign %, 2 percent signs "
181 "%%, 3 percent signs %%%{% endblocktranslate %}"
182 )
183 self.assertEqual(block_tpl.render(Context({})), expected)
184
185 block_tpl = Template(
186 "{% load i18n %}{% blocktranslate %}{{name}} says: 1 percent sign %, "
187 "2 percent signs %%{% endblocktranslate %}"
188 )
189 self.assertEqual(
190 block_tpl.render(Context({"name": "Django"})),
191 "Django dit: 1 pour cent signe %, deux signes de pourcentage %%",
192 )