-
Notifications
You must be signed in to change notification settings - Fork 7
/
po_translate.py
124 lines (99 loc) · 3.19 KB
/
po_translate.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
# -*- coding: utf8 -*-
'''
Created on 2010/4/27
@author: Victor-mortal
'''
import os
import sys
import urllib
import json
import logging
import optparse
import codecs
import htmllib
import time
log = logging.getLogger(__name__)
#from version import __version__
USAGE = """usage: %prog sourcePo destPo sourceLanguage destLanguage
Example:
mypo.po output.po zh-TW zh-CN"""
def unescape(s):
p = htmllib.HTMLParser(None)
p.save_bgn()
p.feed(s)
return p.save_end()
def translate(message_strings, sourceLanguage, destLanguage):
""" Translate a text
@param message_strings: list of texts to translate
@param sourceLanguage: the language original text is in
@param destLanguage: language to translate to
"""
query = [
('key', 'AIzaSyC50uIaMik-Ib2Gjo1g10VhzpDjtPhc1FM'),
('v', '1.0'),
('langpair', '%s|%s' % (sourceLanguage, destLanguage))
]
for item in message_strings:
query.append(('q', item.encode('utf8')))
sock = urllib.urlopen(
'https://ajax.googleapis.com/ajax/services/language/translate',
data=urllib.urlencode(query)
)
text = sock.read()
translations = json.loads(text).get('responseData')
sock.close()
results = {}
for i in range(len(message_strings)):
k = message_strings[i]
try:
v = translations[i].get('responseData').get('translatedText')
results[k] = unescape(v)
except AttributeError:
logging.warning('Can\'t unescape')
logging.warning(v)
return results
def main():
import polib
logging.basicConfig(level=logging.INFO,
format="%(levelname)s - %(message)s")
parser = optparse.OptionParser(usage=USAGE)
(_, args) = parser.parse_args()
if len(args) != 4:
parser.print_usage()
return
sourceFilePath = args[0]
destFilePath = args[1]
sourceLang = args[2]
destLang = args[3]
log.info('Translate %s (in %s) to %s (in %s)',
sourceFilePath, sourceLang, destFilePath, destLang)
def process_cache(cache):
msgstrs = [item.msgstr for item in cache]
results = translate(msgstrs, sourceLang, destLang)
for item in cache:
translated = results.get(item.msgstr)
if translated is not None:
log.info('Translated %r to %r', item.msgstr, translated)
item.msgstr = translated
po = polib.pofile(sourceFilePath)
i = 0
cache = []
for entry in po:
if not entry.msgstr.strip():
entry.msgstr = entry.msgid.strip()
if not entry.msgstr.strip():
continue
if i < 20:
i = i + 1
cache.append(entry)
else:
process_cache(cache)
cache = []
i = 0
time.sleep(1)
process_cache(cache)
po.metadata['Translated-By'] = 'po_translator' #' %s' % __version__
po.save(destFilePath)
log.info('Done')
if __name__ == '__main__':
main()