This repository has been archived by the owner on Jan 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
poxx.py
executable file
·207 lines (165 loc) · 5.52 KB
/
poxx.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Munge a .po file so we English-bound can see what strings aren't marked
for translation yet.
Run this with a .po file as an argument. It will set the translated strings
to be the same as the English, but with vowels in the wrong case:
./poxx.py locale/xx/LC_MESSAGES/messages.po
This code is in the public domain.
Contributors:
* Ned Batchelder
* Will Kahn-Greene
"""
import re
import string
import sys
try:
import polib # from http://bitbucket.org/izi/polib
except ImportError:
print "You need to install polib. Do:"
print ""
print " pip install polib"
sys.exit()
import HTMLParser
INTERP_RE = re.compile(
r'(%(?:[(].+?[)])?[#0 +-]?[.\d*]*[hlL]?[diouxXeEfFgGcrs%])')
def wc(c):
return c == "'" or c in string.letters
def nwc(c):
return not wc(c)
TRANSFORM = (
# INW?, NIW?, match, WC?, NW?, replacement
(False, False, "an", False, False, "un"),
(False, False, "An", False, False, "Un"),
(False, False, "au", False, False, "oo"),
(False, False, "Au", False, False, "Oo"),
(False, False, "a", True, False, "e"),
(False, False, "A", True, False, "E"),
(False, False, "en", False, True, "ee"),
(True, False, "ew", False, False, "oo"),
(True, False, "e", False, True, "e-a"),
(False, True, "e", False, False, "i"),
(False, True, "E", False, False, "I"),
(True, False, "f", False, False, "ff"),
(True, False, "ir", False, False, "ur"),
(True, False, "i", False, False, "ee"), # FIXME
(True, False, "ow", False, False, "oo"),
(False, True, "o", False, False, "oo"),
(False, True, "O", False, False, "Oo"),
(True, False, "o", False, False, "u"),
(False, False, "the", False, False, "zee"),
(False, False, "The", False, False, "Zee"),
(False, False, "th", False, True, "t"),
(True, False, "tion", False, False, "shun"),
(True, False, "u", False, False, "oo"),
(True, False, "U", False, False, "Oo"),
(False, False, "v", False, False, "f"),
(False, False, "V", False, False, "F"),
(False, False, "w", False, False, "v"),
(False, False, "W", False, False, "V")
)
def chef_transform(s):
# old_s = s
out = []
in_word = False # in a word?
# TODO: This is awful--better to do a real lexer
while s:
if s.startswith((".", "!", "?")):
in_word = False
out.append(s[0])
s = s[1:]
continue
for mem in TRANSFORM:
if in_word and not mem[0]:
continue
if not in_word and mem[1]:
continue
if not s.startswith(mem[2]):
continue
try:
if mem[3] and not wc(s[len(mem[2])]):
continue
except IndexError:
continue
try:
if mem[4] and not nwc(s[len(mem[2])]):
continue
except IndexError:
continue
out.append(mem[5])
s = s[len(mem[2]):]
in_word = True
break
else:
out.append(s[0])
s = s[1:]
# print old_s, "->", out
return u"".join(out)
class HtmlAwareMessageMunger(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.s = ""
def result(self):
return self.s
def xform(self, s):
return chef_transform(s)
def handle_starttag(self, tag, attrs, closed=False):
self.s += "<" + tag
for name, val in attrs:
self.s += " "
self.s += name
self.s += '="'
if name in ['alt', 'title']:
self.s += self.xform(val)
else:
self.s += val
self.s += '"'
if closed:
self.s += " /"
self.s += ">"
def handle_startendtag(self, tag, attrs):
self.handle_starttag(tag, attrs, closed=True)
def handle_endtag(self, tag):
self.s += "</" + tag + ">"
def handle_data(self, data):
# We don't want to munge placeholders, so split on them, keeping them
# in the list, then xform every other token.
toks = INTERP_RE.split(data)
for i, tok in enumerate(toks):
if i % 2:
self.s += tok
else:
self.s += self.xform(tok)
def handle_charref(self, name):
self.s += "&#" + name + ";"
def handle_entityref(self, name):
self.s += "&" + name + ";"
def translate_string(s):
hamm = HtmlAwareMessageMunger()
hamm.feed(s)
out = hamm.result()
if out.endswith(" >"):
return out[:-2] + u" börk! >"
elif out.endswith("\n"):
return out[:-2] + u" börk börk börk!\n"
return out + u" börk!"
def munge_one_file(fname):
po = polib.pofile(fname)
po.metadata["Language"] = "Swedish Chef"
po.metadata["Plural-Forms"] = "nplurals=2; plural= n != 1"
po.metadata["Content-Type"] = "text/plain; charset=UTF-8"
count = 0
for entry in po:
if entry.msgid_plural:
entry.msgstr_plural["0"] = translate_string(entry.msgid)
entry.msgstr_plural["1"] = translate_string(entry.msgid_plural)
else:
entry.msgstr = translate_string(entry.msgid)
if 'fuzzy' in entry.flags:
entry.flags.remove('fuzzy') # clear the fuzzy flag
count += 1
print "Munged %d messages in %s" % (count, fname)
po.save()
if __name__ == "__main__":
for fname in sys.argv[1:]:
munge_one_file(fname)