.POT Translation Tools_v1.0

.POT Translation Tools_v1.0

软件名称:.POT Translation Tools

软件版本:v1.0

软件语言:中文简体

适用平台:Windows

最后更新:2021-10-18

使用方法

1.拖动.pot文件至窗口回车即可翻译英语>中文。翻译次数过高会触发失败,只需稍等片刻把已翻译好的内容保存至新的文件,保存剩余未翻译内容再次拖动至窗口翻译。最后手动合并两个文件内容即可。【目前仅支持英语翻译中文】

2.使用Poedit打开.pot文件创建新的翻译。

.POT Translation Tools_v1.0

3.创建完成后验证翻译看有无错误。

.POT Translation Tools_v1.0

4.验证无误保存文件即可。

Python代码

main.py

  1. # -*- coding:utf-8 -*-  
  2. # Time:2021/10/17 12:37  
  3. # Author:qishe.cc  
  4.   
  5. import re  
  6. import os  
  7. import BaiduTranslate  
  8.   
  9. def fanyi(floder):  
  10.     infile = open(floder, 'a+', encoding='utf-8')  
  11.     new_txts = ''  
  12.     infile.seek(0)  
  13.     # 计数  
  14.     nb = 0  
  15.     content = []  
  16.     for lines in infile:  
  17.         if 'msgid' in lines or 'msgid_plural' in lines:  
  18.             # 匹配msgid "此处为需翻译的字符串"内容  
  19.             msg = re.findall('msgid "(.*?)"', lines)  
  20.             if msg:  
  21.                 for item in msg:  
  22.                     if item:  
  23.                         # 百度翻译  
  24.                         nb = nb + 1  
  25.                         print('[' + str(nb) + ']正在翻译 ' + item)  
  26.                         try:  
  27.                             content_txt = BaiduTranslate.Dict()  
  28.                             json = content_txt.dictionary(item, dst='zh', src='en')  
  29.                             content_txt = str(json['trans_result']['data'][0]['dst']).replace('%2元', '%2$s')  
  30.                             content.append(content_txt)  
  31.                             new_txts = new_txts + lines  
  32.                         except:  
  33.                             print('翻译频率过高触发验证,先保存已翻译内容并把剩余未翻译内容新建在新文件内稍等片刻再次翻译,最后合并翻译内容。')  
  34.                             infile.seek(0)  
  35.                             infile.truncate()  
  36.                             infile.write(new_txts)  
  37.                             infile.flush()  
  38.                             infile.close()  
  39.                             os.system('pause')  
  40.                     else:  
  41.                         new_txts = new_txts + lines  
  42.             else:  
  43.                 new_txts = new_txts + lines  
  44.         elif 'msgstr' in lines or 'msgstr[0]' in lines or 'msgstr[1]' in lines:  
  45.             if content:  
  46.                 if 'msgstr[0]' in lines:  
  47.                     key_msg = 'msgstr[0]'  
  48.                 elif 'msgstr[1]' in lines:  
  49.                     key_msg = 'msgstr[1]'  
  50.                 else:  
  51.                     key_msg = 'msgstr'  
  52.                 new_txts = new_txts + key_msg + ' "' + content[0] + '"\n'  
  53.                 content.clear()  
  54.             else:  
  55.                 new_txts = new_txts + lines  
  56.         elif lines == '':  
  57.             pass  
  58.         else:  
  59.             new_txts = new_txts + lines  
  60.     print('全部翻译完成~')  
  61.     infile.seek(0)  
  62.     infile.truncate()  
  63.     infile.write(new_txts)  
  64.     infile.flush()  
  65.     infile.close()  
  66.     os.system('pause')  
  67. if __name__ == '__main__':  
  68.     os.system("mode con cols=81 lines=30")  
  69.     try:  
  70.         tips = ''' 
  71. ******************************************************************************** 
  72. ***********************   .POT Translation Tools_v1.0   ************************ 
  73. ********************************  By:qishe.cc  ********************************* 
  74. ******************************  Date:2021.10.17  ******************************* 
  75. ***************************   https://www.manwuji.com   *************************** 
  76. ******************************************************************************** 
  77.             '''  
  78.         print("\033[32m%s" % tips)  
  79.         floder = input('注意:翻译前请备份好原.pot文件!!!\n注意:翻译前请备份好原.pot文件!!!\n注意:翻译前请备份好原.pot文件!!!\n\n\n拖动.pot文件至窗口:').replace('\\', '/')  
  80.         fanyi(floder)  
  81.   
  82.     except Exception as e:  
  83.         print(e)  
  84.         os.system('pause')  

BaiduTranslate.py

  1. # -*- coding: utf-8 -*-  
  2. import execjs  
  3. import requests  
  4. import re  
  5.   
  6. JS_CODE = """ 
  7. function a(r, o) { 
  8.     for (var t = 0; t < o.length - 2; t += 3) { 
  9.         var a = o.charAt(t + 2); 
  10.         a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a), 
  11.         a = "+" === o.charAt(t + 1) ? r >>> a: r << a, 
  12.         r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a 
  13.     } 
  14.     return r 
  15. } 
  16. var C = null; 
  17. var token = function(r, _gtk) { 
  18.     var o = r.length; 
  19.     o > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(o / 2) - 5, 10) + r.substring(r.length, r.length - 10)); 
  20.     var t = void 0, 
  21.     t = null !== C ? C: (C = _gtk || "") || ""; 
  22.     for (var e = t.split("."), h = Number(e[0]) || 0, i = Number(e[1]) || 0, d = [], f = 0, g = 0; g < r.length; g++) { 
  23.         var m = r.charCodeAt(g); 
  24.         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) 
  25.     } 
  26.     for (var S = h, 
  27.     u = "+-a^+6", 
  28.     l = "+-3^+b+-f", 
  29.     s = 0; s < d.length; s++) S += d[s], 
  30.     S = a(S, u); 
  31.  
  32.     return S = a(S, l), 
  33.     S ^= i, 
  34.     0 > S && (S = (2147483647 & S) + 2147483648), 
  35.     S %= 1e6, 
  36.     S.toString() + "." + (S ^ h) 
  37. } 
  38. """  
  39.   
  40.   
  41. class Dict:  
  42.     def __init__(self):  
  43.         self.sess = requests.Session()  
  44.         self.headers = {  
  45.             'User-Agent':  
  46.             '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'  
  47.         }  
  48.         self.token = None  
  49.         self.gtk = None  
  50.         self.javascript = execjs.compile(JS_CODE)  
  51.   
  52.         # 获得token和gtk  
  53.         # 必须要加载两次保证token是最新的,否则会出现998的错误  
  54.         self.loadMainPage()  
  55.         self.loadMainPage()  
  56.   
  57.     def loadMainPage(self):  
  58.         """ 
  59.             load main page : https://fanyi.baidu.com/ 
  60.             and get token, gtk 
  61.         """  
  62.         url = 'https://fanyi.baidu.com'  
  63.   
  64.         try:  
  65.             r = self.sess.get(url, headers=self.headers)  
  66.             self.token = re.findall(r"token: '(.*?)',", r.text)[0]  
  67.             self.gtk = re.findall(r"window.gtk = '(.*?)';", r.text)[0]  
  68.         except Exception as e:  
  69.             raise e  
  70.   
  71.     def langdetect(self, query):  
  72.         """ 
  73.             post query to https://fanyi.baidu.com/langdetect 
  74.             return json like 
  75.             {"error":0,"msg":"success","lan":"en"} 
  76.         """  
  77.         url = 'https://fanyi.baidu.com/langdetect'  
  78.         data = {'query': query}  
  79.         try:  
  80.             r = self.sess.post(url=url, data=data)  
  81.         except Exception as e:  
  82.             raise e  
  83.   
  84.         json = r.json()  
  85.         if 'msg' in json and json['msg'] == 'success':  
  86.             return json['lan']  
  87.         return None  
  88.   
  89.     def dictionary(self, query, dst='zh', src=None):  
  90.         """ 
  91.             get translate result from https://fanyi.baidu.com/v2transapi 
  92.         """  
  93.         url = 'https://fanyi.baidu.com/v2transapi'  
  94.   
  95.         sign = self.javascript.call('token', query, self.gtk)  
  96.   
  97.         if not src:  
  98.             src = self.langdetect(query)  
  99.   
  100.         data = {  
  101.             'from': src,  
  102.             'to': dst,  
  103.             'query': query,  
  104.             'simple_means_flag': 3,  
  105.             'sign': sign,  
  106.             'token': self.token,  
  107.         }  
  108.         try:  
  109.             r = self.sess.post(url=url, data=data)  
  110.         except Exception as e:  
  111.             raise e  
  112.   
  113.         if r.status_code == 200:  
  114.             json = r.json()  
  115.             if 'error' in json:  
  116.                 raise Exception('baidu sdk error: {}'.format(json['error']))  
  117.                 # 998错误则意味需要重新加载主页获取新的token  
  118.             return json  
  119.         return None  

感谢

感谢ZCY01提供的BaiduTranslate项目,实现免费翻译接口。

本文附件

本文密码:e0a313 网盘链接获取地址:https://www.manwuji.com/downloads.html

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
搜索