annotate babel/messages/tests/pofile.py @ 547:274f9a6485d4

Catalog class should not do decoding of input strings (fixes #256)
author fschwarz
date Sat, 19 Mar 2011 19:34:40 +0000
parents 10de195cfb04
children
rev   line source
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
2 #
532
e93f68837913 Update the copyright line.
jruigrok
parents: 449
diff changeset
3 # Copyright (C) 2007-2011 Edgewall Software
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
5 #
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
9 #
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
13
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
14 from datetime import datetime
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
15 import doctest
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
16 from StringIO import StringIO
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
17 import unittest
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
18
183
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
19 from babel.messages.catalog import Catalog, Message
56
27d55a07c897 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 53
diff changeset
20 from babel.messages import pofile
533
1869978e3895 cleanup: remove unused imports
fschwarz
parents: 532
diff changeset
21 from babel.util import FixedOffsetTimezone
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
22
19
762a5de6faae Recognize python-format messages also for unnamed parameters.
cmlenz
parents: 14
diff changeset
23
108
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
24 class ReadPoTestCase(unittest.TestCase):
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
25
198
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
26 def test_preserve_locale(self):
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
27 buf = StringIO(r'''msgid "foo"
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
28 msgstr "Voh"''')
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
29 catalog = pofile.read_po(buf, locale='en_US')
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
30 self.assertEqual('en_US', catalog.locale)
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
31
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
32 def test_preserve_domain(self):
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
33 buf = StringIO(r'''msgid "foo"
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
34 msgstr "Voh"''')
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
35 catalog = pofile.read_po(buf, domain='mydomain')
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
36 self.assertEqual('mydomain', catalog.domain)
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
37
547
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
38 def test_applies_specified_encoding_during_read(self):
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
39 buf = StringIO(u'''
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
40 msgid ""
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
41 msgstr ""
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
42 "Project-Id-Version: 3.15\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
43 "Report-Msgid-Bugs-To: Fliegender Zirkus <fliegender@zirkus.de>\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
44 "POT-Creation-Date: 2007-09-27 11:19+0700\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
45 "PO-Revision-Date: 2007-09-27 21:42-0700\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
46 "Last-Translator: John <cleese@bavaria.de>\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
47 "Language-Team: German Lang <de@babel.org>\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
48 "Plural-Forms: nplurals=2; plural=(n != 1)\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
49 "MIME-Version: 1.0\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
50 "Content-Type: text/plain; charset=iso-8859-1\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
51 "Content-Transfer-Encoding: 8bit\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
52 "Generated-By: Babel 1.0dev-r313\\n"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
53
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
54 msgid "foo"
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
55 msgstr "bär"'''.encode('iso-8859-1'))
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
56 catalog = pofile.read_po(buf, locale='de_DE')
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
57 self.assertEqual(u'bär', catalog.get('foo').string)
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
58
108
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
59 def test_read_multiline(self):
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
60 buf = StringIO(r'''msgid ""
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
61 "Here's some text that\n"
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
62 "includesareallylongwordthatmightbutshouldnt"
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
63 " throw us into an infinite "
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
64 "loop\n"
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
65 msgstr ""''')
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
66 catalog = pofile.read_po(buf)
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
67 self.assertEqual(1, len(catalog))
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
68 message = list(catalog)[1]
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
69 self.assertEqual("Here's some text that\nincludesareallylongwordthat"
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
70 "mightbutshouldnt throw us into an infinite loop\n",
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
71 message.id)
198
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
72
177
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
73 def test_fuzzy_header(self):
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
74 buf = StringIO(r'''\
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
75 # Translations template for AReallyReallyLongNameForAProject.
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
76 # Copyright (C) 2007 ORGANIZATION
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
77 # This file is distributed under the same license as the
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
78 # AReallyReallyLongNameForAProject project.
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
79 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
80 #
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
81 #, fuzzy
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
82 ''')
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
83 catalog = pofile.read_po(buf)
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
84 self.assertEqual(1, len(list(catalog)))
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
85 self.assertEqual(True, list(catalog)[0].fuzzy)
198
982d7e704fdc Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 193
diff changeset
86
177
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
87 def test_not_fuzzy_header(self):
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
88 buf = StringIO(r'''\
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
89 # Translations template for AReallyReallyLongNameForAProject.
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
90 # Copyright (C) 2007 ORGANIZATION
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
91 # This file is distributed under the same license as the
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
92 # AReallyReallyLongNameForAProject project.
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
93 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
94 #
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
95 ''')
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
96 catalog = pofile.read_po(buf)
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
97 self.assertEqual(1, len(list(catalog)))
47f6c31e9a24 Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents: 108
diff changeset
98 self.assertEqual(False, list(catalog)[0].fuzzy)
108
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
99
293
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
100 def test_header_entry(self):
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
101 buf = StringIO(r'''\
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
102 # SOME DESCRIPTIVE TITLE.
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
103 # Copyright (C) 2007 THE PACKAGE'S COPYRIGHT HOLDER
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
104 # This file is distributed under the same license as the PACKAGE package.
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
105 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
106 #
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
107 #, fuzzy
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
108 msgid ""
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
109 msgstr ""
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
110 "Project-Id-Version: 3.15\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
111 "Report-Msgid-Bugs-To: Fliegender Zirkus <fliegender@zirkus.de>\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
112 "POT-Creation-Date: 2007-09-27 11:19+0700\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
113 "PO-Revision-Date: 2007-09-27 21:42-0700\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
114 "Last-Translator: John <cleese@bavaria.de>\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
115 "Language-Team: German Lang <de@babel.org>\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
116 "Plural-Forms: nplurals=2; plural=(n != 1)\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
117 "MIME-Version: 1.0\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
118 "Content-Type: text/plain; charset=iso-8859-2\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
119 "Content-Transfer-Encoding: 8bit\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
120 "Generated-By: Babel 1.0dev-r313\n"
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
121 ''')
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
122 catalog = pofile.read_po(buf)
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
123 self.assertEqual(1, len(list(catalog)))
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
124 self.assertEqual(u'3.15', catalog.version)
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
125 self.assertEqual(u'Fliegender Zirkus <fliegender@zirkus.de>',
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
126 catalog.msgid_bugs_address)
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
127 self.assertEqual(datetime(2007, 9, 27, 11, 19,
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
128 tzinfo=FixedOffsetTimezone(7 * 60)),
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
129 catalog.creation_date)
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
130 self.assertEqual(u'John <cleese@bavaria.de>', catalog.last_translator)
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
131 self.assertEqual(u'German Lang <de@babel.org>', catalog.language_team)
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
132 self.assertEqual(u'iso-8859-2', catalog.charset)
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
133 self.assertEqual(True, list(catalog)[0].fuzzy)
62d4f85d33ea fix catalogs' charset values not being recognized
pjenvey
parents: 251
diff changeset
134
201
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
135 def test_obsolete_message(self):
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
136 buf = StringIO(r'''# This is an obsolete message
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
137 #~ msgid "foo"
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
138 #~ msgstr "Voh"
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
139
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
140 # This message is not obsolete
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
141 #: main.py:1
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
142 msgid "bar"
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
143 msgstr "Bahr"
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
144 ''')
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
145 catalog = pofile.read_po(buf)
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
146 self.assertEqual(1, len(catalog))
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
147 self.assertEqual(1, len(catalog.obsolete))
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
148 message = catalog.obsolete[u'foo']
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
149 self.assertEqual(u'foo', message.id)
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
150 self.assertEqual(u'Voh', message.string)
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
151 self.assertEqual(['This is an obsolete message'], message.user_comments)
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
152
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
153 def test_obsolete_message_ignored(self):
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
154 buf = StringIO(r'''# This is an obsolete message
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
155 #~ msgid "foo"
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
156 #~ msgstr "Voh"
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
157
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
158 # This message is not obsolete
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
159 #: main.py:1
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
160 msgid "bar"
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
161 msgstr "Bahr"
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
162 ''')
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
163 catalog = pofile.read_po(buf, ignore_obsolete=True)
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
164 self.assertEqual(1, len(catalog))
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
165 self.assertEqual(0, len(catalog.obsolete))
10e8d072e2d1 Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 198
diff changeset
166
337
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
167 def test_with_context(self):
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
168 buf = StringIO(r'''# Some string in the menu
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
169 #: main.py:1
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
170 msgctxt "Menu"
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
171 msgid "foo"
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
172 msgstr "Voh"
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
173
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
174 # Another string in the menu
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
175 #: main.py:2
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
176 msgctxt "Menu"
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
177 msgid "bar"
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
178 msgstr "Bahr"
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
179 ''')
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
180 catalog = pofile.read_po(buf, ignore_obsolete=True)
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
181 self.assertEqual(2, len(catalog))
352
90849c44c531 More work on msgctxt support (#54).
cmlenz
parents: 337
diff changeset
182 message = catalog.get('foo', context='Menu')
337
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
183 self.assertEqual('Menu', message.context)
352
90849c44c531 More work on msgctxt support (#54).
cmlenz
parents: 337
diff changeset
184 message = catalog.get('bar', context='Menu')
337
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
185 self.assertEqual('Menu', message.context)
662d332c0a2b More preparation for msgctxt support (#54).
cmlenz
parents: 317
diff changeset
186
423
322b257aeb3c Include patch from Asheesh Laroia. Fixes #45.
palgarvio
parents: 379
diff changeset
187 # And verify it pass through write_po
322b257aeb3c Include patch from Asheesh Laroia. Fixes #45.
palgarvio
parents: 379
diff changeset
188 out_buf = StringIO()
322b257aeb3c Include patch from Asheesh Laroia. Fixes #45.
palgarvio
parents: 379
diff changeset
189 pofile.write_po(out_buf, catalog, omit_header=True)
322b257aeb3c Include patch from Asheesh Laroia. Fixes #45.
palgarvio
parents: 379
diff changeset
190 assert out_buf.getvalue().strip() == buf.getvalue().strip(), \
322b257aeb3c Include patch from Asheesh Laroia. Fixes #45.
palgarvio
parents: 379
diff changeset
191 out_buf.getvalue()
322b257aeb3c Include patch from Asheesh Laroia. Fixes #45.
palgarvio
parents: 379
diff changeset
192
430
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
193 def test_with_context_two(self):
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
194 buf = StringIO(r'''msgctxt "Menu"
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
195 msgid "foo"
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
196 msgstr "Voh"
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
197
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
198 msgctxt "Mannu"
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
199 msgid "bar"
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
200 msgstr "Bahr"
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
201 ''')
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
202 catalog = pofile.read_po(buf, ignore_obsolete=True)
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
203 self.assertEqual(2, len(catalog))
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
204 message = catalog.get('foo', context='Menu')
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
205 self.assertEqual('Menu', message.context)
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
206 message = catalog.get('bar', context='Mannu')
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
207 self.assertEqual('Mannu', message.context)
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
208
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
209 # And verify it pass through write_po
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
210 out_buf = StringIO()
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
211 pofile.write_po(out_buf, catalog, omit_header=True)
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
212 assert out_buf.getvalue().strip() == buf.getvalue().strip(), out_buf.getvalue()
70f72bc70a93 Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents: 425
diff changeset
213
445
3474ffd631e6 Add a test for a locale with one plural form.
jruigrok
parents: 444
diff changeset
214 def test_single_plural_form(self):
3474ffd631e6 Add a test for a locale with one plural form.
jruigrok
parents: 444
diff changeset
215 buf = StringIO(r'''msgid "foo"
448
ac3cd0d08e95 Actually make the msgid_plural be a plural as per typical use case.
jruigrok
parents: 447
diff changeset
216 msgid_plural "foos"
445
3474ffd631e6 Add a test for a locale with one plural form.
jruigrok
parents: 444
diff changeset
217 msgstr[0] "Voh"''')
3474ffd631e6 Add a test for a locale with one plural form.
jruigrok
parents: 444
diff changeset
218 catalog = pofile.read_po(buf, locale='ja_JP')
3474ffd631e6 Add a test for a locale with one plural form.
jruigrok
parents: 444
diff changeset
219 self.assertEqual(1, len(catalog))
3474ffd631e6 Add a test for a locale with one plural form.
jruigrok
parents: 444
diff changeset
220 self.assertEqual(1, catalog.num_plurals)
3474ffd631e6 Add a test for a locale with one plural form.
jruigrok
parents: 444
diff changeset
221 message = catalog['foo']
3474ffd631e6 Add a test for a locale with one plural form.
jruigrok
parents: 444
diff changeset
222 self.assertEqual(1, len(message.string))
3474ffd631e6 Add a test for a locale with one plural form.
jruigrok
parents: 444
diff changeset
223
444
865b13853345 Fix testcase name typo.
jruigrok
parents: 443
diff changeset
224 def test_singular_plural_form(self):
372
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
225 buf = StringIO(r'''msgid "foo"
448
ac3cd0d08e95 Actually make the msgid_plural be a plural as per typical use case.
jruigrok
parents: 447
diff changeset
226 msgid_plural "foos"
372
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
227 msgstr[0] "Voh"
444
865b13853345 Fix testcase name typo.
jruigrok
parents: 443
diff changeset
228 msgstr[1] "Vohs"''')
865b13853345 Fix testcase name typo.
jruigrok
parents: 443
diff changeset
229 catalog = pofile.read_po(buf, locale='nl_NL')
372
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
230 self.assertEqual(1, len(catalog))
444
865b13853345 Fix testcase name typo.
jruigrok
parents: 443
diff changeset
231 self.assertEqual(2, catalog.num_plurals)
379
de824f875d99 Use item access to catalog messages in tests, so that they can be easily ported back to the 0.9.x branch.
cmlenz
parents: 372
diff changeset
232 message = catalog['foo']
444
865b13853345 Fix testcase name typo.
jruigrok
parents: 443
diff changeset
233 self.assertEqual(2, len(message.string))
443
d2e9aaa7c91c Make sure to only strip on the first occurence of ].
jruigrok
parents: 430
diff changeset
234
372
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
235 def test_more_than_two_plural_forms(self):
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
236 buf = StringIO(r'''msgid "foo"
448
ac3cd0d08e95 Actually make the msgid_plural be a plural as per typical use case.
jruigrok
parents: 447
diff changeset
237 msgid_plural "foos"
372
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
238 msgstr[0] "Voh"
447
841e5ca971a0 More than two plural forms work nowadays. Adjust the test.
jruigrok
parents: 446
diff changeset
239 msgstr[1] "Vohs"
841e5ca971a0 More than two plural forms work nowadays. Adjust the test.
jruigrok
parents: 446
diff changeset
240 msgstr[2] "Vohss"''')
372
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
241 catalog = pofile.read_po(buf, locale='lv_LV')
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
242 self.assertEqual(1, len(catalog))
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
243 self.assertEqual(3, catalog.num_plurals)
379
de824f875d99 Use item access to catalog messages in tests, so that they can be easily ported back to the 0.9.x branch.
cmlenz
parents: 372
diff changeset
244 message = catalog['foo']
372
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
245 self.assertEqual(3, len(message.string))
447
841e5ca971a0 More than two plural forms work nowadays. Adjust the test.
jruigrok
parents: 446
diff changeset
246 self.assertEqual(u'Vohss', message.string[2])
372
d1a9c618d2d5 We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents: 358
diff changeset
247
446
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
248 def test_plural_with_square_brackets(self):
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
249 buf = StringIO(r'''msgid "foo"
448
ac3cd0d08e95 Actually make the msgid_plural be a plural as per typical use case.
jruigrok
parents: 447
diff changeset
250 msgid_plural "foos"
446
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
251 msgstr[0] "Voh [text]"
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
252 msgstr[1] "Vohs [text]"''')
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
253 catalog = pofile.read_po(buf, locale='nb_NO')
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
254 self.assertEqual(1, len(catalog))
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
255 self.assertEqual(2, catalog.num_plurals)
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
256 message = catalog['foo']
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
257 self.assertEqual(2, len(message.string))
99f5a4551ba8 Put the square brackets test after all the normal plural tests. It's more
jruigrok
parents: 445
diff changeset
258
108
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
259
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
260 class WritePoTestCase(unittest.TestCase):
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
261
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
262 def test_join_locations(self):
58
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 56
diff changeset
263 catalog = Catalog()
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 56
diff changeset
264 catalog.add(u'foo', locations=[('main.py', 1)])
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 56
diff changeset
265 catalog.add(u'foo', locations=[('utils.py', 3)])
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
266 buf = StringIO()
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
267 pofile.write_po(buf, catalog, omit_header=True)
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
268 self.assertEqual('''#: main.py:1 utils.py:3
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
269 msgid "foo"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
270 msgstr ""''', buf.getvalue().strip())
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
271
547
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
272 def test_write_po_file_with_specified_charset(self):
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
273 catalog = Catalog(charset='iso-8859-1')
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
274 catalog.add('foo', u'äöü', locations=[('main.py', 1)])
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
275 buf = StringIO()
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
276 pofile.write_po(buf, catalog, omit_header=False)
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
277 po_file = buf.getvalue().strip()
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
278 assert r'"Content-Type: text/plain; charset=iso-8859-1\n"' in po_file
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
279 assert u'msgstr "äöü"'.encode('iso-8859-1') in po_file
274f9a6485d4 Catalog class should not do decoding of input strings (fixes #256)
fschwarz
parents: 546
diff changeset
280
230
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
281 def test_duplicate_comments(self):
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
282 catalog = Catalog()
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
283 catalog.add(u'foo', auto_comments=['A comment'])
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
284 catalog.add(u'foo', auto_comments=['A comment'])
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
285 buf = StringIO()
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
286 pofile.write_po(buf, catalog, omit_header=True)
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
287 self.assertEqual('''#. A comment
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
288 msgid "foo"
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
289 msgstr ""''', buf.getvalue().strip())
aaf36f409166 Follow-up to [239]: also combine duplicate comments when writing PO files.
cmlenz
parents: 205
diff changeset
290
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
291 def test_wrap_long_lines(self):
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
292 text = """Here's some text where
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
293 white space and line breaks matter, and should
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
294
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
295 not be removed
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
296
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
297 """
58
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 56
diff changeset
298 catalog = Catalog()
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 56
diff changeset
299 catalog.add(text, locations=[('main.py', 1)])
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
300 buf = StringIO()
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
301 pofile.write_po(buf, catalog, no_location=True, omit_header=True,
58
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 56
diff changeset
302 width=42)
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
303 self.assertEqual(r'''msgid ""
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
304 "Here's some text where \n"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
305 "white space and line breaks matter, and"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
306 " should\n"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
307 "\n"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
308 "not be removed\n"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
309 "\n"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
310 msgstr ""''', buf.getvalue().strip())
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
311
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
312 def test_wrap_long_lines_with_long_word(self):
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
313 text = """Here's some text that
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
314 includesareallylongwordthatmightbutshouldnt throw us into an infinite loop
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
315 """
58
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 56
diff changeset
316 catalog = Catalog()
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 56
diff changeset
317 catalog.add(text, locations=[('main.py', 1)])
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
318 buf = StringIO()
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
319 pofile.write_po(buf, catalog, no_location=True, omit_header=True,
58
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 56
diff changeset
320 width=32)
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
321 self.assertEqual(r'''msgid ""
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
322 "Here's some text that\n"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
323 "includesareallylongwordthatmightbutshouldnt"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
324 " throw us into an infinite "
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
325 "loop\n"
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
326 msgstr ""''', buf.getvalue().strip())
82
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
327
105
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
328 def test_wrap_long_lines_in_header(self):
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
329 """
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
330 Verify that long lines in the header comment are wrapped correctly.
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
331 """
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
332 catalog = Catalog(project='AReallyReallyLongNameForAProject',
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
333 revision_date=datetime(2007, 4, 1))
105
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
334 buf = StringIO()
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
335 pofile.write_po(buf, catalog)
105
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
336 self.assertEqual('''\
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
337 # Translations template for AReallyReallyLongNameForAProject.
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
338 # Copyright (C) 2007 ORGANIZATION
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
339 # This file is distributed under the same license as the
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
340 # AReallyReallyLongNameForAProject project.
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
341 # FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
105
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
342 #
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
343 #, fuzzy''', '\n'.join(buf.getvalue().splitlines()[:7]))
abd3a594dab4 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 82
diff changeset
344
317
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
345 def test_wrap_locations_with_hyphens(self):
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
346 catalog = Catalog()
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
347 catalog.add(u'foo', locations=[
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
348 ('doupy/templates/base/navmenu.inc.html.py', 60)
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
349 ])
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
350 catalog.add(u'foo', locations=[
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
351 ('doupy/templates/job-offers/helpers.html', 22)
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
352 ])
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
353 buf = StringIO()
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
354 pofile.write_po(buf, catalog, omit_header=True)
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
355 self.assertEqual('''#: doupy/templates/base/navmenu.inc.html.py:60
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
356 #: doupy/templates/job-offers/helpers.html:22
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
357 msgid "foo"
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
358 msgstr ""''', buf.getvalue().strip())
425
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
359
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
360 def test_no_wrap_and_width_behaviour_on_comments(self):
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
361 catalog = Catalog()
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
362 catalog.add("Pretty dam long message id, which must really be big "
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
363 "to test this wrap behaviour, if not it won't work.",
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
364 locations=[("fake.py", n) for n in range(1, 30)])
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
365 buf = StringIO()
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
366 pofile.write_po(buf, catalog, width=None, omit_header=True)
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
367 self.assertEqual("""\
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
368 #: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
369 #: fake.py:8 fake.py:9 fake.py:10 fake.py:11 fake.py:12 fake.py:13 fake.py:14
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
370 #: fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 fake.py:20 fake.py:21
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
371 #: fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
372 #: fake.py:29
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
373 msgid "pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't work."
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
374 msgstr ""
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
375
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
376 """, buf.getvalue().lower())
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
377 buf = StringIO()
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
378 pofile.write_po(buf, catalog, width=100, omit_header=True)
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
379 self.assertEqual("""\
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
380 #: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 fake.py:8 fake.py:9 fake.py:10
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
381 #: fake.py:11 fake.py:12 fake.py:13 fake.py:14 fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
382 #: fake.py:20 fake.py:21 fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
383 #: fake.py:29
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
384 msgid ""
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
385 "pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't"
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
386 " work."
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
387 msgstr ""
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
388
15541acbe8cb Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents: 423
diff changeset
389 """, buf.getvalue().lower())
317
b997f09256c9 Fix for #79 (location lines wrapping at hyphens).
cmlenz
parents: 293
diff changeset
390
82
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
391 def test_pot_with_translator_comments(self):
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
392 catalog = Catalog()
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
393 catalog.add(u'foo', locations=[('main.py', 1)],
107
4b42e23644e5 `Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents: 106
diff changeset
394 auto_comments=['Comment About `foo`'])
82
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
395 catalog.add(u'bar', locations=[('utils.py', 3)],
107
4b42e23644e5 `Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents: 106
diff changeset
396 user_comments=['Comment About `bar` with',
4b42e23644e5 `Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents: 106
diff changeset
397 'multiple lines.'])
82
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
398 buf = StringIO()
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
399 pofile.write_po(buf, catalog, omit_header=True)
82
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
400 self.assertEqual('''#. Comment About `foo`
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
401 #: main.py:1
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
402 msgid "foo"
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
403 msgstr ""
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
404
107
4b42e23644e5 `Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents: 106
diff changeset
405 # Comment About `bar` with
4b42e23644e5 `Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents: 106
diff changeset
406 # multiple lines.
82
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
407 #: utils.py:3
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
408 msgid "bar"
f421e5576d26 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 58
diff changeset
409 msgstr ""''', buf.getvalue().strip())
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
410
192
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
411 def test_po_with_obsolete_message(self):
183
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
412 catalog = Catalog()
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
413 catalog.add(u'foo', u'Voh', locations=[('main.py', 1)])
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
414 catalog.obsolete['bar'] = Message(u'bar', u'Bahr',
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
415 locations=[('utils.py', 3)],
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
416 user_comments=['User comment'])
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
417 buf = StringIO()
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
418 pofile.write_po(buf, catalog, omit_header=True)
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
419 self.assertEqual('''#: main.py:1
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
420 msgid "foo"
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
421 msgstr "Voh"
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
422
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
423 # User comment
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
424 #~ msgid "bar"
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
425 #~ msgstr "Bahr"''', buf.getvalue().strip())
e927dffc9ab4 The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 177
diff changeset
426
192
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
427 def test_po_with_multiline_obsolete_message(self):
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
428 catalog = Catalog()
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
429 catalog.add(u'foo', u'Voh', locations=[('main.py', 1)])
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
430 msgid = r"""Here's a message that covers
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
431 multiple lines, and should still be handled
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
432 correctly.
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
433 """
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
434 msgstr = r"""Here's a message that covers
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
435 multiple lines, and should still be handled
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
436 correctly.
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
437 """
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
438 catalog.obsolete[msgid] = Message(msgid, msgstr,
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
439 locations=[('utils.py', 3)])
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
440 buf = StringIO()
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
441 pofile.write_po(buf, catalog, omit_header=True)
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
442 self.assertEqual(r'''#: main.py:1
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
443 msgid "foo"
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
444 msgstr "Voh"
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
445
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
446 #~ msgid ""
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
447 #~ "Here's a message that covers\n"
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
448 #~ "multiple lines, and should still be handled\n"
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
449 #~ "correctly.\n"
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
450 #~ msgstr ""
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
451 #~ "Here's a message that covers\n"
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
452 #~ "multiple lines, and should still be handled\n"
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
453 #~ "correctly.\n"''', buf.getvalue().strip())
8f5805197198 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 183
diff changeset
454
193
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
455 def test_po_with_obsolete_message_ignored(self):
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
456 catalog = Catalog()
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
457 catalog.add(u'foo', u'Voh', locations=[('main.py', 1)])
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
458 catalog.obsolete['bar'] = Message(u'bar', u'Bahr',
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
459 locations=[('utils.py', 3)],
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
460 user_comments=['User comment'])
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
461 buf = StringIO()
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
462 pofile.write_po(buf, catalog, omit_header=True, ignore_obsolete=True)
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
463 self.assertEqual('''#: main.py:1
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
464 msgid "foo"
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
465 msgstr "Voh"''', buf.getvalue().strip())
b5e58a22ebd2 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 192
diff changeset
466
205
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
467 def test_po_with_previous_msgid(self):
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
468 catalog = Catalog()
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
469 catalog.add(u'foo', u'Voh', locations=[('main.py', 1)],
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
470 previous_id=u'fo')
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
471 buf = StringIO()
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
472 pofile.write_po(buf, catalog, omit_header=True, include_previous=True)
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
473 self.assertEqual('''#: main.py:1
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
474 #| msgid "fo"
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
475 msgid "foo"
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
476 msgstr "Voh"''', buf.getvalue().strip())
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
477
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
478 def test_po_with_previous_msgid_plural(self):
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
479 catalog = Catalog()
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
480 catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'),
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
481 locations=[('main.py', 1)], previous_id=(u'fo', u'fos'))
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
482 buf = StringIO()
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
483 pofile.write_po(buf, catalog, omit_header=True, include_previous=True)
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
484 self.assertEqual('''#: main.py:1
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
485 #| msgid "fo"
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
486 #| msgid_plural "fos"
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
487 msgid "foo"
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
488 msgid_plural "foos"
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
489 msgstr[0] "Voh"
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
490 msgstr[1] "Voeh"''', buf.getvalue().strip())
aefe4ac123a2 Minor changes to how previous msgids are processed.
cmlenz
parents: 201
diff changeset
491
251
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
492 def test_sorted_po(self):
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
493 catalog = Catalog()
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
494 catalog.add(u'bar', locations=[('utils.py', 3)],
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
495 user_comments=['Comment About `bar` with',
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
496 'multiple lines.'])
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
497 catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'),
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
498 locations=[('main.py', 1)])
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
499 buf = StringIO()
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
500 pofile.write_po(buf, catalog, sort_output=True)
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
501 value = buf.getvalue().strip()
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
502 assert '''\
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
503 # Comment About `bar` with
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
504 # multiple lines.
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
505 #: utils.py:3
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
506 msgid "bar"
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
507 msgstr ""
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
508
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
509 #: main.py:1
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
510 msgid "foo"
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
511 msgid_plural "foos"
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
512 msgstr[0] "Voh"
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
513 msgstr[1] "Voeh"''' in value
3b9d993b7aa3 added test cases for correct po/mofile sorting, following up r264
pjenvey
parents: 230
diff changeset
514 assert value.find('msgid ""') < value.find('msgid "bar"') < value.find('msgid "foo"')
26
93eaa2f4a0a2 Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 19
diff changeset
515
358
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
516 def test_silent_location_fallback(self):
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
517 buf = StringIO('''\
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
518 #: broken_file.py
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
519 msgid "missing line number"
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
520 msgstr ""
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
521
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
522 #: broken_file.py:broken_line_number
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
523 msgid "broken line number"
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
524 msgstr ""''')
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
525 catalog = pofile.read_po(buf)
379
de824f875d99 Use item access to catalog messages in tests, so that they can be easily ported back to the 0.9.x branch.
cmlenz
parents: 372
diff changeset
526 self.assertEqual(catalog['missing line number'].locations, [])
de824f875d99 Use item access to catalog messages in tests, so that they can be easily ported back to the 0.9.x branch.
cmlenz
parents: 372
diff changeset
527 self.assertEqual(catalog['broken line number'].locations, [])
358
c82ad0f5ff65 Fixed #59 by falling back silently on invalid location comments.
aronacher
parents: 352
diff changeset
528
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
529 def suite():
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
530 suite = unittest.TestSuite()
546
10de195cfb04 catalog.add() now returns the message instance (closes #245)
fschwarz
parents: 533
diff changeset
531 suite.addTest(doctest.DocTestSuite(pofile, optionflags=doctest.ELLIPSIS))
108
8ea225f33f28 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 107
diff changeset
532 suite.addTest(unittest.makeSuite(ReadPoTestCase))
106
2a00e352c986 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 105
diff changeset
533 suite.addTest(unittest.makeSuite(WritePoTestCase))
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
534 return suite
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
535
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
536 if __name__ == '__main__':
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
537 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software