Mercurial > babel > old > mirror
annotate 0.9.x/babel/messages/tests/frontend.py @ 544:1e1bcb890af1 stable
merge r593 from trunk
author | fschwarz |
---|---|
date | Wed, 16 Mar 2011 14:32:34 +0000 |
parents | 3c4e1592e40a |
children |
rev | line source |
---|---|
263 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2007 Edgewall Software | |
4 # All rights reserved. | |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
14 from datetime import datetime | |
15 from distutils.dist import Distribution | |
381 | 16 from distutils.errors import DistutilsOptionError |
263 | 17 from distutils.log import _global_log |
18 import doctest | |
526 | 19 import logging |
263 | 20 import os |
21 import shutil | |
22 from StringIO import StringIO | |
23 import sys | |
24 import time | |
25 import unittest | |
26 | |
27 from babel import __version__ as VERSION | |
28 from babel.dates import format_datetime | |
29 from babel.messages import frontend | |
30 from babel.util import LOCALTZ | |
31 | |
32 | |
33 class CompileCatalogTestCase(unittest.TestCase): | |
34 | |
35 def setUp(self): | |
36 self.olddir = os.getcwd() | |
37 self.datadir = os.path.join(os.path.dirname(__file__), 'data') | |
38 os.chdir(self.datadir) | |
39 _global_log.threshold = 5 # shut up distutils logging | |
40 | |
41 self.dist = Distribution(dict( | |
42 name='TestProject', | |
43 version='0.1', | |
44 packages=['project'] | |
45 )) | |
46 self.cmd = frontend.compile_catalog(self.dist) | |
47 self.cmd.initialize_options() | |
48 | |
49 def tearDown(self): | |
50 os.chdir(self.olddir) | |
51 | |
52 def test_no_directory_or_output_file_specified(self): | |
53 self.cmd.locale = 'en_US' | |
54 self.cmd.input_file = 'dummy' | |
55 self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) | |
56 | |
57 def test_no_directory_or_input_file_specified(self): | |
58 self.cmd.locale = 'en_US' | |
59 self.cmd.output_file = 'dummy' | |
60 self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) | |
61 | |
62 | |
63 class ExtractMessagesTestCase(unittest.TestCase): | |
64 | |
65 def setUp(self): | |
66 self.olddir = os.getcwd() | |
67 self.datadir = os.path.join(os.path.dirname(__file__), 'data') | |
68 os.chdir(self.datadir) | |
69 _global_log.threshold = 5 # shut up distutils logging | |
70 | |
71 self.dist = Distribution(dict( | |
72 name='TestProject', | |
73 version='0.1', | |
74 packages=['project'] | |
75 )) | |
76 self.cmd = frontend.extract_messages(self.dist) | |
77 self.cmd.initialize_options() | |
78 | |
79 def tearDown(self): | |
80 pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') | |
81 if os.path.isfile(pot_file): | |
82 os.unlink(pot_file) | |
83 | |
84 os.chdir(self.olddir) | |
85 | |
86 def test_neither_default_nor_custom_keywords(self): | |
87 self.cmd.output_file = 'dummy' | |
88 self.cmd.no_default_keywords = True | |
89 self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) | |
90 | |
91 def test_no_output_file_specified(self): | |
92 self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) | |
93 | |
94 def test_both_sort_output_and_sort_by_file(self): | |
95 self.cmd.output_file = 'dummy' | |
96 self.cmd.sort_output = True | |
97 self.cmd.sort_by_file = True | |
98 self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) | |
99 | |
100 def test_extraction_with_default_mapping(self): | |
101 self.cmd.copyright_holder = 'FooBar, Inc.' | |
102 self.cmd.msgid_bugs_address = 'bugs.address@email.tld' | |
103 self.cmd.output_file = 'project/i18n/temp.pot' | |
104 self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:' | |
105 | |
106 self.cmd.finalize_options() | |
107 self.cmd.run() | |
108 | |
109 pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') | |
110 assert os.path.isfile(pot_file) | |
111 | |
112 self.assertEqual( | |
113 r"""# Translations template for TestProject. | |
114 # Copyright (C) %(year)s FooBar, Inc. | |
115 # This file is distributed under the same license as the TestProject | |
116 # project. | |
117 # FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s. | |
118 # | |
119 #, fuzzy | |
120 msgid "" | |
121 msgstr "" | |
122 "Project-Id-Version: TestProject 0.1\n" | |
123 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
124 "POT-Creation-Date: %(date)s\n" | |
125 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | |
126 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
127 "Language-Team: LANGUAGE <LL@li.org>\n" | |
128 "MIME-Version: 1.0\n" | |
129 "Content-Type: text/plain; charset=utf-8\n" | |
130 "Content-Transfer-Encoding: 8bit\n" | |
131 "Generated-By: Babel %(version)s\n" | |
132 | |
348
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
133 #. TRANSLATOR: This will be a translator coment, |
263 | 134 #. that will include several lines |
135 #: project/file1.py:8 | |
136 msgid "bar" | |
137 msgstr "" | |
138 | |
139 #: project/file2.py:9 | |
140 msgid "foobar" | |
141 msgid_plural "foobars" | |
142 msgstr[0] "" | |
143 msgstr[1] "" | |
144 | |
461 | 145 #: project/ignored/this_wont_normally_be_here.py:11 |
263 | 146 msgid "FooBar" |
147 msgid_plural "FooBars" | |
148 msgstr[0] "" | |
149 msgstr[1] "" | |
150 | |
151 """ % {'version': VERSION, | |
152 'year': time.strftime('%Y'), | |
153 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
154 tzinfo=LOCALTZ, locale='en')}, | |
155 open(pot_file, 'U').read()) | |
156 | |
157 def test_extraction_with_mapping_file(self): | |
158 self.cmd.copyright_holder = 'FooBar, Inc.' | |
159 self.cmd.msgid_bugs_address = 'bugs.address@email.tld' | |
160 self.cmd.mapping_file = 'mapping.cfg' | |
161 self.cmd.output_file = 'project/i18n/temp.pot' | |
162 self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:' | |
163 | |
164 self.cmd.finalize_options() | |
165 self.cmd.run() | |
166 | |
167 pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') | |
168 assert os.path.isfile(pot_file) | |
169 | |
170 self.assertEqual( | |
171 r"""# Translations template for TestProject. | |
172 # Copyright (C) %(year)s FooBar, Inc. | |
173 # This file is distributed under the same license as the TestProject | |
174 # project. | |
175 # FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s. | |
176 # | |
177 #, fuzzy | |
178 msgid "" | |
179 msgstr "" | |
180 "Project-Id-Version: TestProject 0.1\n" | |
181 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
182 "POT-Creation-Date: %(date)s\n" | |
183 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | |
184 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
185 "Language-Team: LANGUAGE <LL@li.org>\n" | |
186 "MIME-Version: 1.0\n" | |
187 "Content-Type: text/plain; charset=utf-8\n" | |
188 "Content-Transfer-Encoding: 8bit\n" | |
189 "Generated-By: Babel %(version)s\n" | |
190 | |
348
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
191 #. TRANSLATOR: This will be a translator coment, |
263 | 192 #. that will include several lines |
193 #: project/file1.py:8 | |
194 msgid "bar" | |
195 msgstr "" | |
196 | |
197 #: project/file2.py:9 | |
198 msgid "foobar" | |
199 msgid_plural "foobars" | |
200 msgstr[0] "" | |
201 msgstr[1] "" | |
202 | |
203 """ % {'version': VERSION, | |
204 'year': time.strftime('%Y'), | |
205 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
206 tzinfo=LOCALTZ, locale='en')}, | |
207 open(pot_file, 'U').read()) | |
208 | |
209 def test_extraction_with_mapping_dict(self): | |
210 self.dist.message_extractors = { | |
211 'project': [ | |
461 | 212 ('**/ignored/**.*', 'ignore', None), |
213 ('**.py', 'python', None), | |
263 | 214 ] |
215 } | |
216 self.cmd.copyright_holder = 'FooBar, Inc.' | |
217 self.cmd.msgid_bugs_address = 'bugs.address@email.tld' | |
218 self.cmd.output_file = 'project/i18n/temp.pot' | |
219 self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:' | |
220 | |
221 self.cmd.finalize_options() | |
222 self.cmd.run() | |
223 | |
224 pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') | |
225 assert os.path.isfile(pot_file) | |
226 | |
227 self.assertEqual( | |
228 r"""# Translations template for TestProject. | |
229 # Copyright (C) %(year)s FooBar, Inc. | |
230 # This file is distributed under the same license as the TestProject | |
231 # project. | |
232 # FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s. | |
233 # | |
234 #, fuzzy | |
235 msgid "" | |
236 msgstr "" | |
237 "Project-Id-Version: TestProject 0.1\n" | |
238 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
239 "POT-Creation-Date: %(date)s\n" | |
240 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | |
241 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
242 "Language-Team: LANGUAGE <LL@li.org>\n" | |
243 "MIME-Version: 1.0\n" | |
244 "Content-Type: text/plain; charset=utf-8\n" | |
245 "Content-Transfer-Encoding: 8bit\n" | |
246 "Generated-By: Babel %(version)s\n" | |
247 | |
348
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
248 #. TRANSLATOR: This will be a translator coment, |
263 | 249 #. that will include several lines |
250 #: project/file1.py:8 | |
251 msgid "bar" | |
252 msgstr "" | |
253 | |
254 #: project/file2.py:9 | |
255 msgid "foobar" | |
256 msgid_plural "foobars" | |
257 msgstr[0] "" | |
258 msgstr[1] "" | |
259 | |
260 """ % {'version': VERSION, | |
261 'year': time.strftime('%Y'), | |
262 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
263 tzinfo=LOCALTZ, locale='en')}, | |
264 open(pot_file, 'U').read()) | |
265 | |
266 | |
267 class InitCatalogTestCase(unittest.TestCase): | |
268 | |
269 def setUp(self): | |
270 self.olddir = os.getcwd() | |
271 self.datadir = os.path.join(os.path.dirname(__file__), 'data') | |
272 os.chdir(self.datadir) | |
273 _global_log.threshold = 5 # shut up distutils logging | |
274 | |
275 self.dist = Distribution(dict( | |
276 name='TestProject', | |
277 version='0.1', | |
278 packages=['project'] | |
279 )) | |
280 self.cmd = frontend.init_catalog(self.dist) | |
281 self.cmd.initialize_options() | |
282 | |
283 def tearDown(self): | |
381 | 284 for dirname in ['en_US', 'ja_JP', 'lv_LV']: |
285 locale_dir = os.path.join(self.datadir, 'project', 'i18n', dirname) | |
286 if os.path.isdir(locale_dir): | |
287 shutil.rmtree(locale_dir) | |
263 | 288 |
289 os.chdir(self.olddir) | |
290 | |
291 def test_no_input_file(self): | |
292 self.cmd.locale = 'en_US' | |
293 self.cmd.output_file = 'dummy' | |
294 self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) | |
295 | |
296 def test_no_locale(self): | |
297 self.cmd.input_file = 'dummy' | |
298 self.cmd.output_file = 'dummy' | |
299 self.assertRaises(DistutilsOptionError, self.cmd.finalize_options) | |
300 | |
301 def test_with_output_dir(self): | |
302 self.cmd.input_file = 'project/i18n/messages.pot' | |
303 self.cmd.locale = 'en_US' | |
304 self.cmd.output_dir = 'project/i18n' | |
305 | |
306 self.cmd.finalize_options() | |
307 self.cmd.run() | |
308 | |
309 po_file = os.path.join(self.datadir, 'project', 'i18n', 'en_US', | |
310 'LC_MESSAGES', 'messages.po') | |
311 assert os.path.isfile(po_file) | |
312 | |
313 self.assertEqual( | |
314 r"""# English (United States) translations for TestProject. | |
315 # Copyright (C) 2007 FooBar, Inc. | |
316 # This file is distributed under the same license as the TestProject | |
317 # project. | |
318 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. | |
319 # | |
320 msgid "" | |
321 msgstr "" | |
322 "Project-Id-Version: TestProject 0.1\n" | |
323 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
324 "POT-Creation-Date: 2007-04-01 15:30+0200\n" | |
325 "PO-Revision-Date: %(date)s\n" | |
326 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
327 "Language-Team: en_US <LL@li.org>\n" | |
328 "Plural-Forms: nplurals=2; plural=(n != 1)\n" | |
329 "MIME-Version: 1.0\n" | |
330 "Content-Type: text/plain; charset=utf-8\n" | |
331 "Content-Transfer-Encoding: 8bit\n" | |
332 "Generated-By: Babel %(version)s\n" | |
333 | |
334 #. This will be a translator coment, | |
335 #. that will include several lines | |
336 #: project/file1.py:8 | |
337 msgid "bar" | |
338 msgstr "" | |
339 | |
340 #: project/file2.py:9 | |
341 msgid "foobar" | |
342 msgid_plural "foobars" | |
343 msgstr[0] "" | |
344 msgstr[1] "" | |
345 | |
346 """ % {'version': VERSION, | |
347 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
348 tzinfo=LOCALTZ, locale='en')}, | |
349 open(po_file, 'U').read()) | |
350 | |
381 | 351 def test_keeps_catalog_non_fuzzy(self): |
263 | 352 self.cmd.input_file = 'project/i18n/messages_non_fuzzy.pot' |
353 self.cmd.locale = 'en_US' | |
354 self.cmd.output_dir = 'project/i18n' | |
355 | |
356 self.cmd.finalize_options() | |
357 self.cmd.run() | |
358 | |
359 po_file = os.path.join(self.datadir, 'project', 'i18n', 'en_US', | |
360 'LC_MESSAGES', 'messages.po') | |
361 assert os.path.isfile(po_file) | |
362 | |
363 self.assertEqual( | |
364 r"""# English (United States) translations for TestProject. | |
365 # Copyright (C) 2007 FooBar, Inc. | |
366 # This file is distributed under the same license as the TestProject | |
367 # project. | |
368 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. | |
369 # | |
370 msgid "" | |
371 msgstr "" | |
372 "Project-Id-Version: TestProject 0.1\n" | |
373 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
374 "POT-Creation-Date: 2007-04-01 15:30+0200\n" | |
375 "PO-Revision-Date: %(date)s\n" | |
376 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
377 "Language-Team: en_US <LL@li.org>\n" | |
378 "Plural-Forms: nplurals=2; plural=(n != 1)\n" | |
379 "MIME-Version: 1.0\n" | |
380 "Content-Type: text/plain; charset=utf-8\n" | |
381 "Content-Transfer-Encoding: 8bit\n" | |
382 "Generated-By: Babel %(version)s\n" | |
383 | |
384 #. This will be a translator coment, | |
385 #. that will include several lines | |
386 #: project/file1.py:8 | |
387 msgid "bar" | |
388 msgstr "" | |
389 | |
390 #: project/file2.py:9 | |
391 msgid "foobar" | |
392 msgid_plural "foobars" | |
393 msgstr[0] "" | |
394 msgstr[1] "" | |
395 | |
396 """ % {'version': VERSION, | |
397 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
398 tzinfo=LOCALTZ, locale='en')}, | |
399 open(po_file, 'U').read()) | |
400 | |
381 | 401 def test_correct_init_more_than_2_plurals(self): |
402 self.cmd.input_file = 'project/i18n/messages.pot' | |
403 self.cmd.locale = 'lv_LV' | |
404 self.cmd.output_dir = 'project/i18n' | |
405 | |
406 self.cmd.finalize_options() | |
407 self.cmd.run() | |
408 | |
409 po_file = os.path.join(self.datadir, 'project', 'i18n', 'lv_LV', | |
410 'LC_MESSAGES', 'messages.po') | |
411 assert os.path.isfile(po_file) | |
412 | |
413 self.assertEqual( | |
414 r"""# Latvian (Latvia) translations for TestProject. | |
415 # Copyright (C) 2007 FooBar, Inc. | |
416 # This file is distributed under the same license as the TestProject | |
417 # project. | |
418 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. | |
419 # | |
420 msgid "" | |
421 msgstr "" | |
422 "Project-Id-Version: TestProject 0.1\n" | |
423 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
424 "POT-Creation-Date: 2007-04-01 15:30+0200\n" | |
425 "PO-Revision-Date: %(date)s\n" | |
426 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
427 "Language-Team: lv_LV <LL@li.org>\n" | |
428 "Plural-Forms: nplurals=3; plural=(n%%10==1 && n%%100!=11 ? 0 : n != 0 ? 1 :" | |
429 " 2)\n" | |
430 "MIME-Version: 1.0\n" | |
431 "Content-Type: text/plain; charset=utf-8\n" | |
432 "Content-Transfer-Encoding: 8bit\n" | |
433 "Generated-By: Babel %(version)s\n" | |
434 | |
435 #. This will be a translator coment, | |
436 #. that will include several lines | |
437 #: project/file1.py:8 | |
438 msgid "bar" | |
439 msgstr "" | |
440 | |
441 #: project/file2.py:9 | |
442 msgid "foobar" | |
443 msgid_plural "foobars" | |
444 msgstr[0] "" | |
445 msgstr[1] "" | |
446 msgstr[2] "" | |
447 | |
448 """ % {'version': VERSION, | |
449 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
450 tzinfo=LOCALTZ, locale='en')}, | |
451 open(po_file, 'U').read()) | |
452 | |
453 def test_correct_init_singular_plural_forms(self): | |
454 self.cmd.input_file = 'project/i18n/messages.pot' | |
455 self.cmd.locale = 'ja_JP' | |
456 self.cmd.output_dir = 'project/i18n' | |
457 | |
458 self.cmd.finalize_options() | |
459 self.cmd.run() | |
460 | |
461 po_file = os.path.join(self.datadir, 'project', 'i18n', 'ja_JP', | |
462 'LC_MESSAGES', 'messages.po') | |
463 assert os.path.isfile(po_file) | |
464 | |
465 self.assertEqual( | |
466 r"""# Japanese (Japan) translations for TestProject. | |
467 # Copyright (C) 2007 FooBar, Inc. | |
468 # This file is distributed under the same license as the TestProject | |
469 # project. | |
470 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. | |
471 # | |
472 msgid "" | |
473 msgstr "" | |
474 "Project-Id-Version: TestProject 0.1\n" | |
475 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
476 "POT-Creation-Date: 2007-04-01 15:30+0200\n" | |
477 "PO-Revision-Date: %(date)s\n" | |
478 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
479 "Language-Team: ja_JP <LL@li.org>\n" | |
480 "Plural-Forms: nplurals=1; plural=0\n" | |
481 "MIME-Version: 1.0\n" | |
482 "Content-Type: text/plain; charset=utf-8\n" | |
483 "Content-Transfer-Encoding: 8bit\n" | |
484 "Generated-By: Babel %(version)s\n" | |
485 | |
486 #. This will be a translator coment, | |
487 #. that will include several lines | |
488 #: project/file1.py:8 | |
489 msgid "bar" | |
490 msgstr "" | |
491 | |
492 #: project/file2.py:9 | |
493 msgid "foobar" | |
494 msgid_plural "foobars" | |
495 msgstr[0] "" | |
496 | |
497 """ % {'version': VERSION, | |
498 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
499 tzinfo=LOCALTZ, locale='ja_JP')}, | |
500 open(po_file, 'U').read()) | |
501 | |
263 | 502 |
503 class CommandLineInterfaceTestCase(unittest.TestCase): | |
504 | |
505 def setUp(self): | |
506 self.datadir = os.path.join(os.path.dirname(__file__), 'data') | |
544 | 507 self.orig_working_dir = os.getcwd() |
263 | 508 self.orig_argv = sys.argv |
509 self.orig_stdout = sys.stdout | |
510 self.orig_stderr = sys.stderr | |
511 sys.argv = ['pybabel'] | |
512 sys.stdout = StringIO() | |
513 sys.stderr = StringIO() | |
544 | 514 os.chdir(self.datadir) |
526 | 515 |
516 # Logging handlers will be reused if possible (#227). This breaks the | |
517 # implicit assumption that our newly created StringIO for sys.stderr | |
518 # contains the console output. Removing the old handler ensures that a | |
519 # new handler with our new StringIO instance will be used. | |
520 log = logging.getLogger('babel') | |
521 for handler in log.handlers: | |
522 log.removeHandler(handler) | |
263 | 523 self.cli = frontend.CommandLineInterface() |
524 | |
525 def tearDown(self): | |
544 | 526 os.chdir(self.orig_working_dir) |
263 | 527 sys.argv = self.orig_argv |
528 sys.stdout = self.orig_stdout | |
529 sys.stderr = self.orig_stderr | |
381 | 530 for dirname in ['lv_LV', 'ja_JP']: |
531 locale_dir = os.path.join(self.datadir, 'project', 'i18n', dirname) | |
532 if os.path.isdir(locale_dir): | |
533 shutil.rmtree(locale_dir) | |
263 | 534 |
535 def test_usage(self): | |
536 try: | |
537 self.cli.run(sys.argv) | |
538 self.fail('Expected SystemExit') | |
539 except SystemExit, e: | |
510
4c473bedd528
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
476
diff
changeset
|
540 stderr = sys.stderr.getvalue() |
4c473bedd528
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
476
diff
changeset
|
541 if isinstance(e.code, int): |
4c473bedd528
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
476
diff
changeset
|
542 self.assertEqual(2, e.code) |
4c473bedd528
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
476
diff
changeset
|
543 else: |
4c473bedd528
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
476
diff
changeset
|
544 # OptionParser in Python 2.3 does not set the exit code. |
4c473bedd528
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
476
diff
changeset
|
545 # Instead the 'code' contains the custom error message from the |
4c473bedd528
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
476
diff
changeset
|
546 # frontend |
4c473bedd528
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
476
diff
changeset
|
547 stderr = stderr + e.code + '\n' |
263 | 548 self.assertEqual("""\ |
549 usage: pybabel command [options] [args] | |
550 | |
476 | 551 pybabel: error: no valid command or option passed. try the -h/--help option for more information. |
510
4c473bedd528
Fix Python 2.3 compatibility for 0.9 branch (closes #233)
fschwarz
parents:
476
diff
changeset
|
552 """, stderr.lower()) |
263 | 553 |
542 | 554 def _run_init_catalog(self): |
555 i18n_dir = os.path.join(self.datadir, 'project', 'i18n') | |
556 pot_path = os.path.join(self.datadir, 'project', 'i18n', 'messages.pot') | |
557 init_argv = sys.argv + ['init', '--locale', 'en_US', '-d', i18n_dir, | |
558 '-i', pot_path] | |
559 self.cli.run(init_argv) | |
560 | |
561 def test_no_duplicated_output_for_multiple_runs(self): | |
562 self._run_init_catalog() | |
563 first_output = sys.stderr.getvalue() | |
564 self._run_init_catalog() | |
565 second_output = sys.stderr.getvalue()[len(first_output):] | |
566 | |
567 # in case the log message is not duplicated we should get the same | |
568 # output as before | |
569 self.assertEqual(first_output, second_output) | |
570 | |
571 def test_frontend_can_log_to_predefined_handler(self): | |
572 custom_stream = StringIO() | |
573 log = logging.getLogger('babel') | |
574 log.addHandler(logging.StreamHandler(custom_stream)) | |
575 | |
576 self._run_init_catalog() | |
577 self.assertNotEqual(id(sys.stderr), id(custom_stream)) | |
578 self.assertEqual('', sys.stderr.getvalue()) | |
579 assert len(custom_stream.getvalue()) > 0 | |
580 | |
263 | 581 def test_help(self): |
582 try: | |
583 self.cli.run(sys.argv + ['--help']) | |
584 self.fail('Expected SystemExit') | |
585 except SystemExit, e: | |
586 self.assertEqual(0, e.code) | |
587 self.assertEqual("""\ | |
588 usage: pybabel command [options] [args] | |
589 | |
590 options: | |
591 --version show program's version number and exit | |
592 -h, --help show this help message and exit | |
593 --list-locales print all known locales and exit | |
594 -v, --verbose print as much as possible | |
595 -q, --quiet print as little as possible | |
596 | |
597 commands: | |
598 compile compile message catalogs to mo files | |
599 extract extract messages from source files and generate a pot file | |
600 init create new message catalogs from a pot file | |
601 update update existing message catalogs from a pot file | |
602 """, sys.stdout.getvalue().lower()) | |
603 | |
604 def test_extract_with_default_mapping(self): | |
605 pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') | |
544 | 606 self.cli.run(sys.argv + ['extract', |
607 '--copyright-holder', 'FooBar, Inc.', | |
608 '--project', 'TestProject', '--version', '0.1', | |
609 '--msgid-bugs-address', 'bugs.address@email.tld', | |
610 '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', | |
611 '-o', pot_file, 'project']) | |
612 assert os.path.isfile(pot_file) | |
613 self.assertEqual( | |
263 | 614 r"""# Translations template for TestProject. |
615 # Copyright (C) %(year)s FooBar, Inc. | |
616 # This file is distributed under the same license as the TestProject | |
617 # project. | |
618 # FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s. | |
619 # | |
620 #, fuzzy | |
621 msgid "" | |
622 msgstr "" | |
623 "Project-Id-Version: TestProject 0.1\n" | |
624 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
625 "POT-Creation-Date: %(date)s\n" | |
626 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | |
627 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
628 "Language-Team: LANGUAGE <LL@li.org>\n" | |
629 "MIME-Version: 1.0\n" | |
630 "Content-Type: text/plain; charset=utf-8\n" | |
631 "Content-Transfer-Encoding: 8bit\n" | |
632 "Generated-By: Babel %(version)s\n" | |
633 | |
544 | 634 #. TRANSLATOR: This will be a translator coment, |
263 | 635 #. that will include several lines |
636 #: project/file1.py:8 | |
637 msgid "bar" | |
638 msgstr "" | |
639 | |
640 #: project/file2.py:9 | |
641 msgid "foobar" | |
642 msgid_plural "foobars" | |
643 msgstr[0] "" | |
644 msgstr[1] "" | |
645 | |
461 | 646 #: project/ignored/this_wont_normally_be_here.py:11 |
263 | 647 msgid "FooBar" |
648 msgid_plural "FooBars" | |
649 msgstr[0] "" | |
650 msgstr[1] "" | |
651 | |
652 """ % {'version': VERSION, | |
653 'year': time.strftime('%Y'), | |
654 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
655 tzinfo=LOCALTZ, locale='en')}, | |
656 open(pot_file, 'U').read()) | |
657 | |
658 def test_extract_with_mapping_file(self): | |
659 pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot') | |
544 | 660 self.cli.run(sys.argv + ['extract', |
661 '--copyright-holder', 'FooBar, Inc.', | |
662 '--project', 'TestProject', '--version', '0.1', | |
663 '--msgid-bugs-address', 'bugs.address@email.tld', | |
664 '--mapping', os.path.join(self.datadir, 'mapping.cfg'), | |
665 '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', | |
666 '-o', pot_file, 'project']) | |
667 assert os.path.isfile(pot_file) | |
668 self.assertEqual( | |
263 | 669 r"""# Translations template for TestProject. |
670 # Copyright (C) %(year)s FooBar, Inc. | |
671 # This file is distributed under the same license as the TestProject | |
672 # project. | |
673 # FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s. | |
674 # | |
675 #, fuzzy | |
676 msgid "" | |
677 msgstr "" | |
678 "Project-Id-Version: TestProject 0.1\n" | |
679 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
680 "POT-Creation-Date: %(date)s\n" | |
681 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | |
682 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
683 "Language-Team: LANGUAGE <LL@li.org>\n" | |
684 "MIME-Version: 1.0\n" | |
685 "Content-Type: text/plain; charset=utf-8\n" | |
686 "Content-Transfer-Encoding: 8bit\n" | |
687 "Generated-By: Babel %(version)s\n" | |
688 | |
544 | 689 #. TRANSLATOR: This will be a translator coment, |
263 | 690 #. that will include several lines |
691 #: project/file1.py:8 | |
692 msgid "bar" | |
693 msgstr "" | |
694 | |
695 #: project/file2.py:9 | |
696 msgid "foobar" | |
697 msgid_plural "foobars" | |
698 msgstr[0] "" | |
699 msgstr[1] "" | |
700 | |
701 """ % {'version': VERSION, | |
702 'year': time.strftime('%Y'), | |
703 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
704 tzinfo=LOCALTZ, locale='en')}, | |
705 open(pot_file, 'U').read()) | |
706 | |
707 def test_init_with_output_dir(self): | |
708 po_file = os.path.join(self.datadir, 'project', 'i18n', 'en_US', | |
709 'LC_MESSAGES', 'messages.po') | |
544 | 710 self.cli.run(sys.argv + ['init', |
711 '--locale', 'en_US', | |
712 '-d', os.path.join(self.datadir, 'project', 'i18n'), | |
713 '-i', os.path.join(self.datadir, 'project', 'i18n', 'messages.pot')]) | |
714 assert os.path.isfile(po_file) | |
715 self.assertEqual( | |
263 | 716 r"""# English (United States) translations for TestProject. |
717 # Copyright (C) 2007 FooBar, Inc. | |
718 # This file is distributed under the same license as the TestProject | |
719 # project. | |
720 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. | |
721 # | |
722 #, fuzzy | |
723 msgid "" | |
724 msgstr "" | |
725 "Project-Id-Version: TestProject 0.1\n" | |
726 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
727 "POT-Creation-Date: 2007-04-01 15:30+0200\n" | |
728 "PO-Revision-Date: %(date)s\n" | |
729 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
730 "Language-Team: en_US <LL@li.org>\n" | |
731 "Plural-Forms: nplurals=2; plural=(n != 1)\n" | |
732 "MIME-Version: 1.0\n" | |
733 "Content-Type: text/plain; charset=utf-8\n" | |
734 "Content-Transfer-Encoding: 8bit\n" | |
735 "Generated-By: Babel %(version)s\n" | |
736 | |
737 #. This will be a translator coment, | |
738 #. that will include several lines | |
739 #: project/file1.py:8 | |
740 msgid "bar" | |
741 msgstr "" | |
742 | |
743 #: project/file2.py:9 | |
744 msgid "foobar" | |
745 msgid_plural "foobars" | |
746 msgstr[0] "" | |
747 msgstr[1] "" | |
748 | |
749 """ % {'version': VERSION, | |
750 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
751 tzinfo=LOCALTZ, locale='en')}, | |
752 open(po_file, 'U').read()) | |
381 | 753 |
754 def test_init_singular_plural_forms(self): | |
755 po_file = os.path.join(self.datadir, 'project', 'i18n', 'ja_JP', | |
756 'LC_MESSAGES', 'messages.po') | |
544 | 757 self.cli.run(sys.argv + ['init', |
758 '--locale', 'ja_JP', | |
759 '-d', os.path.join(self.datadir, 'project', 'i18n'), | |
760 '-i', os.path.join(self.datadir, 'project', 'i18n', | |
761 'messages.pot')]) | |
381 | 762 assert os.path.isfile(po_file) |
763 self.assertEqual( | |
764 r"""# Japanese (Japan) translations for TestProject. | |
765 # Copyright (C) 2007 FooBar, Inc. | |
766 # This file is distributed under the same license as the TestProject | |
767 # project. | |
768 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. | |
769 # | |
770 #, fuzzy | |
771 msgid "" | |
772 msgstr "" | |
773 "Project-Id-Version: TestProject 0.1\n" | |
774 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
775 "POT-Creation-Date: 2007-04-01 15:30+0200\n" | |
776 "PO-Revision-Date: %(date)s\n" | |
777 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
778 "Language-Team: ja_JP <LL@li.org>\n" | |
779 "Plural-Forms: nplurals=1; plural=0\n" | |
780 "MIME-Version: 1.0\n" | |
781 "Content-Type: text/plain; charset=utf-8\n" | |
782 "Content-Transfer-Encoding: 8bit\n" | |
783 "Generated-By: Babel %(version)s\n" | |
784 | |
785 #. This will be a translator coment, | |
786 #. that will include several lines | |
787 #: project/file1.py:8 | |
788 msgid "bar" | |
789 msgstr "" | |
790 | |
791 #: project/file2.py:9 | |
792 msgid "foobar" | |
793 msgid_plural "foobars" | |
794 msgstr[0] "" | |
795 | |
796 """ % {'version': VERSION, | |
797 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
798 tzinfo=LOCALTZ, locale='en')}, | |
799 open(po_file, 'U').read()) | |
800 | |
801 def test_init_more_than_2_plural_forms(self): | |
802 po_file = os.path.join(self.datadir, 'project', 'i18n', 'lv_LV', | |
803 'LC_MESSAGES', 'messages.po') | |
544 | 804 self.cli.run(sys.argv + ['init', |
805 '--locale', 'lv_LV', | |
806 '-d', os.path.join(self.datadir, 'project', 'i18n'), | |
807 '-i', os.path.join(self.datadir, 'project', 'i18n', | |
808 'messages.pot')]) | |
381 | 809 assert os.path.isfile(po_file) |
810 self.assertEqual( | |
811 r"""# Latvian (Latvia) translations for TestProject. | |
812 # Copyright (C) 2007 FooBar, Inc. | |
813 # This file is distributed under the same license as the TestProject | |
814 # project. | |
815 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. | |
816 # | |
817 #, fuzzy | |
818 msgid "" | |
819 msgstr "" | |
820 "Project-Id-Version: TestProject 0.1\n" | |
821 "Report-Msgid-Bugs-To: bugs.address@email.tld\n" | |
822 "POT-Creation-Date: 2007-04-01 15:30+0200\n" | |
823 "PO-Revision-Date: %(date)s\n" | |
824 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
825 "Language-Team: lv_LV <LL@li.org>\n" | |
826 "Plural-Forms: nplurals=3; plural=(n%%10==1 && n%%100!=11 ? 0 : n != 0 ? 1 :" | |
827 " 2)\n" | |
828 "MIME-Version: 1.0\n" | |
829 "Content-Type: text/plain; charset=utf-8\n" | |
830 "Content-Transfer-Encoding: 8bit\n" | |
831 "Generated-By: Babel %(version)s\n" | |
832 | |
833 #. This will be a translator coment, | |
834 #. that will include several lines | |
835 #: project/file1.py:8 | |
836 msgid "bar" | |
837 msgstr "" | |
838 | |
839 #: project/file2.py:9 | |
840 msgid "foobar" | |
841 msgid_plural "foobars" | |
842 msgstr[0] "" | |
843 msgstr[1] "" | |
844 msgstr[2] "" | |
845 | |
846 """ % {'version': VERSION, | |
847 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', | |
848 tzinfo=LOCALTZ, locale='en')}, | |
849 open(po_file, 'U').read()) | |
263 | 850 |
851 def test_compile_catalog(self): | |
852 po_file = os.path.join(self.datadir, 'project', 'i18n', 'de_DE', | |
853 'LC_MESSAGES', 'messages.po') | |
854 mo_file = po_file.replace('.po', '.mo') | |
855 self.cli.run(sys.argv + ['compile', | |
856 '--locale', 'de_DE', | |
857 '-d', os.path.join(self.datadir, 'project', 'i18n')]) | |
858 assert not os.path.isfile(mo_file), 'Expected no file at %r' % mo_file | |
859 self.assertEqual("""\ | |
860 catalog %r is marked as fuzzy, skipping | |
861 """ % (po_file), sys.stderr.getvalue()) | |
862 | |
863 def test_compile_fuzzy_catalog(self): | |
864 po_file = os.path.join(self.datadir, 'project', 'i18n', 'de_DE', | |
865 'LC_MESSAGES', 'messages.po') | |
866 mo_file = po_file.replace('.po', '.mo') | |
867 try: | |
868 self.cli.run(sys.argv + ['compile', | |
869 '--locale', 'de_DE', '--use-fuzzy', | |
870 '-d', os.path.join(self.datadir, 'project', 'i18n')]) | |
871 assert os.path.isfile(mo_file) | |
872 self.assertEqual("""\ | |
873 compiling catalog %r to %r | |
874 """ % (po_file, mo_file), sys.stderr.getvalue()) | |
875 finally: | |
876 if os.path.isfile(mo_file): | |
877 os.unlink(mo_file) | |
878 | |
348
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
879 def test_compile_catalog_with_more_than_2_plural_forms(self): |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
880 po_file = os.path.join(self.datadir, 'project', 'i18n', 'ru_RU', |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
881 'LC_MESSAGES', 'messages.po') |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
882 mo_file = po_file.replace('.po', '.mo') |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
883 try: |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
884 self.cli.run(sys.argv + ['compile', |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
885 '--locale', 'ru_RU', '--use-fuzzy', |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
886 '-d', os.path.join(self.datadir, 'project', 'i18n')]) |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
887 assert os.path.isfile(mo_file) |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
888 self.assertEqual("""\ |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
889 compiling catalog %r to %r |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
890 """ % (po_file, mo_file), sys.stderr.getvalue()) |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
891 finally: |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
892 if os.path.isfile(mo_file): |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
893 os.unlink(mo_file) |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
263
diff
changeset
|
894 |
263 | 895 |
896 def suite(): | |
897 suite = unittest.TestSuite() | |
898 suite.addTest(doctest.DocTestSuite(frontend)) | |
899 suite.addTest(unittest.makeSuite(CompileCatalogTestCase)) | |
900 suite.addTest(unittest.makeSuite(ExtractMessagesTestCase)) | |
901 suite.addTest(unittest.makeSuite(InitCatalogTestCase)) | |
902 suite.addTest(unittest.makeSuite(CommandLineInterfaceTestCase)) | |
903 return suite | |
904 | |
905 if __name__ == '__main__': | |
906 unittest.main(defaultTest='suite') |