Debian Codegraph @

tests/messages/frontend/test_init.py

raw
1 #
2 # Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team
3 # All rights reserved.
4 #
5 # This software is licensed as described in the file LICENSE, which
6 # you should have received as part of this distribution. The terms
7 # are also available at https://github.com/python-babel/babel/blob/master/LICENSE.
8 #
9 # This software consists of voluntary contributions made by many
10 # individuals. For the exact contribution history, see the revision
11 # history and logs, available at https://github.com/python-babel/babel/commits/master/.
12
13 from __future__ import annotations
14
15 import os
16 import shutil
17 from datetime import datetime
18
19 import pytest
20 from freezegun import freeze_time
21
22 from babel import __version__ as VERSION
23 from babel.dates import format_datetime
24 from babel.messages import frontend
25 from babel.util import LOCALTZ
26 from tests.messages.consts import (
27 TEST_PROJECT_DISTRIBUTION_DATA,
28 data_dir,
29 get_po_file_path,
30 i18n_dir,
31 )
32 from tests.messages.utils import Distribution
33
34
35 @pytest.fixture
36 def init_cmd(monkeypatch):
37 monkeypatch.chdir(data_dir)
38 dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
39 init_cmd = frontend.InitCatalog(dist)
40 init_cmd.initialize_options()
41 yield init_cmd
42 for dirname in ['en_US', 'ja_JP', 'lv_LV']:
43 locale_dir = os.path.join(i18n_dir, dirname)
44 if os.path.isdir(locale_dir):
45 shutil.rmtree(locale_dir)
46
47
48 def test_no_input_file(init_cmd):
49 init_cmd.locale = 'en_US'
50 init_cmd.output_file = 'dummy'
51 with pytest.raises(frontend.OptionError):
52 init_cmd.finalize_options()
53
54
55 def test_no_locale(init_cmd):
56 init_cmd.input_file = 'dummy'
57 init_cmd.output_file = 'dummy'
58 with pytest.raises(frontend.OptionError):
59 init_cmd.finalize_options()
60
61
62 @freeze_time("1994-11-11")
63 def test_with_output_dir(init_cmd):
64 init_cmd.input_file = 'project/i18n/messages.pot'
65 init_cmd.locale = 'en_US'
66 init_cmd.output_dir = 'project/i18n'
67
68 init_cmd.finalize_options()
69 init_cmd.run()
70
71 date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
72 expected_content = fr"""# English (United States) translations for TestProject.
73 # Copyright (C) 2007 FooBar, Inc.
74 # This file is distributed under the same license as the TestProject
75 # project.
76 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
77 #
78 msgid ""
79 msgstr ""
80 "Project-Id-Version: TestProject 0.1\n"
81 "Report-Msgid-Bugs-To: [email protected]\n"
82 "POT-Creation-Date: 2007-04-01 15:30+0200\n"
83 "PO-Revision-Date: {date}\n"
84 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
85 "Language: en_US\n"
86 "Language-Team: en_US <[email protected]>\n"
87 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
88 "MIME-Version: 1.0\n"
89 "Content-Type: text/plain; charset=utf-8\n"
90 "Content-Transfer-Encoding: 8bit\n"
91 "Generated-By: Babel {VERSION}\n"
92
93 #. This will be a translator coment,
94 #. that will include several lines
95 #: project/file1.py:8
96 msgid "bar"
97 msgstr ""
98
99 #: project/file2.py:9
100 msgid "foobar"
101 msgid_plural "foobars"
102 msgstr[0] ""
103 msgstr[1] ""
104
105 """
106 with open(get_po_file_path('en_US')) as f:
107 actual_content = f.read()
108 assert expected_content == actual_content
109
110
111 @freeze_time("1994-11-11")
112 def test_keeps_catalog_non_fuzzy(init_cmd):
113 init_cmd.input_file = 'project/i18n/messages_non_fuzzy.pot'
114 init_cmd.locale = 'en_US'
115 init_cmd.output_dir = 'project/i18n'
116
117 init_cmd.finalize_options()
118 init_cmd.run()
119
120 date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
121 expected_content = fr"""# English (United States) translations for TestProject.
122 # Copyright (C) 2007 FooBar, Inc.
123 # This file is distributed under the same license as the TestProject
124 # project.
125 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
126 #
127 msgid ""
128 msgstr ""
129 "Project-Id-Version: TestProject 0.1\n"
130 "Report-Msgid-Bugs-To: [email protected]\n"
131 "POT-Creation-Date: 2007-04-01 15:30+0200\n"
132 "PO-Revision-Date: {date}\n"
133 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
134 "Language: en_US\n"
135 "Language-Team: en_US <[email protected]>\n"
136 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
137 "MIME-Version: 1.0\n"
138 "Content-Type: text/plain; charset=utf-8\n"
139 "Content-Transfer-Encoding: 8bit\n"
140 "Generated-By: Babel {VERSION}\n"
141
142 #. This will be a translator coment,
143 #. that will include several lines
144 #: project/file1.py:8
145 msgid "bar"
146 msgstr ""
147
148 #: project/file2.py:9
149 msgid "foobar"
150 msgid_plural "foobars"
151 msgstr[0] ""
152 msgstr[1] ""
153
154 """
155 with open(get_po_file_path('en_US')) as f:
156 actual_content = f.read()
157 assert expected_content == actual_content
158
159
160 @freeze_time("1994-11-11")
161 def test_correct_init_more_than_2_plurals(init_cmd):
162 init_cmd.input_file = 'project/i18n/messages.pot'
163 init_cmd.locale = 'lv_LV'
164 init_cmd.output_dir = 'project/i18n'
165
166 init_cmd.finalize_options()
167 init_cmd.run()
168
169 date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
170 expected_content = fr"""# Latvian (Latvia) translations for TestProject.
171 # Copyright (C) 2007 FooBar, Inc.
172 # This file is distributed under the same license as the TestProject
173 # project.
174 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
175 #
176 msgid ""
177 msgstr ""
178 "Project-Id-Version: TestProject 0.1\n"
179 "Report-Msgid-Bugs-To: [email protected]\n"
180 "POT-Creation-Date: 2007-04-01 15:30+0200\n"
181 "PO-Revision-Date: {date}\n"
182 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
183 "Language: lv_LV\n"
184 "Language-Team: lv_LV <[email protected]>\n"
185 "Plural-Forms: nplurals=3; plural=(n % 10 == 0 || n % 100 >= 11 && n % 100"
186 " <= 19 ? 0 : n % 10 == 1 && n % 100 != 11 ? 1 : 2);\n"
187 "MIME-Version: 1.0\n"
188 "Content-Type: text/plain; charset=utf-8\n"
189 "Content-Transfer-Encoding: 8bit\n"
190 "Generated-By: Babel {VERSION}\n"
191
192 #. This will be a translator coment,
193 #. that will include several lines
194 #: project/file1.py:8
195 msgid "bar"
196 msgstr ""
197
198 #: project/file2.py:9
199 msgid "foobar"
200 msgid_plural "foobars"
201 msgstr[0] ""
202 msgstr[1] ""
203 msgstr[2] ""
204
205 """
206 with open(get_po_file_path('lv_LV')) as f:
207 actual_content = f.read()
208 assert expected_content == actual_content
209
210
211 @freeze_time("1994-11-11")
212 def test_correct_init_singular_plural_forms(init_cmd):
213 init_cmd.input_file = 'project/i18n/messages.pot'
214 init_cmd.locale = 'ja_JP'
215 init_cmd.output_dir = 'project/i18n'
216
217 init_cmd.finalize_options()
218 init_cmd.run()
219
220 date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='ja_JP')
221 expected_content = fr"""# Japanese (Japan) translations for TestProject.
222 # Copyright (C) 2007 FooBar, Inc.
223 # This file is distributed under the same license as the TestProject
224 # project.
225 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
226 #
227 msgid ""
228 msgstr ""
229 "Project-Id-Version: TestProject 0.1\n"
230 "Report-Msgid-Bugs-To: [email protected]\n"
231 "POT-Creation-Date: 2007-04-01 15:30+0200\n"
232 "PO-Revision-Date: {date}\n"
233 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
234 "Language: ja_JP\n"
235 "Language-Team: ja_JP <[email protected]>\n"
236 "Plural-Forms: nplurals=1; plural=0;\n"
237 "MIME-Version: 1.0\n"
238 "Content-Type: text/plain; charset=utf-8\n"
239 "Content-Transfer-Encoding: 8bit\n"
240 "Generated-By: Babel {VERSION}\n"
241
242 #. This will be a translator coment,
243 #. that will include several lines
244 #: project/file1.py:8
245 msgid "bar"
246 msgstr ""
247
248 #: project/file2.py:9
249 msgid "foobar"
250 msgid_plural "foobars"
251 msgstr[0] ""
252
253 """
254 with open(get_po_file_path('ja_JP')) as f:
255 actual_content = f.read()
256 assert expected_content == actual_content
257
258
259 @freeze_time("1994-11-11")
260 def test_supports_no_wrap(init_cmd):
261 init_cmd.input_file = 'project/i18n/long_messages.pot'
262 init_cmd.locale = 'en_US'
263 init_cmd.output_dir = 'project/i18n'
264
265 long_message = '"' + 'xxxxx ' * 15 + '"'
266
267 with open('project/i18n/messages.pot', 'rb') as f:
268 pot_contents = f.read().decode('latin-1')
269 pot_with_very_long_line = pot_contents.replace('"bar"', long_message)
270 with open(init_cmd.input_file, 'wb') as f:
271 f.write(pot_with_very_long_line.encode('latin-1'))
272 init_cmd.no_wrap = True
273
274 init_cmd.finalize_options()
275 init_cmd.run()
276
277 date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
278 expected_content = fr"""# English (United States) translations for TestProject.
279 # Copyright (C) 2007 FooBar, Inc.
280 # This file is distributed under the same license as the TestProject
281 # project.
282 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
283 #
284 msgid ""
285 msgstr ""
286 "Project-Id-Version: TestProject 0.1\n"
287 "Report-Msgid-Bugs-To: [email protected]\n"
288 "POT-Creation-Date: 2007-04-01 15:30+0200\n"
289 "PO-Revision-Date: {date}\n"
290 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
291 "Language: en_US\n"
292 "Language-Team: en_US <[email protected]>\n"
293 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
294 "MIME-Version: 1.0\n"
295 "Content-Type: text/plain; charset=utf-8\n"
296 "Content-Transfer-Encoding: 8bit\n"
297 "Generated-By: Babel {VERSION}\n"
298
299 #. This will be a translator coment,
300 #. that will include several lines
301 #: project/file1.py:8
302 msgid {long_message}
303 msgstr ""
304
305 #: project/file2.py:9
306 msgid "foobar"
307 msgid_plural "foobars"
308 msgstr[0] ""
309 msgstr[1] ""
310
311 """
312 with open(get_po_file_path('en_US')) as f:
313 actual_content = f.read()
314 assert expected_content == actual_content
315
316
317 @freeze_time("1994-11-11")
318 def test_supports_width(init_cmd):
319 init_cmd.input_file = 'project/i18n/long_messages.pot'
320 init_cmd.locale = 'en_US'
321 init_cmd.output_dir = 'project/i18n'
322
323 long_message = '"' + 'xxxxx ' * 15 + '"'
324
325 with open('project/i18n/messages.pot', 'rb') as f:
326 pot_contents = f.read().decode('latin-1')
327 pot_with_very_long_line = pot_contents.replace('"bar"', long_message)
328 with open(init_cmd.input_file, 'wb') as f:
329 f.write(pot_with_very_long_line.encode('latin-1'))
330 init_cmd.width = 120
331 init_cmd.finalize_options()
332 init_cmd.run()
333
334 date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
335 expected_content = fr"""# English (United States) translations for TestProject.
336 # Copyright (C) 2007 FooBar, Inc.
337 # This file is distributed under the same license as the TestProject
338 # project.
339 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
340 #
341 msgid ""
342 msgstr ""
343 "Project-Id-Version: TestProject 0.1\n"
344 "Report-Msgid-Bugs-To: [email protected]\n"
345 "POT-Creation-Date: 2007-04-01 15:30+0200\n"
346 "PO-Revision-Date: {date}\n"
347 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
348 "Language: en_US\n"
349 "Language-Team: en_US <[email protected]>\n"
350 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
351 "MIME-Version: 1.0\n"
352 "Content-Type: text/plain; charset=utf-8\n"
353 "Content-Transfer-Encoding: 8bit\n"
354 "Generated-By: Babel {VERSION}\n"
355
356 #. This will be a translator coment,
357 #. that will include several lines
358 #: project/file1.py:8
359 msgid {long_message}
360 msgstr ""
361
362 #: project/file2.py:9
363 msgid "foobar"
364 msgid_plural "foobars"
365 msgstr[0] ""
366 msgstr[1] ""
367
368 """
369 with open(get_po_file_path('en_US')) as f:
370 actual_content = f.read()
371 assert expected_content == actual_content