annotate babel/messages/pofile.py @ 84:3ae316b58231 trunk

Some cosmetic changes for the new translator comments support.
author cmlenz
date Sun, 10 Jun 2007 18:03:46 +0000
parents 116e34b8cefa
children 96037779b518
rev   line source
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
2 #
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
5 #
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
9 #
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
13
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
14 """Reading and writing of files in the ``gettext`` PO (portable object)
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
15 format.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
16
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
17 :see: `The Format of PO Files
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
18 <http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files>`_
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
19 """
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
20
5
132526dcd074 * 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
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
22 import re
6
c3b1b0b3d129 Add basic 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:
c3b1b0b3d129 Add basic 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
c3b1b0b3d129 Add basic 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:
c3b1b0b3d129 Add basic 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
132526dcd074 * The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents: 1
diff changeset
28 import time
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
29
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
30 from babel import __version__ as VERSION
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
31 from babel.messages.catalog import Catalog
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
32
51
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
33 __all__ = ['escape', 'normalize', 'read_po', 'write_po', 'write_pot']
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
34
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
35 def read_po(fileobj):
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
36 """Read messages from a ``gettext`` PO (portable object) file from the given
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
37 file-like object and return a `Catalog`.
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
38
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
39 >>> from StringIO import StringIO
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
40 >>> buf = StringIO('''
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
41 ... #: main.py:1
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
42 ... #, fuzzy, python-format
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
43 ... msgid "foo %(name)s"
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
44 ... msgstr ""
21
cd9aa202568e Change pot header's first line, "Translations Template for %%(project)s." instead of "SOME DESCRIPTIVE TITLE.". '''`project`''' and '''`version`''' now default to '''PROJECT''' and '''VERSION''' respectively. Fixed a bug regarding '''Content-Transfer-Encoding''', it shouldn't be the charset, and we're defaulting to `8bit` untill someone complains.
palgarvio
parents: 17
diff changeset
45 ...
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
46 ... #: main.py:3
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
47 ... msgid "bar"
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
48 ... msgid_plural "baz"
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
49 ... msgstr[0] ""
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
50 ... msgstr[1] ""
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
51 ... ''')
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
52 >>> catalog = read_po(buf)
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
53 >>> for message in catalog:
67
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
54 ... if message.id:
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
55 ... print (message.id, message.string)
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
56 ... print ' ', (message.locations, message.flags)
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
57 ('foo %(name)s', '')
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
58 ([('main.py', 1)], set(['fuzzy', 'python-format']))
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
59 (('bar', 'baz'), ('', ''))
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
60 ([('main.py', 3)], set([]))
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
61
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
62 :param fileobj: the file-like object to read the PO file from
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
63 :return: an iterator over ``(message, translation, location)`` tuples
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
64 :rtype: ``iterator``
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
65 """
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
66 catalog = Catalog()
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
67
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
68 messages = []
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
69 translations = []
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
70 locations = []
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
71 flags = []
84
3ae316b58231 Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
72 comments = []
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
73 in_msgid = in_msgstr = False
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
74
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
75 def _add_message():
6
c3b1b0b3d129 Add basic 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 translations.sort()
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
77 if len(messages) > 1:
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
78 msgid = tuple(messages)
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
79 else:
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
80 msgid = messages[0]
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
81 if len(translations) > 1:
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
82 string = tuple([t[1] for t in translations])
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
83 else:
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
84 string = translations[0][1]
84
3ae316b58231 Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
85 catalog.add(msgid, string, list(locations), set(flags), list(comments))
3ae316b58231 Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
86 del messages[:]; del translations[:]; del locations[:];
3ae316b58231 Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
87 del flags[:]; del comments[:]
6
c3b1b0b3d129 Add basic 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
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
89 for line in fileobj.readlines():
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
90 line = line.strip()
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
91 if line.startswith('#'):
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
92 in_msgid = in_msgstr = False
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
93 if messages:
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
94 _add_message()
6
c3b1b0b3d129 Add basic 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 if line[1:].startswith(':'):
c3b1b0b3d129 Add basic 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 for location in line[2:].lstrip().split():
c3b1b0b3d129 Add basic 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 filename, lineno = location.split(':', 1)
c3b1b0b3d129 Add basic 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 locations.append((filename, int(lineno)))
c3b1b0b3d129 Add basic 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 elif line[1:].startswith(','):
c3b1b0b3d129 Add basic 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 for flag in line[2:].lstrip().split(','):
c3b1b0b3d129 Add basic 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 flags.append(flag.strip())
84
3ae316b58231 Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
102 elif line[1:].startswith('.'):
3ae316b58231 Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
103 comments.append(line[2:].strip)
6
c3b1b0b3d129 Add basic 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 elif line:
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
105 if line.startswith('msgid_plural'):
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
106 in_msgid = True
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
107 msg = line[12:].lstrip()
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
108 messages.append(msg[1:-1])
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
109 elif line.startswith('msgid'):
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
110 in_msgid = True
c3b1b0b3d129 Add basic 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 if messages:
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
112 _add_message()
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
113 msg = line[5:].lstrip()
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
114 messages.append(msg[1:-1])
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
115 elif line.startswith('msgstr'):
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
116 in_msgid = False
c3b1b0b3d129 Add basic 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_msgstr = True
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
118 msg = line[6:].lstrip()
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
119 if msg.startswith('['):
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
120 idx, msg = msg[1:].split(']')
c3b1b0b3d129 Add basic 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 translations.append([int(idx), msg.lstrip()[1:-1]])
c3b1b0b3d129 Add basic 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 else:
c3b1b0b3d129 Add basic 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 translations.append([0, msg[1:-1]])
c3b1b0b3d129 Add basic 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 elif line.startswith('"'):
c3b1b0b3d129 Add basic 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 if in_msgid:
c3b1b0b3d129 Add basic 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 messages[-1] += line.rstrip()[1:-1]
c3b1b0b3d129 Add basic 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 elif in_msgstr:
c3b1b0b3d129 Add basic 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 translations[-1][1] += line.rstrip()[1:-1]
c3b1b0b3d129 Add basic 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
c3b1b0b3d129 Add basic 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 if messages:
64
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
131 _add_message()
ef318245cfe5 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
132 return catalog
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
133
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 = """\
68
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
135 # Translations template for %(project)s.
79
450ac2291ca5 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
136 # Copyright (C) %(year)s %(copyright_holder)s
27
54ad80b62674 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
68
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
138 # %(project)s project.
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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.
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 #
68
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
141 """
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 WORD_SEP = re.compile('('
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 r'\s+|' # any whitespace
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
146 r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w)' # em-dash
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 ')')
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 def escape(string):
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 r"""Escape the given string so that it can be included in double-quoted
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 strings in ``PO`` files.
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 >>> escape('''Say:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 ... "hello, world!"
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 ... ''')
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 '"Say:\\n \\"hello, world!\\"\\n"'
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 :param string: the string to escape
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 :return: the escaped string
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 :rtype: `str` or `unicode`
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 """
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 return '"%s"' % string.replace('\\', '\\\\') \
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 .replace('\t', '\\t') \
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 .replace('\r', '\\r') \
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 .replace('\n', '\\n') \
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 .replace('\"', '\\"')
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 def normalize(string, width=76):
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 r"""This converts a string into a format that is appropriate for .po files.
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 >>> print normalize('''Say:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
172 ... "hello, world!"
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 ... ''', width=None)
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 ""
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 "Say:\n"
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 " \"hello, world!\"\n"
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 >>> print normalize('''Say:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
179 ... "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 ... ''', width=32)
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 ""
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 "Say:\n"
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 " \"Lorem ipsum dolor sit "
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 "amet, consectetur adipisicing"
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 " elit, \"\n"
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 :param string: the string to normalize
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 :param width: the maximum line width; use `None`, 0, or a negative number
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 to completely disable line wrapping
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 :return: the normalized string
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 :rtype: `unicode`
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 """
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 if width and width > 0:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 lines = []
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 for idx, line in enumerate(string.splitlines(True)):
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 if len(escape(line)) > width:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 chunks = WORD_SEP.split(line)
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 chunks.reverse()
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 while chunks:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 buf = []
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 size = 2
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 while chunks:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 l = len(escape(chunks[-1])) - 2
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
204 if size + l < width:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 buf.append(chunks.pop())
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 size += l
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 else:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 not buf:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 # handle long chunks by putting them on a
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 # separate line
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 buf.append(chunks.pop())
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 break
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
213 lines.append(u''.join(buf))
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 else:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 lines.append(line)
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 else:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 lines = string.splitlines(True)
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
67
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
219 if len(lines) <= 1:
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 return escape(string)
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 # Remove empty trailing line
67
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
223 if lines and not lines[-1]:
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 del lines[-1]
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 lines[-1] += '\n'
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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 return u'""\n' + u'\n'.join([escape(l) for l in lines])
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive 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
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
228 def write_pot(fileobj, catalog, project='PROJECT', version='VERSION', width=76,
78
d0d8d6cd8601 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
229 charset='utf-8', no_location=False, omit_header=False,
79
450ac2291ca5 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
230 sort_output=False, sort_by_file=False, copyright_holder=None):
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
231 r"""Write a ``gettext`` PO (portable object) template file for a given
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
232 message catalog to the provided file-like object.
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
233
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
234 >>> catalog = Catalog()
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
235 >>> catalog.add(u'foo %(name)s', locations=[('main.py', 1)],
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
236 ... flags=('fuzzy',))
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
237 >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)])
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
238 >>> from StringIO import StringIO
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
239 >>> buf = StringIO()
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
240 >>> write_pot(buf, catalog, omit_header=True)
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
241 >>> print buf.getvalue()
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
242 #: main.py:1
6
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
243 #, fuzzy, python-format
c3b1b0b3d129 Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents: 5
diff changeset
244 msgid "foo %(name)s"
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
245 msgstr ""
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
246 <BLANKLINE>
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
247 #: main.py:3
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
248 msgid "bar"
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
249 msgid_plural "baz"
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
250 msgstr[0] ""
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
251 msgstr[1] ""
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
252 <BLANKLINE>
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
253 <BLANKLINE>
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
254
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
255 :param fileobj: the file-like object to write to
67
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
256 :param catalog: the `Catalog` instance
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
257 :param project: the project name
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
258 :param version: the project version
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
259 :param width: the maximum line width for the generated output; use `None`,
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
260 0, or a negative number to completely disable line wrapping
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
261 :param charset: the encoding
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
262 :param no_location: do not emit a location comment for every message
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
263 :param omit_header: do not include the ``msgid ""`` entry at the top of the
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
264 output
79
450ac2291ca5 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
265 :param copyright_holder: sets the copyright holder in the output
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
266 """
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
267 def _normalize(key):
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
268 return normalize(key, width=width).encode(charset, 'backslashreplace')
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
269
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
270 def _write(text):
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
271 if isinstance(text, unicode):
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
272 text = text.encode(charset)
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
273 fileobj.write(text)
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
274
68
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
275 catalog.project = project
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
276 catalog.version = version
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
277 catalog.charset = charset
71
27f01e7626ea Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
278
27f01e7626ea Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
279 if sort_output:
27f01e7626ea Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
280 messages = list(catalog)
27f01e7626ea Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
281 messages.sort(lambda x,y: cmp(x.id, y.id))
27f01e7626ea Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
282 elif sort_by_file:
27f01e7626ea Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
283 messages = list(catalog)
27f01e7626ea Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
284 messages.sort(lambda x,y: cmp(x.locations, y.locations))
27f01e7626ea Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
285 else:
79
450ac2291ca5 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
286 messages = catalog
450ac2291ca5 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
287
450ac2291ca5 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
288 _copyright_holder = copyright_holder or 'ORGANIZATION'
68
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
289
71
27f01e7626ea Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
290 for message in messages:
67
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
291 if not message.id: # This is the header "message"
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
292 if omit_header:
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
293 continue
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
294 _write(POT_HEADER % {
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
295 'year': time.strftime('%Y'),
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
296 'project': project,
79
450ac2291ca5 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
297 'copyright_holder': _copyright_holder,
67
7b2fcd6d6d26 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
298 })
80
116e34b8cefa Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
299
116e34b8cefa Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
300 if message.comments:
116e34b8cefa Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
301 for comment in message.comments:
116e34b8cefa Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
302 for line in textwrap.wrap(comment,
116e34b8cefa Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
303 width, break_long_words=False):
116e34b8cefa Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
304 _write('#. %s\n' % line.strip())
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
305
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
306 if not no_location:
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
307 locs = u' '.join([u'%s:%d' % item for item in message.locations])
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
308 if width and width > 0:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
309 locs = textwrap.wrap(locs, width, break_long_words=False)
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
310 for line in locs:
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
311 _write('#: %s\n' % line.strip())
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
312 if message.flags:
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
313 _write('#%s\n' % ', '.join([''] + list(message.flags)))
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
314
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
315 if isinstance(message.id, (list, tuple)):
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
316 _write('msgid %s\n' % _normalize(message.id[0]))
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
317 _write('msgid_plural %s\n' % _normalize(message.id[1]))
68
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
318 for i, string in enumerate(message.string):
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
319 _write('msgstr[%d] %s\n' % (i, _normalize(message.string[i])))
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
320 else:
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
321 _write('msgid %s\n' % _normalize(message.id))
68
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
322 _write('msgstr %s\n' % _normalize(message.string or ''))
24
b09e90803d1b Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
323 _write('\n')
51
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
324
55
a258e4015eb6 `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
325 def write_po(fileobj, input_fileobj, locale_obj, project='PROJECT',
51
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
326 version='VERSION', first_author=None, first_author_email=None,
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
327 plurals=('INTEGER', 'EXPRESSION')):
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
328 r"""Write a ``gettext`` PO (portable object) file to the given file-like
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
329 object, from the given input PO template file.
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
330
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
331 >>> from StringIO import StringIO
55
a258e4015eb6 `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
332 >>> from babel import Locale
a258e4015eb6 `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
333 >>> locale_obj = Locale.parse('pt_PT')
68
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
334 >>> inbuf = StringIO(r'''# Translations template for FooBar.
51
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
335 ... # Copyright (C) 2007 ORGANIZATION
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
336 ... # This file is distributed under the same license as the
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
337 ... # FooBar project.
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
338 ... # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
339 ... #
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
340 ... #, fuzzy
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
341 ... msgid ""
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
342 ... msgstr ""
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
343 ... "Project-Id-Version: FooBar 0.1\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
344 ... "POT-Creation-Date: 2007-06-07 22:54+0100\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
345 ... "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
346 ... "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
347 ... "Language-Team: LANGUAGE <LL@li.org>\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
348 ... "MIME-Version: 1.0\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
349 ... "Content-Type: text/plain; charset=utf-8\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
350 ... "Content-Transfer-Encoding: 8bit\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
351 ... "Generated-By: Babel 0.1dev-r50\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
352 ...
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
353 ... #: base.py:83 templates/index.html:9
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
354 ... #: templates/index2.html:9
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
355 ... msgid "Home"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
356 ... msgstr ""
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
357 ...
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
358 ... #: base.py:84 templates/index.html:9
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
359 ... msgid "Accounts"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
360 ... msgstr ""
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
361 ... ''')
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
362 >>> outbuf = StringIO()
55
a258e4015eb6 `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
363 >>> write_po(outbuf, inbuf, locale_obj, project='FooBar',
51
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
364 ... version='0.1', first_author='A Name',
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
365 ... first_author_email='user@domain.tld',
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
366 ... plurals=(2, '(n != 1)'))
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
367 >>> print outbuf.getvalue() # doctest: +ELLIPSIS
55
a258e4015eb6 `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
368 # Portuguese (Portugal) Translations for FooBar
51
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
369 # Copyright (C) 2007 ORGANIZATION
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
370 # This file is distributed under the same license as the
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
371 # FooBar project.
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
372 # A Name <user@domain.tld>, ...
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
373 #
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
374 #, fuzzy
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
375 msgid ""
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
376 msgstr ""
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
377 "Project-Id-Version: FooBar 0.1\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
378 "POT-Creation-Date: 2007-06-07 22:54+0100\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
379 "PO-Revision-Date: ...\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
380 "Last-Translator: A Name <user@domain.tld>\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
381 "Language-Team: LANGUAGE <LL@li.org>\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
382 "MIME-Version: 1.0\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
383 "Content-Type: text/plain; charset=utf-8\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
384 "Content-Transfer-Encoding: 8bit\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
385 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
386 "Generated-By: Babel ...\n"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
387 <BLANKLINE>
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
388 #: base.py:83 templates/index.html:9
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
389 #: templates/index2.html:9
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
390 msgid "Home"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
391 msgstr ""
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
392 <BLANKLINE>
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
393 #: base.py:84 templates/index.html:9
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
394 msgid "Accounts"
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
395 msgstr ""
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
396 <BLANKLINE>
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
397 >>>
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
398 """
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
399
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
400 _first_author = ''
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
401 if first_author:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
402 _first_author += first_author
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
403 if first_author_email:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
404 _first_author += ' <%s>' % first_author_email
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
405
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
406 inlines = input_fileobj.readlines()
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
407 outlines = []
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
408 in_header = True
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
409 for index in range(len(inlines)):
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
410 if in_header:
68
269941aa0e55 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
411 if '# Translations template' in inlines[index]:
55
a258e4015eb6 `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
412 outlines.append('# %s Translations for %s\n' % \
a258e4015eb6 `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
413 (locale_obj.english_name, project))
51
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
414 elif '# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.' in inlines[index]:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
415 if _first_author:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
416 outlines.append(
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
417 '# %s, %s\n' % (_first_author, time.strftime('%Y'))
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
418 )
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
419 else:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
420 outlines.append(inlines[index])
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
421 elif '"PO-Revision-Date:' in inlines[index]:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
422 outlines.append(
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
423 '"PO-Revision-Date: %s\\n"\n' % \
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
424 time.strftime('%Y-%m-%d %H:%M%z')
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
425 )
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
426 elif '"Last-Translator:' in inlines[index]:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
427 if _first_author:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
428 outlines.append(
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
429 '"Last-Translator: %s\\n"\n' % _first_author
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
430 )
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
431 else:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
432 outlines.append(inlines[index])
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
433 elif '"Content-Transfer-Encoding:' in inlines[index]:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
434 outlines.append(inlines[index])
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
435 if '"Plural-Forms:' not in inlines[index+1]:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
436 outlines.append(
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
437 '"Plural-Forms: nplurals=%s; plural=%s;\\n"\n' % plurals
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
438 )
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
439 elif inlines[index].endswith('\\n"\n') and \
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
440 inlines[index+1] == '\n':
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
441 in_header = False
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
442 outlines.append(inlines[index])
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
443 else:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
444 outlines.append(inlines[index])
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
445 else:
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
446 outlines.extend(inlines[index:])
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
447 break
d484eb9a70d5 Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 27
diff changeset
448 fileobj.writelines(outlines)
Copyright (C) 2012-2017 Edgewall Software