-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtranslate_po.py
executable file
·154 lines (128 loc) · 4.07 KB
/
translate_po.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# url: http://www.minilinux.net/node/27
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import httplib
import urllib
import urllib2
import traceback
import simplejson
import re
import random
import time
proxies = [
"proxy1:port",
"proxy2:port",
None,
]
def get_splits(text, length=4500):
'''
Translate Api has a limit on length of text(4500 characters) that can be translated at once
'''
return (text[index:index+length] for index in xrange(0,len(text),length))
def translate(text):
from_lang = "en"
to_lang = "zh-CN"
langpair = '%s|%s' % (from_lang, to_lang)
# OK, dirty hack, but works. ^_^
text = 'OMG!'.join(text.split('\n'))
base_url = 'http://ajax.googleapis.com/ajax/services/language/translate?'
params = {'v': 1.0,
'ie': 'UTF8',
'q': text,
'langpair': langpair}
new_text = ''
for splite in get_splits(text):
params['q'] = splite
data = urllib.urlencode(params)
resp = simplejson.load(urllib.urlopen('%s' % (base_url), data=data))
try:
translated = resp['responseData']['translatedText']
translated = translated.replace('%', '%')
translated = re.sub("&#(\d+);", lambda s: chr(int(s.group(1))), translated)
translated = re.sub("&([a-z]+);", lambda s: {"lt":"<", "gt":">", "amp":"&"}[s.group(1)], translated)
new_text += translated
except:
pass
# recover the word
return '\n'.join(new_text.split('OMG!'))
def translate_fixed(text):
# signature = random.randrange(1E20, 1E21)
notrans = []
def replace(match):
notrans.append(match.group(0))
return " 0.%d68065175210" % (len(notrans) - 1)
text = re.sub("\${[\w_]+}|\$[\w_]+", replace, text)
text = re.sub("\\\\\"|\\\\$|\\\\\\\\n|\\\\t", replace, text)
text = re.sub("\\\\", replace, text)
text = re.sub("[A-Z]{2,100}", replace, text)
text = re.sub("<\w+>|</\w+>", replace, text)
text = re.sub("puppy(?i)", replace, text)
trans = translate(text)
for i in range(len(notrans)):
# print "re.sub",
# print "%dx" % (signature + i), notrans[i], trans
trans = re.sub("0 ?.%d68065175210" % i, lambda x: notrans[i], trans)
return trans
random.seed()
if len(sys.argv) != 2:
print "Usage: " + sys.argv[0] + " filename.pot"
sys.exit(1)
if sys.argv[1] == "-":
f = sys.stdin
else:
f = open(sys.argv[1])
print """# ä¸æPuppy Linuxå¼åè
ä¹å®¶.
# This file is distributed under GPL.
#"""
print """#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: %(program)s\\n"
"Report-Msgid-Bugs-To: %(reportbug)s\\n"
"POT-Creation-Date: %(date)s\\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"Last-Translator: Google Translate\\n"
"Language-Team: Chinese\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
""" % {"program": sys.argv[1],
"reportbug": "[email protected]",
"date": time.strftime("%Y-%m-%d %H:%M%z")}
for line in f:
if not line.rstrip():
break;
trans = []
buf = ""
skip = False
for line in f:
line = line.rstrip()
if skip:
skip = line;
continue
if line.startswith("#,"):
print line + ", fuzzy"
elif line.startswith("#"):
print line
elif line.startswith("msgid "):
print "msgid",
line = line[6:]
elif line.startswith("msgstr "):
print "msgstr",
if buf:
trans.append(translate_fixed(buf))
buf = ""
for s in trans:
print "\"%s\"" % s
print
trans = []
skip = True
if line.startswith("\""):
print line
buf += line[1:-1]
if buf.endswith("\\n"):
trans.append(translate_fixed(buf[:-2])+"\\n")
buf = ""