This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
extract_translations.py
56 lines (45 loc) · 1.84 KB
/
extract_translations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from zope.app.locales.extract import POTMaker, POTEntry
from zope.app.locales.extract import py_strings
from zope.app.locales.extract import tal_strings
from babel.messages.pofile import normalize
from zope.i18nmessageid import Message
from zope.app.locales.pygettext import make_escapes
make_escapes(1)
class CCPOTEntry(POTEntry):
def write(self, file):
if (isinstance(self.msgid, Message) and
self.msgid.default is not None):
# Write Default
default = self.msgid.default.strip()
lines = normalize(default).split("\n")
lines[0] = "#. Default: %s\n" % lines[0]
for i in range(1, len(lines)):
lines[i] = "#. %s\n" % lines[i]
file.write("".join(lines))
if self.comments:
file.write(self.comments)
file.write('msgid %s\n' % normalize(self.msgid))
if (isinstance(self.msgid, Message) and
self.msgid.default is not None):
# Write msgstr
file.write('msgstr %s\n' % normalize(default))
else:
file.write('msgstr ""\n')
file.write('\n')
class CCPOTMaker(POTMaker):
def add(self, strings, base_dir=None):
for msgid, locations in strings.items():
if msgid == '':
continue
if msgid not in self.catalog:
self.catalog[msgid] = CCPOTEntry(msgid)
for filename, lineno in locations:
if base_dir is not None:
filename = filename.replace(base_dir, '')
self.catalog[msgid].addLocationComment(filename, lineno)
maker = CCPOTMaker('extracted.pot', '')
maker.add(py_strings('cc/engine/', 'cc_org'),
'cc/engine/')
maker.add(tal_strings('cc/engine/templates/', 'cc_org', include_default_domain=True),
'cc/engine/templates/')
maker.write()