-
Notifications
You must be signed in to change notification settings - Fork 16
/
AppStringsTranslator.py
129 lines (94 loc) · 3.25 KB
/
AppStringsTranslator.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
#!/usr/bin/python
#-*- coding:utf-8 -*-
# 百度翻译 API 文档:http://api.fanyi.baidu.com/api/trans/product/apidoc
import httplib
import md5
import urllib
import random
import re
import json
import os
import sys
kBaiduAppID = 'Please generate from you Baidu developer center' # 百度开发管理后台申请的 AppID
kBaiduSecretKey = 'Please generate from you Baidu developer center' # 百度开发管理后台申请的 SecretKey
gStringsFileName = ''
gStringsKeyList = []
gStringsValueList = []
gAllSupportedLangList = ['auto', 'zh', 'en', 'yue', 'wyw', 'jp', 'kor', 'fra', 'spa', 'th', 'ara', 'ru', 'pt', 'de', 'it', 'el', 'nl', 'pl', 'bul', 'est', 'dan', 'fin', 'cs', 'rom', 'slo', 'swe', 'hu', 'cht', 'vie']
reload(sys)
sys.setdefaultencoding( "utf-8" )
def initStringsKeyValueFromFile(fileName):
global gStringsFileName
global gStringsKeyList
global gStringsValueList
gStringsFileName = fileName
try:
f = open(fileName, 'r')
lines = f.readlines()
except IOError as e:
print e
else:
for line in lines:
match = re.search(r'"(?P<key>.*?)" = "(?P<value>.*?)"', line)
if match:
gStringsKeyList.append(match.group('key'))
gStringsValueList.append(match.group('value'))
else:
# 为了保存注释或空行到新的翻译文件
gStringsKeyList.append(line)
gStringsValueList.append('')
finally:
f.close()
def translateToLanguageList(fromLang, toLangs):
if fromLang not in gAllSupportedLangList:
print fromLang + 'is not supported'
return
for toLang in toLangs:
if toLang not in gAllSupportedLangList:
print toLang + 'is not supported'
break
translateToLang(fromLang, toLang)
def translateToLang(fromLang, toLang):
httpClient = None
myurl = '/api/trans/vip/translate'
httpClient = httplib.HTTPConnection('api.fanyi.baidu.com')
extension = os.path.splitext(gStringsFileName)[1]
toFileName = gStringsFileName.replace(extension, '_' + toLang + extension)
toFile = open(toFileName, 'w');
print 'Translating ' + toLang + ' to fileName: ' + toFileName
for index,val in enumerate(gStringsValueList):
q = val
if q:
salt = random.randint(32768, 65536)
sign = kBaiduAppID + q + str(salt) + kBaiduSecretKey
m1 = md5.new()
m1.update(sign)
sign = m1.hexdigest()
myurl = myurl + '?appid=' + kBaiduAppID + '&q=' + urllib.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(salt) + '&sign=' + sign
try:
httpClient.request('GET', myurl)
#response是HTTPResponse对象
response = httpClient.getresponse()
jsonData = json.loads(response.read())
dst = jsonData['trans_result'][0]['dst']
result = '"' + gStringsKeyList[index] + '" = "' + dst + '";\n'
toFile.write(result)
except Exception, e:
print e
else:
# 不需要翻译,直接保存原来的 Key
toFile.write(gStringsKeyList[index])
if httpClient:
httpClient.close()
if toFile:
toFile.close()
print 'Finished translating to ' + toLang
fileName = raw_input('Enter a fileName: ')
initStringsKeyValueFromFile(fileName)
print 'Supports languages:'
print gAllSupportedLangList
fromLang = raw_input('Enter from language: ')
toLangs = raw_input('Enter to language list, split by space: ')
print 'Start'
translateToLanguageList(fromLang, toLangs.split())
print 'All done!'