annotate babel/messages/pofile.py @ 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. Removed the language and coutry arguments of `write_po`, which instead accepts a `babel.core.Locale` object to retrieve the needed object.
author palgarvio
date Fri, 08 Jun 2007 10:37:47 +0000
parents b3395b285104
children 27fba894d3ca
rev   line source
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
2 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
5 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
9 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
13
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
14 """Reading and writing of files in the ``gettext`` PO (portable object)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
15 format.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
16
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
17 :see: `The Format of PO Files
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
18 <http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
19 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
20
5
50ad95bee876 * The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents: 1
diff changeset
21 from datetime import date, datetime
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
22 import re
6
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
23 try:
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
24 set
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
25 except NameError:
1801bc2b60ca Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
26 from sets import Set as set
24
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
27 import textwrap
5
50ad95bee876 * The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents: 1
diff changeset
28 import time
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
29
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
30 from babel import __version__ as VERSION
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
33
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
37
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
38 This function yields tuples of the form:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
41
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
42 where:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
43
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
44 * ``message`` is the original (untranslated) message, or a
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
45 ``(singular, plural)`` tuple for pluralizable messages
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
46 * ``translation`` is the translation of the message, or a tuple of
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
47 translations for pluralizable messages
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
71
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
72 :param fileobj: the file-like object to read the PO file from
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
73 :return: an iterator over ``(message, translation, location)`` tuples
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
74 :rtype: ``iterator``
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
92 for line in fileobj.readlines():
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
93 line = line.strip()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
119 msg = line[6:].lstrip()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
247
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
248 The `messages` parameter is expected to be an iterable object producing
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
249 tuples of the form:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
252
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
253 >>> from StringIO import StringIO
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
258 ... ], omit_header=True)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
259
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
260 >>> print buf.getvalue()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
264 msgstr ""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
265 <BLANKLINE>
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
266 #: main.py:3
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
267 msgid "bar"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
268 msgid_plural "baz"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
269 msgstr[0] ""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
270 msgstr[1] ""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
271 <BLANKLINE>
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
272 <BLANKLINE>
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
273
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
274 :param fileobj: the file-like object to write to
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
275 :param messages: an iterable over the messages
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
276 :param project: the project name
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
280 :param charset: the encoding
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
281 :param no_location: do not emit a location comment for every message
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
282 :param omit_header: do not include the ``msgid ""`` entry at the top of the
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
283 output
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
284 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
292
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
299 'charset': charset,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
300 })
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
301
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
313 if key in msgids:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
324 msgids.append(key)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
325
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
326 for msgid in msgids:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
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
347 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
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
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
354 >>> 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
355 >>> locale_obj = Locale.parse('pt_PT')
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
356 >>> 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
357 ... # Copyright (C) 2007 ORGANIZATION
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
358 ... # 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
359 ... # FooBar project.
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
360 ... # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
361 ... #
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
362 ... #, fuzzy
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
363 ... msgid ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
364 ... msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
365 ... "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
366 ... "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
367 ... "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
368 ... "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
369 ... "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
370 ... "MIME-Version: 1.0\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
371 ... "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
372 ... "Content-Transfer-Encoding: 8bit\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
373 ... "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
374 ...
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
375 ... #: 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
376 ... #: templates/index2.html:9
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
377 ... msgid "Home"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
378 ... msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
379 ...
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
380 ... #: 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
381 ... msgid "Accounts"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
382 ... msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
383 ... ''')
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
384 >>> 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
385 >>> 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
386 ... 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
387 ... 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
388 ... plurals=(2, '(n != 1)'))
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
389 >>> 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
390 # 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
391 # Copyright (C) 2007 ORGANIZATION
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
392 # 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
393 # FooBar project.
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
394 # A Name <user@domain.tld>, ...
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 #, fuzzy
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
397 msgid ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
398 msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
399 "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
400 "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
401 "PO-Revision-Date: ...\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
402 "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
403 "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
404 "MIME-Version: 1.0\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
405 "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
406 "Content-Transfer-Encoding: 8bit\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
407 "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
408 "Generated-By: Babel ...\n"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
409 <BLANKLINE>
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
410 #: 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
411 #: templates/index2.html:9
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
412 msgid "Home"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
413 msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
414 <BLANKLINE>
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
415 #: 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
416 msgid "Accounts"
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
417 msgstr ""
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
418 <BLANKLINE>
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 """
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
421
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
422 _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:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
424 _first_author += first_author
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
425 if first_author_email:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
426 _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
427
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
428 inlines = input_fileobj.readlines()
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
429 outlines = []
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
430 in_header = True
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
431 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
432 if in_header:
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
433 if '# Translations Template' in inlines[index]:
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
434 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
435 (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
436 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
437 if _first_author:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
438 outlines.append(
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
439 '# %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
440 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
441 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
442 outlines.append(inlines[index])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
443 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
444 outlines.append(
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
445 '"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
446 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
447 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
448 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
449 if _first_author:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
450 outlines.append(
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
451 '"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
452 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
453 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
454 outlines.append(inlines[index])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
455 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
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 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
458 outlines.append(
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
459 '"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
460 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
461 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
462 inlines[index+1] == '\n':
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
463 in_header = False
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
464 outlines.append(inlines[index])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
465 else:
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.extend(inlines[index:])
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
469 break
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
470 fileobj.writelines(outlines)
Copyright (C) 2012-2017 Edgewall Software