Mercurial > babel > old > babel-test
annotate babel/catalog/pofile.py @ 23:bd45ff425485
Added line-wrap support for `write_po`.
Fixed bug on the distutils frontend regarding the `keywords`.
author | palgarvio |
---|---|
date | Thu, 31 May 2007 22:44:57 +0000 |
parents | ddfac856c34f |
children | 4fad20ab7cca |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2007 Edgewall Software | |
4 # All rights reserved. | |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
14 """Reading and writing of files in the ``gettext`` PO (portable object) | |
15 format. | |
16 | |
17 :see: `The Format of PO Files | |
18 <http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files>`_ | |
19 """ | |
20 | |
21 # TODO: line wrapping | |
23 | 22 from textwrap import wrap |
5
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
23 from datetime import date, datetime |
1 | 24 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
|
25 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
|
26 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
|
27 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
|
28 from sets import Set as set |
5
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
29 import time |
1 | 30 |
31 from babel import __version__ as VERSION | |
32 | |
33 __all__ = ['escape', 'normalize', 'read_po', 'write_po'] | |
34 | |
35 POT_HEADER = """\ | |
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
|
36 # Translations Template for %%(project)s. |
1 | 37 # Copyright (C) YEAR ORGANIZATION |
38 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. | |
39 # | |
40 msgid "" | |
41 msgstr "" | |
42 "Project-Id-Version: %%(project)s %%(version)s\\n" | |
5
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
43 "POT-Creation-Date: %%(creation_date)s\\n" |
1 | 44 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n" |
45 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n" | |
46 "Language-Team: LANGUAGE <LL@li.org>\\n" | |
47 "MIME-Version: 1.0\\n" | |
48 "Content-Type: text/plain; charset=%%(charset)s\\n" | |
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
|
49 "Content-Transfer-Encoding: 8bit\\n" |
1 | 50 "Generated-By: Babel %s\\n" |
51 | |
52 """ % VERSION | |
53 | |
17
6aa4c4df8871
Recognize python-format messages also for unnamed parameters.
cmlenz
parents:
6
diff
changeset
|
54 PYTHON_FORMAT = re.compile(r'\%(\([\w]+\))?[diouxXeEfFgGcrs]').search |
1 | 55 |
56 def escape(string): | |
57 r"""Escape the given string so that it can be included in double-quoted | |
58 strings in ``PO`` files. | |
59 | |
60 >>> escape('''Say: | |
61 ... "hello, world!" | |
62 ... ''') | |
63 'Say:\\n \\"hello, world!\\"\\n' | |
64 | |
65 :param string: the string to escape | |
66 :return: the escaped string | |
67 :rtype: `str` or `unicode` | |
68 """ | |
69 return string.replace('\\', '\\\\') \ | |
70 .replace('\t', '\\t') \ | |
71 .replace('\r', '\\r') \ | |
72 .replace('\n', '\\n') \ | |
73 .replace('\"', '\\"') | |
74 | |
75 def normalize(string, charset='utf-8'): | |
76 """This converts a string into a format that is appropriate for .po files, | |
77 namely much closer to C style. | |
78 | |
79 :param string: the string to normalize | |
80 :param charset: the encoding to use for `unicode` strings | |
81 :return: the normalized string | |
82 :rtype: `str` | |
83 """ | |
84 string = string.encode(charset, 'backslashreplace') | |
85 lines = string.split('\n') | |
86 if len(lines) == 1: | |
87 string = '"' + escape(string) + '"' | |
88 else: | |
89 if not lines[-1]: | |
90 del lines[-1] | |
91 lines[-1] = lines[-1] + '\n' | |
92 for i in range(len(lines)): | |
93 lines[i] = escape(lines[i]) | |
94 lineterm = '\\n"\n"' | |
95 string = '""\n"' + lineterm.join(lines) + '"' | |
96 return string | |
97 | |
98 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
|
99 """Read messages from a ``gettext`` PO (portable object) file from the given |
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 file-like object. |
1 | 101 |
102 This function yields tuples of the form: | |
103 | |
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
|
104 ``(message, translation, locations, flags)`` |
1 | 105 |
106 where: | |
107 | |
108 * ``message`` is the original (untranslated) message, or a | |
109 ``(singular, plural)`` tuple for pluralizable messages | |
110 * ``translation`` is the translation of the message, or a tuple of | |
111 translations for pluralizable messages | |
112 * ``locations`` is a sequence of ``(filename, lineno)`` tuples | |
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
|
113 * ``flags`` is a set of strings (for exampe, "fuzzy") |
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
|
114 |
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
|
115 >>> 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
|
116 >>> 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
|
117 ... #: 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
|
118 ... #, 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
|
119 ... 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
|
120 ... 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
|
121 ... |
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 ... #: 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
|
123 ... 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
|
124 ... 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
|
125 ... 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
|
126 ... 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
|
127 ... ''') |
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 >>> for message, translation, locations, flags in read_po(buf): |
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 ... print (message, translation) |
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
|
130 ... print ' ', (locations, flags) |
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
|
131 (('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
|
132 ((('main.py', 1),), set(['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
|
133 (('bar', '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
|
134 ((('main.py', 3),), set([])) |
1 | 135 |
136 :param fileobj: the file-like object to read the PO file from | |
137 :return: an iterator over ``(message, translation, location)`` tuples | |
138 :rtype: ``iterator`` | |
139 """ | |
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
|
140 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
|
141 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
|
142 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
|
143 flags = [] |
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
|
144 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
|
145 |
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
|
146 def pack(): |
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
|
147 translations.sort() |
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
|
148 retval = (tuple(messages), tuple([t[1] for t in 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
|
149 tuple(locations), set(flags)) |
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
|
150 del 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
|
151 del 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
|
152 del 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
|
153 del flags[:] |
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
|
154 return retval |
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
|
155 |
1 | 156 for line in fileobj.readlines(): |
157 line = line.strip() | |
158 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
|
159 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
|
160 if 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
|
161 yield pack() |
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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 flags.append(flag.strip()) |
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
|
169 elif line: |
1 | 170 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
|
171 in_msgid = True |
1 | 172 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
|
173 messages.append(msg[1:-1]) |
1 | 174 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
|
175 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
|
176 if 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
|
177 yield pack() |
1 | 178 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
|
179 messages.append(msg[1:-1]) |
1 | 180 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
|
181 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
|
182 in_msgstr = True |
1 | 183 msg = line[6:].lstrip() |
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 |
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
|
195 if 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
|
196 yield pack() |
1 | 197 |
23 | 198 def write_po(fileobj, messages, project='PROJECT', version='VERSION', width=76, |
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
|
199 charset='utf-8', no_location=False, omit_header=False): |
1 | 200 r"""Write a ``gettext`` PO (portable object) file to the given file-like |
201 object. | |
202 | |
203 The `messages` parameter is expected to be an iterable object producing | |
204 tuples of the form: | |
205 | |
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
|
206 ``(filename, lineno, funcname, message, flags)`` |
1 | 207 |
208 >>> from StringIO import StringIO | |
209 >>> buf = StringIO() | |
210 >>> write_po(buf, [ | |
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
|
211 ... ('main.py', 1, None, u'foo %(name)s', ('fuzzy',)), |
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
|
212 ... ('main.py', 3, 'ngettext', (u'bar', u'baz'), None) |
1 | 213 ... ], omit_header=True) |
214 | |
215 >>> print buf.getvalue() | |
216 #: 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
|
217 #, 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
|
218 msgid "foo %(name)s" |
1 | 219 msgstr "" |
220 <BLANKLINE> | |
221 #: main.py:3 | |
222 msgid "bar" | |
223 msgid_plural "baz" | |
224 msgstr[0] "" | |
225 msgstr[1] "" | |
226 <BLANKLINE> | |
227 <BLANKLINE> | |
228 | |
229 :param fileobj: the file-like object to write to | |
230 :param messages: an iterable over the messages | |
231 :param project: the project name | |
232 :param version: the project version | |
233 :param charset: the encoding | |
234 :param no_location: do not emit a location comment for every message | |
235 :param omit_header: do not include the ``msgid ""`` entry at the top of the | |
236 output | |
237 """ | |
238 def _normalize(key): | |
239 return normalize(key, charset=charset) | |
240 | |
241 if not omit_header: | |
242 fileobj.write(POT_HEADER % { | |
5
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
243 'project': project, |
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
244 'version': version, |
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
245 'creation_date': time.strftime('%Y-%m-%d %H:%M%z'), |
1 | 246 'charset': charset, |
247 }) | |
248 | |
249 locations = {} | |
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
|
250 msgflags = {} |
1 | 251 msgids = [] |
252 | |
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
|
253 for filename, lineno, funcname, key, flags in 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
|
254 flags = set(flags or []) |
1 | 255 if key in msgids: |
256 locations[key].append((filename, lineno)) | |
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
|
257 msgflags[key] |= flags |
1 | 258 else: |
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
|
259 if (isinstance(key, (list, tuple)) and |
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
|
260 filter(None, [PYTHON_FORMAT(k) for k in key])) or \ |
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
|
261 (isinstance(key, basestring) and PYTHON_FORMAT(key)): |
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
|
262 flags.add('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
|
263 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
|
264 flags.discard('python-format') |
1 | 265 locations[key] = [(filename, lineno)] |
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
|
266 msgflags[key] = flags |
1 | 267 msgids.append(key) |
268 | |
269 for msgid in msgids: | |
270 if not no_location: | |
23 | 271 locs = [ |
272 u' %s:%s' % (fname, lineno) for | |
273 fname, lineno in locations[msgid] | |
274 ] | |
275 if width > 0: | |
276 wrapped = wrap(u''.join(locs), width, break_long_words=False) | |
277 else: | |
278 wrapped = locs | |
279 for line in wrapped: | |
280 fileobj.write(u'#: %s\n' % line.strip()) | |
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
|
281 flags = msgflags[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
|
282 if flags: |
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
|
283 fileobj.write('#%s\n' % ', '.join([''] + list(flags))) |
1 | 284 if type(msgid) is tuple: |
285 assert len(msgid) == 2 | |
23 | 286 if width > 0: |
287 wrapped = wrap(msgid[0], width, break_long_words=False) | |
288 else: | |
289 wrapped = [msgid[0]] | |
290 if len(wrapped) == 1: | |
291 fileobj.write('msgid ') | |
292 else: | |
293 fileobj.write('msgid ""\n') | |
294 for line in wrapped: | |
295 fileobj.write('%s\n' % normalize(line, charset)) | |
296 if width > 0: | |
297 wrapped = wrap(msgid[1], width, break_long_words=False) | |
298 else: | |
299 wrapped = [msgid[1]] | |
300 if len(wrapped) == 1: | |
301 fileobj.write('msgid_plural ') | |
302 else: | |
303 fileobj.write('msgid_plural ""\n') | |
304 for line in wrapped: | |
305 fileobj.write('%s\n' % normalize(line, charset)) | |
1 | 306 fileobj.write('msgstr[0] ""\n') |
307 fileobj.write('msgstr[1] ""\n') | |
308 else: | |
23 | 309 if width > 0: |
310 wrapped = wrap(msgid, width, break_long_words=False) | |
311 else: | |
312 wrapped = [msgid] | |
313 if len(wrapped) == 1: | |
314 fileobj.write('msgid ') | |
315 else: | |
316 fileobj.write('msgid ""\n') | |
317 for line in wrapped: | |
318 fileobj.write('%s\n' % normalize(line, charset)) | |
1 | 319 fileobj.write('msgstr ""\n') |
320 fileobj.write('\n') |