Mercurial > babel > old > babel-test
annotate babel/messages/pofile.py @ 54:b3395b285104
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
author | cmlenz |
---|---|
date | Fri, 08 Jun 2007 09:16:32 +0000 |
parents | babel/catalog/pofile.py@7f61453c1bea |
children | c3291ad6b010 |
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 | |
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 | 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 |
5
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
28 import time |
1 | 29 |
30 from babel import __version__ as VERSION | |
31 | |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
32 __all__ = ['escape', 'normalize', 'read_po', 'write_po', 'write_pot'] |
1 | 33 |
34 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
|
35 """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
|
36 file-like object. |
1 | 37 |
38 This function yields tuples of the form: | |
39 | |
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
|
40 ``(message, translation, locations, flags)`` |
1 | 41 |
42 where: | |
43 | |
44 * ``message`` is the original (untranslated) message, or a | |
45 ``(singular, plural)`` tuple for pluralizable messages | |
46 * ``translation`` is the translation of the message, or a tuple of | |
47 translations for pluralizable messages | |
48 * ``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
|
49 * ``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
|
50 |
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 >>> 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
|
52 >>> 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
|
53 ... #: 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
|
54 ... #, 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
|
55 ... 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
|
56 ... 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
|
57 ... |
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
|
58 ... #: 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
|
59 ... 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
|
60 ... 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
|
61 ... 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
|
62 ... 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
|
63 ... ''') |
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
|
64 >>> 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
|
65 ... 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
|
66 ... 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
|
67 (('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
|
68 ((('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
|
69 (('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
|
70 ((('main.py', 3),), set([])) |
1 | 71 |
72 :param fileobj: the file-like object to read the PO file from | |
73 :return: an iterator over ``(message, translation, location)`` tuples | |
74 :rtype: ``iterator`` | |
75 """ | |
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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 |
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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 |
1 | 92 for line in fileobj.readlines(): |
93 line = line.strip() | |
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 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
|
104 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
|
105 elif line: |
1 | 106 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
|
107 in_msgid = True |
1 | 108 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
|
109 messages.append(msg[1:-1]) |
1 | 110 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
|
111 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
|
112 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
|
113 yield pack() |
1 | 114 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
|
115 messages.append(msg[1:-1]) |
1 | 116 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
|
117 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
|
118 in_msgstr = True |
1 | 119 msg = line[6:].lstrip() |
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 |
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 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
|
132 yield pack() |
1 | 133 |
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
|
134 POT_HEADER = """\ |
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
|
135 # Translations Template for %%(project)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
|
136 # Copyright (C) %%(year)s ORGANIZATION |
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
|
137 # This file is distributed under the same license as the |
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
|
138 # %%(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
|
139 # 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
|
140 # |
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
|
141 #, fuzzy |
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
|
142 msgid "" |
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
|
143 msgstr "" |
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
|
144 "Project-Id-Version: %%(project)s %%(version)s\\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
|
145 "POT-Creation-Date: %%(creation_date)s\\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
|
146 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\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
|
147 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\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
|
148 "Language-Team: LANGUAGE <LL@li.org>\\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
|
149 "MIME-Version: 1.0\\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
|
150 "Content-Type: text/plain; charset=%%(charset)s\\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
|
151 "Content-Transfer-Encoding: 8bit\\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
|
152 "Generated-By: Babel %s\\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
|
153 |
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 """ % VERSION |
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 |
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 PYTHON_FORMAT = re.compile(r'\%(\([\w]+\))?[diouxXeEfFgGcrs]').search |
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 |
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 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
|
159 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
|
160 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
|
161 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
|
162 ')') |
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 |
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 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
|
165 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
|
166 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
|
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 >>> 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
|
169 ... "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
|
170 ... ''') |
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 '"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
|
172 |
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 :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
|
174 :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
|
175 :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
|
176 """ |
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 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
|
178 .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
|
179 .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
|
180 .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
|
181 .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
|
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 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
|
184 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
|
185 |
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 >>> 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
|
187 ... "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
|
188 ... ''', 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
|
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 "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
|
191 " \"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
|
192 |
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 >>> 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
|
194 ... "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
|
195 ... ''', 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
|
196 "" |
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 "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
|
198 " \"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
|
199 "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
|
200 " 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
|
201 |
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 :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
|
203 :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
|
204 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
|
205 :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
|
206 :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
|
207 """ |
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 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
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 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 # 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
|
225 # 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
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 if len(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
|
235 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
|
236 |
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 # Remove empty trailing 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
|
238 if not 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
|
239 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
|
240 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
|
241 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
|
242 |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
243 def write_pot(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
|
244 charset='utf-8', no_location=False, omit_header=False): |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
245 r"""Write a ``gettext`` PO (portable object) template file to the given |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
246 file-like object. |
1 | 247 |
248 The `messages` parameter is expected to be an iterable object producing | |
249 tuples of the form: | |
250 | |
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
|
251 ``(filename, lineno, funcname, message, flags)`` |
1 | 252 |
253 >>> from StringIO import StringIO | |
254 >>> buf = StringIO() | |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
255 >>> write_pot(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
|
256 ... ('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
|
257 ... ('main.py', 3, 'ngettext', (u'bar', u'baz'), None) |
1 | 258 ... ], omit_header=True) |
259 | |
260 >>> print buf.getvalue() | |
261 #: 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
|
262 #, 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
|
263 msgid "foo %(name)s" |
1 | 264 msgstr "" |
265 <BLANKLINE> | |
266 #: main.py:3 | |
267 msgid "bar" | |
268 msgid_plural "baz" | |
269 msgstr[0] "" | |
270 msgstr[1] "" | |
271 <BLANKLINE> | |
272 <BLANKLINE> | |
273 | |
274 :param fileobj: the file-like object to write to | |
275 :param messages: an iterable over the messages | |
276 :param project: the project name | |
277 :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
|
278 :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
|
279 0, or a negative number to completely disable line wrapping |
1 | 280 :param charset: the encoding |
281 :param no_location: do not emit a location comment for every message | |
282 :param omit_header: do not include the ``msgid ""`` entry at the top of the | |
283 output | |
284 """ | |
285 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
|
286 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
|
287 |
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
|
288 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
|
289 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
|
290 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
|
291 fileobj.write(text) |
1 | 292 |
293 if not omit_header: | |
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
|
294 _write(POT_HEADER % { |
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
|
295 'year': time.strftime('%Y'), |
5
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
296 'project': project, |
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
297 'version': version, |
50ad95bee876
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
298 'creation_date': time.strftime('%Y-%m-%d %H:%M%z'), |
1 | 299 'charset': charset, |
300 }) | |
301 | |
302 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
|
303 msgflags = {} |
1 | 304 msgids = [] |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
305 plurals = {} |
1 | 306 |
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
|
307 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
|
308 flags = set(flags or []) |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
309 if isinstance(key, (list, tuple)): |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
310 assert len(key) == 2 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
311 plurals[key[0]] = key[1] |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
312 key = key[0] |
1 | 313 if key in msgids: |
314 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
|
315 msgflags[key] |= flags |
1 | 316 else: |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
317 if PYTHON_FORMAT(key): |
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
|
318 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
|
319 else: |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
320 flags.discard('python-format') |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
321 |
1 | 322 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
|
323 msgflags[key] = flags |
1 | 324 msgids.append(key) |
325 | |
326 for msgid in msgids: | |
327 if not no_location: | |
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
|
328 locs = u' '.join([u'%s:%d' % item for item in locations[msgid]]) |
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
|
329 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
|
330 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
|
331 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
|
332 _write('#: %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
|
333 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
|
334 if 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
|
335 _write('#%s\n' % ', '.join([''] + list(flags))) |
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
|
336 |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
337 if plurals.has_key(msgid): |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
338 _write('msgid %s\n' % _normalize(msgid)) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
339 _write('msgid_plural %s\n' % _normalize(plurals[msgid])) |
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
|
340 _write('msgstr[0] ""\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
|
341 _write('msgstr[1] ""\n') |
1 | 342 else: |
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
|
343 _write('msgid %s\n' % _normalize(msgid)) |
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
|
344 _write('msgstr ""\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
|
345 _write('\n') |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
346 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
347 def write_po(fileobj, input_fileobj, language, country=None, project='PROJECT', |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
348 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
|
349 plurals=('INTEGER', 'EXPRESSION')): |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
350 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
|
351 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
|
352 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
353 >>> from StringIO import StringIO |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
354 >>> inbuf = StringIO(r'''# Translations Template for FooBar. |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
355 ... # Copyright (C) 2007 ORGANIZATION |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
356 ... # 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
|
357 ... # FooBar project. |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
358 ... # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
359 ... # |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
360 ... #, fuzzy |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
361 ... msgid "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
362 ... msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
363 ... "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
|
364 ... "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
|
365 ... "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
|
366 ... "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
|
367 ... "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
|
368 ... "MIME-Version: 1.0\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
369 ... "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
|
370 ... "Content-Transfer-Encoding: 8bit\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
371 ... "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
|
372 ... |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
373 ... #: 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
|
374 ... #: templates/index2.html:9 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
375 ... msgid "Home" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
376 ... msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
377 ... |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
378 ... #: 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
|
379 ... msgid "Accounts" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
380 ... msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
381 ... ''') |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
382 >>> outbuf = StringIO() |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
383 >>> write_po(outbuf, inbuf, 'English', project='FooBar', |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
384 ... 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
|
385 ... 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
|
386 ... plurals=(2, '(n != 1)')) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
387 >>> print outbuf.getvalue() # doctest: +ELLIPSIS |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
388 # English Translations for FooBar |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
389 # Copyright (C) 2007 ORGANIZATION |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
390 # 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
|
391 # FooBar project. |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
392 # A Name <user@domain.tld>, ... |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
393 # |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
394 #, fuzzy |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
395 msgid "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
396 msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
397 "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
|
398 "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
|
399 "PO-Revision-Date: ...\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
400 "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
|
401 "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
|
402 "MIME-Version: 1.0\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
403 "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
|
404 "Content-Transfer-Encoding: 8bit\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
405 "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
|
406 "Generated-By: Babel ...\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
407 <BLANKLINE> |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
408 #: 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
|
409 #: templates/index2.html:9 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
410 msgid "Home" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
411 msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
412 <BLANKLINE> |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
413 #: 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
|
414 msgid "Accounts" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
415 msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
416 <BLANKLINE> |
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 """ |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
419 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
420 _first_author = '' |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
421 if first_author: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
422 _first_author += first_author |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
423 if first_author_email: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
424 _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
|
425 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
426 inlines = input_fileobj.readlines() |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
427 outlines = [] |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
428 in_header = True |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
429 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
|
430 if in_header: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
431 if '# Translations Template' in inlines[index]: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
432 if country: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
433 line = '# %s (%s) Translations for %%s\n' % \ |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
434 (language, country) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
435 else: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
436 line = '# %s Translations for %%s\n' % language |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
437 outlines.append(line % project) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
438 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
|
439 if _first_author: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
440 outlines.append( |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
441 '# %s, %s\n' % (_first_author, time.strftime('%Y')) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
442 ) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
443 else: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
444 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
445 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
|
446 outlines.append( |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
447 '"PO-Revision-Date: %s\\n"\n' % \ |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
448 time.strftime('%Y-%m-%d %H:%M%z') |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
449 ) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
450 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
|
451 if _first_author: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
452 outlines.append( |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
453 '"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
|
454 ) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
455 else: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
456 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
457 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
|
458 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
459 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
|
460 outlines.append( |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
461 '"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
|
462 ) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
463 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
|
464 inlines[index+1] == '\n': |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
465 in_header = False |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
466 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
467 else: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
468 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
469 else: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
470 outlines.extend(inlines[index:]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
471 break |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
472 fileobj.writelines(outlines) |