annotate babel/messages/pofile.py @ 56:27d55a07c897

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