Mercurial > babel > old > babel-test
annotate babel/messages/pofile.py @ 68:7e64668126d9
Add back POT header broken in previous check-in.
author | cmlenz |
---|---|
date | Fri, 08 Jun 2007 15:47:00 +0000 |
parents | 5496b9127a07 |
children | ea4cb904df8f |
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 | |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
31 from babel.messages.catalog import Catalog |
1 | 32 |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
33 __all__ = ['escape', 'normalize', 'read_po', 'write_po', 'write_pot'] |
1 | 34 |
35 def read_po(fileobj): | |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
36 """Read messages from a ``gettext`` PO (portable object) file from the given |
64 | 37 file-like object and return a `Catalog`. |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
38 |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
39 >>> from StringIO import StringIO |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
40 >>> buf = StringIO(''' |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
41 ... #: main.py:1 |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
42 ... #, fuzzy, python-format |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
43 ... msgid "foo %(name)s" |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
44 ... msgstr "" |
21
ddfac856c34f
Change pot header's first line, "Translations Template for %%(project)s." instead of "SOME DESCRIPTIVE TITLE.". '''`project`''' and '''`version`''' now default to '''PROJECT''' and '''VERSION''' respectively. Fixed a bug regarding '''Content-Transfer-Encoding''', it shouldn't be the charset, and we're defaulting to `8bit` untill someone complains.
palgarvio
parents:
17
diff
changeset
|
45 ... |
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
|
46 ... #: 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
|
47 ... 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
|
48 ... 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
|
49 ... 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
|
50 ... 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
|
51 ... ''') |
64 | 52 >>> catalog = read_po(buf) |
53 >>> for message in catalog: | |
67 | 54 ... if message.id: |
55 ... print (message.id, message.string) | |
56 ... print ' ', (message.locations, message.flags) | |
64 | 57 ('foo %(name)s', '') |
58 ([('main.py', 1)], set(['fuzzy', 'python-format'])) | |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
59 (('bar', 'baz'), ('', '')) |
64 | 60 ([('main.py', 3)], set([])) |
1 | 61 |
62 :param fileobj: the file-like object to read the PO file from | |
63 :return: an iterator over ``(message, translation, location)`` tuples | |
64 :rtype: ``iterator`` | |
65 """ | |
64 | 66 catalog = Catalog() |
67 | |
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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 |
64 | 74 def _add_message(): |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
75 translations.sort() |
64 | 76 if len(messages) > 1: |
77 msgid = tuple(messages) | |
78 else: | |
79 msgid = messages[0] | |
80 if len(translations) > 1: | |
81 string = tuple([t[1] for t in translations]) | |
82 else: | |
83 string = translations[0][1] | |
84 catalog.add(msgid, string, list(locations), set(flags)) | |
85 del messages[:]; del translations[:]; del locations[:]; del flags[:] | |
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
|
86 |
1 | 87 for line in fileobj.readlines(): |
88 line = line.strip() | |
89 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
|
90 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
|
91 if messages: |
64 | 92 _add_message() |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 elif line: |
1 | 101 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
|
102 in_msgid = True |
1 | 103 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
|
104 messages.append(msg[1:-1]) |
1 | 105 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
|
106 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
|
107 if messages: |
64 | 108 _add_message() |
1 | 109 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
|
110 messages.append(msg[1:-1]) |
1 | 111 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
|
112 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
|
113 in_msgstr = True |
1 | 114 msg = line[6:].lstrip() |
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 |
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 messages: |
64 | 127 _add_message() |
128 return catalog | |
1 | 129 |
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
|
130 POT_HEADER = """\ |
68 | 131 # Translations template for %(project)s. |
132 # Copyright (C) %(year)s ORGANIZATION | |
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
|
133 # This file is distributed under the same license as the |
68 | 134 # %(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
|
135 # 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
|
136 # |
68 | 137 """ |
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
|
138 |
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 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
|
140 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
|
141 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
|
142 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
|
143 ')') |
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 |
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 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
|
146 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
|
147 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
|
148 |
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 >>> 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
|
150 ... "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
|
151 ... ''') |
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 '"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
|
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 :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
|
155 :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
|
156 :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
|
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 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
|
159 .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
|
160 .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
|
161 .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
|
162 .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
|
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 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
|
165 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
|
166 |
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 >>> 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
|
168 ... "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
|
169 ... ''', 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
|
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" |
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 " \"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
|
173 |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
174 >>> 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
|
175 ... "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
|
176 ... ''', 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
|
177 "" |
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 "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
|
179 " \"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
|
180 "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
|
181 " 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
|
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 :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
|
184 :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
|
185 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
|
186 :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
|
187 :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
|
188 """ |
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 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 # 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
|
206 # 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 |
67 | 215 if len(lines) <= 1: |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
216 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
|
217 |
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 # Remove empty trailing line |
67 | 219 if lines and not lines[-1]: |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
220 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
|
221 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
|
222 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
|
223 |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
224 def write_pot(fileobj, catalog, 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
|
225 charset='utf-8', no_location=False, omit_header=False): |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
226 r"""Write a ``gettext`` PO (portable object) template file for a given |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
227 message catalog to the provided file-like object. |
1 | 228 |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
229 >>> catalog = Catalog() |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
230 >>> catalog.add(u'foo %(name)s', locations=[('main.py', 1)], |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
231 ... flags=('fuzzy',)) |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
232 >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)]) |
1 | 233 >>> from StringIO import StringIO |
234 >>> buf = StringIO() | |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
235 >>> write_pot(buf, catalog, omit_header=True) |
1 | 236 |
237 >>> print buf.getvalue() | |
238 #: 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
|
239 #, 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
|
240 msgid "foo %(name)s" |
1 | 241 msgstr "" |
242 <BLANKLINE> | |
243 #: main.py:3 | |
244 msgid "bar" | |
245 msgid_plural "baz" | |
246 msgstr[0] "" | |
247 msgstr[1] "" | |
248 <BLANKLINE> | |
249 <BLANKLINE> | |
250 | |
251 :param fileobj: the file-like object to write to | |
67 | 252 :param catalog: the `Catalog` instance |
1 | 253 :param project: the project name |
254 :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
|
255 :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
|
256 0, or a negative number to completely disable line wrapping |
1 | 257 :param charset: the encoding |
258 :param no_location: do not emit a location comment for every message | |
259 :param omit_header: do not include the ``msgid ""`` entry at the top of the | |
260 output | |
261 """ | |
262 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
|
263 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
|
264 |
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
|
265 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
|
266 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
|
267 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
|
268 fileobj.write(text) |
1 | 269 |
68 | 270 catalog.project = project |
271 catalog.version = version | |
272 catalog.charset = charset | |
273 | |
67 | 274 for message in catalog: |
275 if not message.id: # This is the header "message" | |
276 if omit_header: | |
277 continue | |
278 _write(POT_HEADER % { | |
279 'year': time.strftime('%Y'), | |
280 'project': project, | |
281 }) | |
1 | 282 |
283 if not no_location: | |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
284 locs = u' '.join([u'%s:%d' % item for item in message.locations]) |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
285 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
|
286 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
|
287 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
|
288 _write('#: %s\n' % line.strip()) |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
289 if message.flags: |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
290 _write('#%s\n' % ', '.join([''] + list(message.flags))) |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
291 |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
292 if isinstance(message.id, (list, tuple)): |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
293 _write('msgid %s\n' % _normalize(message.id[0])) |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
294 _write('msgid_plural %s\n' % _normalize(message.id[1])) |
68 | 295 for i, string in enumerate(message.string): |
296 _write('msgstr[%d] %s\n' % (i, _normalize(message.string[i]))) | |
1 | 297 else: |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
298 _write('msgid %s\n' % _normalize(message.id)) |
68 | 299 _write('msgstr %s\n' % _normalize(message.string or '')) |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
300 _write('\n') |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
301 |
55
c3291ad6b010
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
302 def write_po(fileobj, input_fileobj, locale_obj, project='PROJECT', |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
303 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
|
304 plurals=('INTEGER', 'EXPRESSION')): |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
305 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
|
306 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
|
307 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
308 >>> from StringIO import StringIO |
55
c3291ad6b010
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
309 >>> from babel import Locale |
c3291ad6b010
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
310 >>> locale_obj = Locale.parse('pt_PT') |
68 | 311 >>> inbuf = StringIO(r'''# Translations template for FooBar. |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
312 ... # Copyright (C) 2007 ORGANIZATION |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
313 ... # 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
|
314 ... # FooBar project. |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
315 ... # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
316 ... # |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
317 ... #, fuzzy |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
318 ... msgid "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
319 ... msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
320 ... "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
|
321 ... "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
|
322 ... "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
|
323 ... "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
|
324 ... "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
|
325 ... "MIME-Version: 1.0\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
326 ... "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
|
327 ... "Content-Transfer-Encoding: 8bit\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
328 ... "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
|
329 ... |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
330 ... #: 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
|
331 ... #: templates/index2.html:9 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
332 ... msgid "Home" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
333 ... msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
334 ... |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
335 ... #: 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
|
336 ... msgid "Accounts" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
337 ... msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
338 ... ''') |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
339 >>> outbuf = StringIO() |
55
c3291ad6b010
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
340 >>> write_po(outbuf, inbuf, locale_obj, project='FooBar', |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
341 ... 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
|
342 ... 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
|
343 ... plurals=(2, '(n != 1)')) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
344 >>> print outbuf.getvalue() # doctest: +ELLIPSIS |
55
c3291ad6b010
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
345 # Portuguese (Portugal) Translations for FooBar |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
346 # Copyright (C) 2007 ORGANIZATION |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
347 # 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
|
348 # FooBar project. |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
349 # A Name <user@domain.tld>, ... |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
350 # |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
351 #, fuzzy |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
352 msgid "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
353 msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
354 "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
|
355 "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
|
356 "PO-Revision-Date: ...\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
357 "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
|
358 "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
|
359 "MIME-Version: 1.0\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
360 "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
|
361 "Content-Transfer-Encoding: 8bit\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
362 "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
|
363 "Generated-By: Babel ...\n" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
364 <BLANKLINE> |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
365 #: base.py:83 templates/index.html:9 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
366 #: templates/index2.html:9 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
367 msgid "Home" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
368 msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
369 <BLANKLINE> |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
370 #: base.py:84 templates/index.html:9 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
371 msgid "Accounts" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
372 msgstr "" |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
373 <BLANKLINE> |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
374 >>> |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
375 """ |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
376 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
377 _first_author = '' |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
378 if first_author: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
379 _first_author += first_author |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
380 if first_author_email: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
381 _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
|
382 |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
383 inlines = input_fileobj.readlines() |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
384 outlines = [] |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
385 in_header = True |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
386 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
|
387 if in_header: |
68 | 388 if '# Translations template' in inlines[index]: |
55
c3291ad6b010
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
389 outlines.append('# %s Translations for %s\n' % \ |
c3291ad6b010
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
390 (locale_obj.english_name, project)) |
51
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
391 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
|
392 if _first_author: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
393 outlines.append( |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
394 '# %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
|
395 ) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
396 else: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
397 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
398 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
|
399 outlines.append( |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
400 '"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
|
401 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
|
402 ) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
403 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
|
404 if _first_author: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
405 outlines.append( |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
406 '"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
|
407 ) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
408 else: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
409 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
410 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
|
411 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
412 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
|
413 outlines.append( |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
414 '"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
|
415 ) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
416 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
|
417 inlines[index+1] == '\n': |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
418 in_header = False |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
419 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
420 else: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
421 outlines.append(inlines[index]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
422 else: |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
423 outlines.extend(inlines[index:]) |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
424 break |
7f61453c1bea
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
27
diff
changeset
|
425 fileobj.writelines(outlines) |