annotate babel/messages/pofile.py @ 96:11e90b91a4f0

Also use tzinfo for `write_po`.
author palgarvio
date Tue, 12 Jun 2007 19:57:18 +0000
parents b176f325d127
children a02952b73cf1
rev   line source
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
2 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
5 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
9 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
13
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
14 """Reading and writing of files in the ``gettext`` PO (portable object)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
15 format.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
16
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
17 :see: `The Format of PO Files
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
18 <http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
19 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
20
5
50ad95bee876 * The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents: 1
diff changeset
21 from datetime import date, datetime
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
22 import re
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
23 try:
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
24 set
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
25 except NameError:
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
26 from sets import Set as set
24
4fad20ab7cca 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: 23
diff changeset
27 import textwrap
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
28
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
29 from babel import __version__ as VERSION
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
30 from babel.messages.catalog import Catalog
96
11e90b91a4f0 Also use tzinfo for `write_po`.
palgarvio
parents: 94
diff changeset
31 from babel.util import LOCAL
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
32
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
33 __all__ = ['escape', 'normalize', 'read_po', 'write_po', 'write_pot']
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
34
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
35 def read_po(fileobj):
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
36 """Read messages from a ``gettext`` PO (portable object) file from the given
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
37 file-like object and return a `Catalog`.
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
38
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
39 >>> from StringIO import StringIO
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
40 >>> buf = StringIO('''
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
41 ... #: main.py:1
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
42 ... #, fuzzy, python-format
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
43 ... msgid "foo %(name)s"
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
44 ... msgstr ""
21
ddfac856c34f Change pot header's first line, "Translations Template for %%(project)s." instead of "SOME DESCRIPTIVE TITLE.". '''`project`''' and '''`version`''' now default to '''PROJECT''' and '''VERSION''' respectively. Fixed a bug regarding '''Content-Transfer-Encoding''', it shouldn't be the charset, and we're defaulting to `8bit` untill someone complains.
palgarvio
parents: 17
diff changeset
45 ...
94
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
46 ... # A user comment
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
47 ... #. An auto comment
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
48 ... #: main.py:3
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
49 ... msgid "bar"
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
50 ... msgid_plural "baz"
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
51 ... msgstr[0] ""
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
52 ... msgstr[1] ""
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
53 ... ''')
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
54 >>> catalog = read_po(buf)
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
55 >>> for message in catalog:
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
56 ... if message.id:
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
57 ... print (message.id, message.string)
94
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
58 ... print ' ', (message.locations, message.flags, message.comments)
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
59 ('foo %(name)s', '')
94
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
60 ([('main.py', 1)], set(['fuzzy', 'python-format']), [])
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
61 (('bar', 'baz'), ('', ''))
94
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
62 ([('main.py', 3)], set([]), ['A user comment', 'An auto comment'])
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
63
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
64 :param fileobj: the file-like object to read the PO file from
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
65 :return: an iterator over ``(message, translation, location)`` tuples
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
66 :rtype: ``iterator``
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
67 """
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
68 catalog = Catalog()
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
69
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
70 messages = []
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
71 translations = []
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
72 locations = []
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
73 flags = []
84
4ff9cc26c11b Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
74 comments = []
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
75 in_msgid = in_msgstr = False
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
76
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
77 def _add_message():
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
78 translations.sort()
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
79 if len(messages) > 1:
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
80 msgid = tuple(messages)
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
81 else:
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
82 msgid = messages[0]
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
83 if len(translations) > 1:
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
84 string = tuple([t[1] for t in translations])
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
85 else:
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
86 string = translations[0][1]
84
4ff9cc26c11b Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
87 catalog.add(msgid, string, list(locations), set(flags), list(comments))
4ff9cc26c11b Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
88 del messages[:]; del translations[:]; del locations[:];
4ff9cc26c11b Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
89 del flags[:]; del comments[:]
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
90
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
91 for line in fileobj.readlines():
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
92 line = line.strip()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
93 if line.startswith('#'):
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
94 in_msgid = in_msgstr = False
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
95 if messages:
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
96 _add_message()
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
97 if line[1:].startswith(':'):
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
98 for location in line[2:].lstrip().split():
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
99 filename, lineno = location.split(':', 1)
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
100 locations.append((filename, int(lineno)))
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
101 elif line[1:].startswith(','):
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
102 for flag in line[2:].lstrip().split(','):
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
103 flags.append(flag.strip())
84
4ff9cc26c11b Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
104 elif line[1:].startswith('.'):
94
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
105 # These are called auto-comments
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
106 comment = line[2:].strip()
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
107 if comment:
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
108 # Just check that we're not adding empty comments
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
109 comments.append(comment)
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
110 elif line[1:].startswith(' '):
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
111 # These are called user comments
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
112 comment = line[1:].strip()
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
113 if comment:
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
114 # Just check that we're not adding empty comments
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
115 comments.append(comment)
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
116 elif line:
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
117 if line.startswith('msgid_plural'):
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
118 in_msgid = True
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
119 msg = line[12:].lstrip()
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
120 messages.append(msg[1:-1])
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
121 elif line.startswith('msgid'):
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
122 in_msgid = True
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
123 if messages:
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
124 _add_message()
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
125 msg = line[5:].lstrip()
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
126 messages.append(msg[1:-1])
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
127 elif line.startswith('msgstr'):
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
128 in_msgid = False
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
129 in_msgstr = True
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
130 msg = line[6:].lstrip()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
131 if msg.startswith('['):
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
132 idx, msg = msg[1:].split(']')
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
133 translations.append([int(idx), msg.lstrip()[1:-1]])
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
134 else:
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
135 translations.append([0, msg[1:-1]])
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
136 elif line.startswith('"'):
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
137 if in_msgid:
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
138 messages[-1] += line.rstrip()[1:-1]
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
139 elif in_msgstr:
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
140 translations[-1][1] += line.rstrip()[1:-1]
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
141
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
142 if messages:
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
143 _add_message()
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
144 return catalog
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
145
24
4fad20ab7cca 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: 23
diff changeset
146 POT_HEADER = """\
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
147 # Translations template for %(project)s.
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
148 # Copyright (C) %(year)s %(copyright_holder)s
27
8521326b0627 Changing `write_po` to include licensing info, same as the project. Since the method is also to create the initial pot file, we also include the year in the copyright. We also mark the translations catalog template as fuzzy as it should be, only localized translations catalogs ready to be compiled should not include the fuzzy bit on the header.
palgarvio
parents: 26
diff changeset
149 # This file is distributed under the same license as the
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
150 # %(project)s project.
24
4fad20ab7cca 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: 23
diff changeset
151 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
4fad20ab7cca 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: 23
diff changeset
152 #
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
153 """
24
4fad20ab7cca 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: 23
diff changeset
154
4fad20ab7cca 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: 23
diff changeset
155 WORD_SEP = re.compile('('
4fad20ab7cca 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: 23
diff changeset
156 r'\s+|' # any whitespace
4fad20ab7cca 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: 23
diff changeset
157 r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words
4fad20ab7cca 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: 23
diff changeset
158 r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w)' # em-dash
4fad20ab7cca 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: 23
diff changeset
159 ')')
4fad20ab7cca 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: 23
diff changeset
160
4fad20ab7cca 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: 23
diff changeset
161 def escape(string):
4fad20ab7cca 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: 23
diff changeset
162 r"""Escape the given string so that it can be included in double-quoted
4fad20ab7cca 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: 23
diff changeset
163 strings in ``PO`` files.
4fad20ab7cca 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: 23
diff changeset
164
4fad20ab7cca 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: 23
diff changeset
165 >>> escape('''Say:
4fad20ab7cca 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: 23
diff changeset
166 ... "hello, world!"
4fad20ab7cca 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: 23
diff changeset
167 ... ''')
4fad20ab7cca 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: 23
diff changeset
168 '"Say:\\n \\"hello, world!\\"\\n"'
4fad20ab7cca 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: 23
diff changeset
169
4fad20ab7cca 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: 23
diff changeset
170 :param string: the string to escape
4fad20ab7cca 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: 23
diff changeset
171 :return: the escaped string
4fad20ab7cca 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: 23
diff changeset
172 :rtype: `str` or `unicode`
4fad20ab7cca 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: 23
diff changeset
173 """
4fad20ab7cca 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: 23
diff changeset
174 return '"%s"' % string.replace('\\', '\\\\') \
4fad20ab7cca 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: 23
diff changeset
175 .replace('\t', '\\t') \
4fad20ab7cca 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: 23
diff changeset
176 .replace('\r', '\\r') \
4fad20ab7cca 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: 23
diff changeset
177 .replace('\n', '\\n') \
4fad20ab7cca 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: 23
diff changeset
178 .replace('\"', '\\"')
4fad20ab7cca 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: 23
diff changeset
179
4fad20ab7cca 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: 23
diff changeset
180 def normalize(string, width=76):
4fad20ab7cca 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: 23
diff changeset
181 r"""This converts a string into a format that is appropriate for .po files.
4fad20ab7cca 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: 23
diff changeset
182
4fad20ab7cca 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: 23
diff changeset
183 >>> print normalize('''Say:
4fad20ab7cca 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: 23
diff changeset
184 ... "hello, world!"
4fad20ab7cca 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: 23
diff changeset
185 ... ''', width=None)
4fad20ab7cca 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: 23
diff changeset
186 ""
4fad20ab7cca 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: 23
diff changeset
187 "Say:\n"
4fad20ab7cca 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: 23
diff changeset
188 " \"hello, world!\"\n"
4fad20ab7cca 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: 23
diff changeset
189
4fad20ab7cca 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: 23
diff changeset
190 >>> print normalize('''Say:
4fad20ab7cca 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: 23
diff changeset
191 ... "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
4fad20ab7cca 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: 23
diff changeset
192 ... ''', width=32)
4fad20ab7cca 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: 23
diff changeset
193 ""
4fad20ab7cca 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: 23
diff changeset
194 "Say:\n"
4fad20ab7cca 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: 23
diff changeset
195 " \"Lorem ipsum dolor sit "
4fad20ab7cca 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: 23
diff changeset
196 "amet, consectetur adipisicing"
4fad20ab7cca 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: 23
diff changeset
197 " elit, \"\n"
4fad20ab7cca 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: 23
diff changeset
198
4fad20ab7cca 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: 23
diff changeset
199 :param string: the string to normalize
4fad20ab7cca 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: 23
diff changeset
200 :param width: the maximum line width; use `None`, 0, or a negative number
4fad20ab7cca 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: 23
diff changeset
201 to completely disable line wrapping
4fad20ab7cca 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: 23
diff changeset
202 :return: the normalized string
4fad20ab7cca 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: 23
diff changeset
203 :rtype: `unicode`
4fad20ab7cca 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: 23
diff changeset
204 """
4fad20ab7cca 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: 23
diff changeset
205 if width and width > 0:
4fad20ab7cca 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: 23
diff changeset
206 lines = []
4fad20ab7cca 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: 23
diff changeset
207 for idx, line in enumerate(string.splitlines(True)):
4fad20ab7cca 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: 23
diff changeset
208 if len(escape(line)) > width:
4fad20ab7cca 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: 23
diff changeset
209 chunks = WORD_SEP.split(line)
4fad20ab7cca 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: 23
diff changeset
210 chunks.reverse()
4fad20ab7cca 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: 23
diff changeset
211 while chunks:
4fad20ab7cca 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: 23
diff changeset
212 buf = []
4fad20ab7cca 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: 23
diff changeset
213 size = 2
4fad20ab7cca 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: 23
diff changeset
214 while chunks:
4fad20ab7cca 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: 23
diff changeset
215 l = len(escape(chunks[-1])) - 2
4fad20ab7cca 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: 23
diff changeset
216 if size + l < width:
4fad20ab7cca 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: 23
diff changeset
217 buf.append(chunks.pop())
4fad20ab7cca 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: 23
diff changeset
218 size += l
4fad20ab7cca 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: 23
diff changeset
219 else:
4fad20ab7cca 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: 23
diff changeset
220 if not buf:
4fad20ab7cca 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: 23
diff changeset
221 # handle long chunks by putting them on a
4fad20ab7cca 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: 23
diff changeset
222 # separate line
4fad20ab7cca 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: 23
diff changeset
223 buf.append(chunks.pop())
4fad20ab7cca 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: 23
diff changeset
224 break
4fad20ab7cca 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: 23
diff changeset
225 lines.append(u''.join(buf))
4fad20ab7cca 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: 23
diff changeset
226 else:
4fad20ab7cca 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: 23
diff changeset
227 lines.append(line)
4fad20ab7cca 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: 23
diff changeset
228 else:
4fad20ab7cca 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: 23
diff changeset
229 lines = string.splitlines(True)
4fad20ab7cca 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: 23
diff changeset
230
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
231 if len(lines) <= 1:
24
4fad20ab7cca 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: 23
diff changeset
232 return escape(string)
4fad20ab7cca 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: 23
diff changeset
233
4fad20ab7cca 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: 23
diff changeset
234 # Remove empty trailing line
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
235 if lines and not lines[-1]:
24
4fad20ab7cca 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: 23
diff changeset
236 del lines[-1]
4fad20ab7cca 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: 23
diff changeset
237 lines[-1] += '\n'
4fad20ab7cca 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: 23
diff changeset
238 return u'""\n' + u'\n'.join([escape(l) for l in lines])
4fad20ab7cca 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: 23
diff changeset
239
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
240 def write_pot(fileobj, catalog, project='PROJECT', version='VERSION', width=76,
78
ee043bb666f0 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
241 charset='utf-8', no_location=False, omit_header=False,
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
242 sort_output=False, sort_by_file=False, copyright_holder=None):
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
243 r"""Write a ``gettext`` PO (portable object) template file for a given
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
244 message catalog to the provided file-like object.
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
245
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
246 >>> catalog = Catalog()
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
247 >>> catalog.add(u'foo %(name)s', locations=[('main.py', 1)],
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
248 ... flags=('fuzzy',))
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
249 >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)])
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
250 >>> from StringIO import StringIO
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
251 >>> buf = StringIO()
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
252 >>> write_pot(buf, catalog, omit_header=True)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
253 >>> print buf.getvalue()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
254 #: main.py:1
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
255 #, fuzzy, python-format
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
256 msgid "foo %(name)s"
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
257 msgstr ""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
258 <BLANKLINE>
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
259 #: main.py:3
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
260 msgid "bar"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
261 msgid_plural "baz"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
262 msgstr[0] ""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
263 msgstr[1] ""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
264 <BLANKLINE>
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
265 <BLANKLINE>
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
266
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
267 :param fileobj: the file-like object to write to
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
268 :param catalog: the `Catalog` instance
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
269 :param project: the project name
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
270 :param version: the project version
24
4fad20ab7cca 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: 23
diff changeset
271 :param width: the maximum line width for the generated output; use `None`,
4fad20ab7cca 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: 23
diff changeset
272 0, or a negative number to completely disable line wrapping
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
273 :param charset: the encoding
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
274 :param no_location: do not emit a location comment for every message
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
275 :param omit_header: do not include the ``msgid ""`` entry at the top of the
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
276 output
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
277 :param copyright_holder: sets the copyright holder in the output
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
278 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
279 def _normalize(key):
24
4fad20ab7cca 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: 23
diff changeset
280 return normalize(key, width=width).encode(charset, 'backslashreplace')
4fad20ab7cca 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: 23
diff changeset
281
4fad20ab7cca 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: 23
diff changeset
282 def _write(text):
4fad20ab7cca 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: 23
diff changeset
283 if isinstance(text, unicode):
4fad20ab7cca 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: 23
diff changeset
284 text = text.encode(charset)
4fad20ab7cca 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: 23
diff changeset
285 fileobj.write(text)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
286
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
287 catalog.project = project
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
288 catalog.version = version
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
289 catalog.charset = charset
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
290
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
291 if sort_output:
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
292 messages = list(catalog)
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
293 messages.sort(lambda x,y: cmp(x.id, y.id))
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
294 elif sort_by_file:
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
295 messages = list(catalog)
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
296 messages.sort(lambda x,y: cmp(x.locations, y.locations))
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
297 else:
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
298 messages = catalog
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
299
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
300 _copyright_holder = copyright_holder or 'ORGANIZATION'
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
301
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
302 for message in messages:
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
303 if not message.id: # This is the header "message"
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
304 if omit_header:
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
305 continue
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
306 _write(POT_HEADER % {
96
11e90b91a4f0 Also use tzinfo for `write_po`.
palgarvio
parents: 94
diff changeset
307 'year': date.today().strftime('%Y'),
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
308 'project': project,
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
309 'copyright_holder': _copyright_holder,
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
310 })
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
311
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
312 if message.comments:
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
313 for comment in message.comments:
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
314 for line in textwrap.wrap(comment,
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
315 width, break_long_words=False):
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
316 _write('#. %s\n' % line.strip())
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
317
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
318 if not no_location:
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
319 locs = u' '.join([u'%s:%d' % item for item in message.locations])
24
4fad20ab7cca 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: 23
diff changeset
320 if width and width > 0:
4fad20ab7cca 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: 23
diff changeset
321 locs = textwrap.wrap(locs, width, break_long_words=False)
4fad20ab7cca 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: 23
diff changeset
322 for line in locs:
4fad20ab7cca 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: 23
diff changeset
323 _write('#: %s\n' % line.strip())
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
324 if message.flags:
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
325 _write('#%s\n' % ', '.join([''] + list(message.flags)))
24
4fad20ab7cca 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: 23
diff changeset
326
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
327 if isinstance(message.id, (list, tuple)):
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
328 _write('msgid %s\n' % _normalize(message.id[0]))
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
329 _write('msgid_plural %s\n' % _normalize(message.id[1]))
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
330 for i, string in enumerate(message.string):
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
331 _write('msgstr[%d] %s\n' % (i, _normalize(message.string[i])))
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
332 else:
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
333 _write('msgid %s\n' % _normalize(message.id))
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
334 _write('msgstr %s\n' % _normalize(message.string or ''))
24
4fad20ab7cca 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: 23
diff changeset
335 _write('\n')
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
336
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
337 def write_po(fileobj, input_fileobj, locale_obj, project='PROJECT',
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
338 version='VERSION', first_author=None, first_author_email=None,
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
339 plurals=('INTEGER', 'EXPRESSION')):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
340 r"""Write a ``gettext`` PO (portable object) file to the given file-like
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
341 object, from the given input PO template file.
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
342
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
343 >>> from StringIO import StringIO
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
344 >>> from babel import Locale
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
345 >>> locale_obj = Locale.parse('pt_PT')
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
346 >>> inbuf = StringIO(r'''# Translations template for FooBar.
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
347 ... # Copyright (C) 2007 ORGANIZATION
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
348 ... # This file is distributed under the same license as the
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
349 ... # FooBar project.
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
350 ... # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
351 ... #
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
352 ... #, fuzzy
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
353 ... msgid ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
354 ... msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
355 ... "Project-Id-Version: FooBar 0.1\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
356 ... "POT-Creation-Date: 2007-06-07 22:54+0100\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
357 ... "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
358 ... "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
359 ... "Language-Team: LANGUAGE <LL@li.org>\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
360 ... "MIME-Version: 1.0\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
361 ... "Content-Type: text/plain; charset=utf-8\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
362 ... "Content-Transfer-Encoding: 8bit\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
363 ... "Generated-By: Babel 0.1dev-r50\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
364 ...
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
365 ... #: base.py:83 templates/index.html:9
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
366 ... #: templates/index2.html:9
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
367 ... msgid "Home"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
368 ... msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
369 ...
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
370 ... #: base.py:84 templates/index.html:9
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
371 ... msgid "Accounts"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
372 ... msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
373 ... ''')
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
374 >>> outbuf = StringIO()
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
375 >>> write_po(outbuf, inbuf, locale_obj, project='FooBar',
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
376 ... version='0.1', first_author='A Name',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
377 ... first_author_email='user@domain.tld',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
378 ... plurals=(2, '(n != 1)'))
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
379 >>> print outbuf.getvalue() # doctest: +ELLIPSIS
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
380 # Portuguese (Portugal) Translations for FooBar
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
381 # Copyright (C) 2007 ORGANIZATION
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
382 # This file is distributed under the same license as the
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
383 # FooBar project.
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
384 # A Name <user@domain.tld>, ...
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
385 #
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
386 #, fuzzy
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
387 msgid ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
388 msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
389 "Project-Id-Version: FooBar 0.1\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
390 "POT-Creation-Date: 2007-06-07 22:54+0100\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
391 "PO-Revision-Date: ...\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
392 "Last-Translator: A Name <user@domain.tld>\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
393 "Language-Team: LANGUAGE <LL@li.org>\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
394 "MIME-Version: 1.0\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
395 "Content-Type: text/plain; charset=utf-8\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
396 "Content-Transfer-Encoding: 8bit\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
397 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
398 "Generated-By: Babel ...\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
399 <BLANKLINE>
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
400 #: base.py:83 templates/index.html:9
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
401 #: templates/index2.html:9
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
402 msgid "Home"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
403 msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
404 <BLANKLINE>
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
405 #: base.py:84 templates/index.html:9
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
406 msgid "Accounts"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
407 msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
408 <BLANKLINE>
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
409 >>>
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
410 """
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
411
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
412 _first_author = ''
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
413 if first_author:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
414 _first_author += first_author
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
415 if first_author_email:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
416 _first_author += ' <%s>' % first_author_email
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
417
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
418 inlines = input_fileobj.readlines()
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
419 outlines = []
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
420 in_header = True
96
11e90b91a4f0 Also use tzinfo for `write_po`.
palgarvio
parents: 94
diff changeset
421 _date = datetime.now(LOCAL)
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
422 for index in range(len(inlines)):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
423 if in_header:
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
424 if '# Translations template' in inlines[index]:
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
425 outlines.append('# %s Translations for %s\n' % \
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
426 (locale_obj.english_name, project))
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
427 elif '# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.' in inlines[index]:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
428 if _first_author:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
429 outlines.append(
96
11e90b91a4f0 Also use tzinfo for `write_po`.
palgarvio
parents: 94
diff changeset
430 '# %s, %s\n' % (_first_author, _date.strftime('%Y'))
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
431 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
432 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
433 outlines.append(inlines[index])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
434 elif '"PO-Revision-Date:' in inlines[index]:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
435 outlines.append(
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
436 '"PO-Revision-Date: %s\\n"\n' % \
96
11e90b91a4f0 Also use tzinfo for `write_po`.
palgarvio
parents: 94
diff changeset
437 _date.strftime('%Y-%m-%d %H:%M%z')
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
438 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
439 elif '"Last-Translator:' in inlines[index]:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
440 if _first_author:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
441 outlines.append(
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
442 '"Last-Translator: %s\\n"\n' % _first_author
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
443 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
444 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
445 outlines.append(inlines[index])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
446 elif '"Content-Transfer-Encoding:' in inlines[index]:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
447 outlines.append(inlines[index])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
448 if '"Plural-Forms:' not in inlines[index+1]:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
449 outlines.append(
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
450 '"Plural-Forms: nplurals=%s; plural=%s;\\n"\n' % plurals
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
451 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
452 elif inlines[index].endswith('\\n"\n') and \
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
453 inlines[index+1] == '\n':
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
454 in_header = False
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
455 outlines.append(inlines[index])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
456 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
457 outlines.append(inlines[index])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
458 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
459 outlines.extend(inlines[index:])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
460 break
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
461 fileobj.writelines(outlines)
Copyright (C) 2012-2017 Edgewall Software