软件名称:.POT Translation Tools
软件版本:v1.0
软件语言:中文简体
适用平台:Windows
最后更新:2021-10-18
使用方法
1.拖动.pot文件至窗口回车即可翻译英语>中文。翻译次数过高会触发失败,只需稍等片刻把已翻译好的内容保存至新的文件,保存剩余未翻译内容再次拖动至窗口翻译。最后手动合并两个文件内容即可。【目前仅支持英语翻译中文】
2.使用Poedit打开.pot文件创建新的翻译。
3.创建完成后验证翻译看有无错误。
4.验证无误保存文件即可。
Python代码
main.py
- # -*- coding:utf-8 -*-
- # Time:2021/10/17 12:37
- # Author:qishe.cc
- import re
- import os
- import BaiduTranslate
- def fanyi(floder):
- infile = open(floder, 'a+', encoding='utf-8')
- new_txts = ''
- infile.seek(0)
- # 计数
- nb = 0
- content = []
- for lines in infile:
- if 'msgid' in lines or 'msgid_plural' in lines:
- # 匹配msgid "此处为需翻译的字符串"内容
- msg = re.findall('msgid "(.*?)"', lines)
- if msg:
- for item in msg:
- if item:
- # 百度翻译
- nb = nb + 1
- print('[' + str(nb) + ']正在翻译 ' + item)
- try:
- content_txt = BaiduTranslate.Dict()
- json = content_txt.dictionary(item, dst='zh', src='en')
- content_txt = str(json['trans_result']['data'][0]['dst']).replace('%2元', '%2$s')
- content.append(content_txt)
- new_txts = new_txts + lines
- except:
- print('翻译频率过高触发验证,先保存已翻译内容并把剩余未翻译内容新建在新文件内稍等片刻再次翻译,最后合并翻译内容。')
- infile.seek(0)
- infile.truncate()
- infile.write(new_txts)
- infile.flush()
- infile.close()
- os.system('pause')
- else:
- new_txts = new_txts + lines
- else:
- new_txts = new_txts + lines
- elif 'msgstr' in lines or 'msgstr[0]' in lines or 'msgstr[1]' in lines:
- if content:
- if 'msgstr[0]' in lines:
- key_msg = 'msgstr[0]'
- elif 'msgstr[1]' in lines:
- key_msg = 'msgstr[1]'
- else:
- key_msg = 'msgstr'
- new_txts = new_txts + key_msg + ' "' + content[0] + '"\n'
- content.clear()
- else:
- new_txts = new_txts + lines
- elif lines == '':
- pass
- else:
- new_txts = new_txts + lines
- print('全部翻译完成~')
- infile.seek(0)
- infile.truncate()
- infile.write(new_txts)
- infile.flush()
- infile.close()
- os.system('pause')
- if __name__ == '__main__':
- os.system("mode con cols=81 lines=30")
- try:
- tips = '''
- ********************************************************************************
- *********************** .POT Translation Tools_v1.0 ************************
- ******************************** By:qishe.cc *********************************
- ****************************** Date:2021.10.17 *******************************
- *************************** https://www.manwuji.com ***************************
- ********************************************************************************
- '''
- print("\033[32m%s" % tips)
- floder = input('注意:翻译前请备份好原.pot文件!!!\n注意:翻译前请备份好原.pot文件!!!\n注意:翻译前请备份好原.pot文件!!!\n\n\n拖动.pot文件至窗口:').replace('\\', '/')
- fanyi(floder)
- except Exception as e:
- print(e)
- os.system('pause')
BaiduTranslate.py
- # -*- coding: utf-8 -*-
- import execjs
- import requests
- import re
- JS_CODE = """
- function a(r, o) {
- for (var t = 0; t < o.length - 2; t += 3) {
- var a = o.charAt(t + 2);
- a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
- a = "+" === o.charAt(t + 1) ? r >>> a: r << a,
- r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
- }
- return r
- }
- var C = null;
- var token = function(r, _gtk) {
- var o = r.length;
- o > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(o / 2) - 5, 10) + r.substring(r.length, r.length - 10));
- var t = void 0,
- t = null !== C ? C: (C = _gtk || "") || "";
- for (var e = t.split("."), h = Number(e[0]) || 0, i = Number(e[1]) || 0, d = [], f = 0, g = 0; g < r.length; g++) {
- var m = r.charCodeAt(g);
- 128 > m ? d[f++] = m: (2048 > m ? d[f++] = m >> 6 | 192 : (55296 === (64512 & m) && g + 1 < r.length && 56320 === (64512 & r.charCodeAt(g + 1)) ? (m = 65536 + ((1023 & m) << 10) + (1023 & r.charCodeAt(++g)), d[f++] = m >> 18 | 240, d[f++] = m >> 12 & 63 | 128) : d[f++] = m >> 12 | 224, d[f++] = m >> 6 & 63 | 128), d[f++] = 63 & m | 128)
- }
- for (var S = h,
- u = "+-a^+6",
- l = "+-3^+b+-f",
- s = 0; s < d.length; s++) S += d[s],
- S = a(S, u);
- return S = a(S, l),
- S ^= i,
- 0 > S && (S = (2147483647 & S) + 2147483648),
- S %= 1e6,
- S.toString() + "." + (S ^ h)
- }
- """
- class Dict:
- def __init__(self):
- self.sess = requests.Session()
- self.headers = {
- 'User-Agent':
- 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
- }
- self.token = None
- self.gtk = None
- self.javascript = execjs.compile(JS_CODE)
- # 获得token和gtk
- # 必须要加载两次保证token是最新的,否则会出现998的错误
- self.loadMainPage()
- self.loadMainPage()
- def loadMainPage(self):
- """
- load main page : https://fanyi.baidu.com/
- and get token, gtk
- """
- url = 'https://fanyi.baidu.com'
- try:
- r = self.sess.get(url, headers=self.headers)
- self.token = re.findall(r"token: '(.*?)',", r.text)[0]
- self.gtk = re.findall(r"window.gtk = '(.*?)';", r.text)[0]
- except Exception as e:
- raise e
- def langdetect(self, query):
- """
- post query to https://fanyi.baidu.com/langdetect
- return json like
- {"error":0,"msg":"success","lan":"en"}
- """
- url = 'https://fanyi.baidu.com/langdetect'
- data = {'query': query}
- try:
- r = self.sess.post(url=url, data=data)
- except Exception as e:
- raise e
- json = r.json()
- if 'msg' in json and json['msg'] == 'success':
- return json['lan']
- return None
- def dictionary(self, query, dst='zh', src=None):
- """
- get translate result from https://fanyi.baidu.com/v2transapi
- """
- url = 'https://fanyi.baidu.com/v2transapi'
- sign = self.javascript.call('token', query, self.gtk)
- if not src:
- src = self.langdetect(query)
- data = {
- 'from': src,
- 'to': dst,
- 'query': query,
- 'simple_means_flag': 3,
- 'sign': sign,
- 'token': self.token,
- }
- try:
- r = self.sess.post(url=url, data=data)
- except Exception as e:
- raise e
- if r.status_code == 200:
- json = r.json()
- if 'error' in json:
- raise Exception('baidu sdk error: {}'.format(json['error']))
- # 998错误则意味需要重新加载主页获取新的token
- return json
- return None
感谢
感谢ZCY01提供的BaiduTranslate项目,实现免费翻译接口。
本文附件
本文密码:e0a313 网盘链接获取地址:https://www.manwuji.com/downloads.html
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。