Skip to content

Instantly share code, notes, and snippets.

@calvingit
Created October 19, 2016 08:10
Show Gist options
  • Save calvingit/2061063365ff473ff287f56e501cd92f to your computer and use it in GitHub Desktop.
Save calvingit/2061063365ff473ff287f56e501cd92f to your computer and use it in GitHub Desktop.
iOS 调用百度翻译 API 将中文 strings 翻译成其他国家语言
# /usr/bin/env python
# -*- coding: utf-8 -*-
# 同级目录下面包含 Localizable.strings 或者 InfoPlist.strings,把strings文件的注释都去掉
# cnFile 参数修改需要翻译Localizable还是InfoPlist
# 到百度翻译API页面申请免费的appid和secretKey(http://api.fanyi.baidu.com/api/trans/product/index)
# 百度翻译每月翻译字符数低于200万,享免费服务
# 会自动生成对应的.lproj目录
import httplib
import hashlib
import urllib
import random
import json
import sys
import os
# 中文翻译文件
cnFile = 'Localizable' #或者'InfoPlist'
# 替换自己的百度翻译API接口
appid = '2016101700003'
secretKey = 'Q4ZQM82ldzCv9Lz'
def translateChinese(query, destLang):
httpClient = None
myurl = '/api/trans/vip/translate'
fromLang = 'zh'
salt = random.randint(32768, 65536)
sign = appid + query + str(salt) + secretKey
m1 = hashlib.md5()
m1.update(sign)
sign = m1.hexdigest()
myurl = myurl + '?appid=' + appid + '&q=' + urllib.quote(
query) + '&from=' + fromLang + '&to=' + destLang + '&salt=' + str(
salt) + '&sign=' + sign
try:
httpClient = httplib.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
# response是HTTPResponse对象
response = httpClient.getresponse()
return response.read()
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()
return None
def writeNewStringsFile(lan):
translationsList = []
folder = lan[2] + '.lproj'
if not os.path.isdir(folder):
os.mkdir(folder)
file = open(cnFile + '.strings', 'rb')
while 1:
lines = file.readlines(100000)
if not lines:
break
for line in lines:
line.rstrip("\n")
if len(line) < 4:
continue
tempArray = line.split('=')
key = tempArray[0].strip().strip('\"')
value = tempArray[1].strip().strip(';\n').strip('\"')
translationsList.append((key,value))
file.close()
file = open( folder + '/' + cnFile +'.strings', 'w+')
for tup in translationsList:
ret = translateChinese(tup[1], lan[1]);
decodejson = json.loads(ret)
resultList = decodejson['trans_result']
first = resultList[0]
dst = first['dst']
dst = dst.strip('\"').capitalize()
strings = '\"' + tup[0] + '\"=\"' + dst + '\";\n'
strings = strings.decode('UTF-8')
print lan[0] + ': ' + strings
file.write(strings)
file.close()
#tuple(中文,百度语言缩写,Xcode语言缩写)
lans = [('英语', 'en', 'en'),
('日语', 'jp', 'ja'),
('韩语', 'kor', 'ko'),
('法语', 'fra', 'fr'),
('西班牙语', 'spa', 'es'),
('泰语', 'th', 'th'),
('阿拉伯语', 'ara','ar'),
('俄语', 'ru', 'ru'),
('葡萄牙语', 'pt', 'pt-PT'),
('德语', 'de', 'de'),
('意大利语', 'it', 'it'),
('荷兰语', 'nl', 'nl'),
('波兰语', 'pl', 'pl'),
('保加利亚语', 'bul', 'bg'),
('爱沙尼亚语', 'est', 'et'),
('丹麦语', 'dan', 'da'),
('芬兰语', 'fin', 'fi'),
('捷克语', 'cs','cs'),
('罗马尼亚语', 'rom', 'ro'),
('瑞典语', 'swe', 'sv'),
('匈牙利语', 'hu', 'hu'),
('繁体中文', 'cht', 'zh-Hant')];
reload(sys) # reload 才能调用 setdefaultencoding 方法
sys.setdefaultencoding('utf-8') # 设置 'utf-8'
for lan in lans :
writeNewStringsFile(lan)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment