-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#!/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!' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,82 @@ | ||
# AppStringsTranslator | ||
Automatically translate iOS App language into multi-language, depends on Baidu translate. 依赖百度翻译,自动把 App 语言文件翻译成多国语言。 | ||
|
||
## Usage | ||
|
||
1. Modify `AppStringsTranslator.py`, fill your own Baidu `AppKey/SecretKey`, See [this](http://api.fanyi.baidu.com/api/trans/product/desktop?req=developer). | ||
2. Run script from terminal. | ||
3. Input strings file name. | ||
4. Input from language. | ||
5. Input to language list, split by space. | ||
6. Wait and done. | ||
|
||
``` bash | ||
>> LSiMac:AppStringsTranslator $ python AppStringsTranslator.py | ||
>> Enter a fileName: Localizable.strings | ||
>> Supports languages: | ||
['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'] | ||
>> Enter from language: zh | ||
>> Enter to language list, split by space: cht en jp kor | ||
>> Start | ||
Translating cht to fileName: Localizable_cht.strings | ||
Finished translating to cht | ||
Translating en to fileName: Localizable_en.strings | ||
Finished translating to en | ||
Translating jp to fileName: Localizable_jp.strings | ||
Finished translating to jp | ||
Translating kor to fileName: Localizable_kor.strings | ||
Finished translating to kor | ||
All done! | ||
|
||
``` | ||
|
||
## Feature | ||
|
||
- Translate all values in `xx.strings`. | ||
- Keep original keys, comments and empty lines. | ||
- Generate new strings file `xx_toLang.strings` in the same directory. | ||
|
||
## Supports languages | ||
|
||
``` bash | ||
语言简写 名称 | ||
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 越南语 | ||
``` | ||
|
||
## Q & A | ||
|
||
Q: Why do not use Google translate? | ||
A: In China, Google's services are not stable. | ||
|
||
|
||
## Reference | ||
|
||
1. [Baidu Translate API](http://api.fanyi.baidu.com/api/trans/product/apidoc) |