Skip to content

Commit

Permalink
Support for macos , ubuntu , Windows operating system(requires python…
Browse files Browse the repository at this point in the history
…2.x or python3.x environment)
  • Loading branch information
jiangxiaocai committed Jun 21, 2019
1 parent d0aa107 commit 6674790
Show file tree
Hide file tree
Showing 27 changed files with 47 additions and 75 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- support i18ns.com translation
- Check if the network is available, so choose google or Youdao translation channel
- 支持繁体,简体互译
- Support for macos , ubuntu , Windows operating system(requires python2.x or python3.x environment)
- Support alfred (will be added)
- more features will be added..

**The default translation is google translation, you can specify other translation channels**
Expand Down
1 change: 1 addition & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- 支持 i18ns.com 聚合翻译
- 会自动检测当前网络情况,从而决定使用谷歌还是有道翻译(有道翻译为了防IP封锁,使用3种渠道切换,所以速度会比谷歌慢一些,如果谷歌能用,将优先使用谷歌翻译)
- 支持繁体,简体互译
- 支持 macos , ubuntu , Windows 系统(需要 python2.x 或 python3.x 环境)


- **翻译之后,请务必人工核实,否则可能会因为语义上的差异带来理解困扰或政治风险。实际上任何一种翻译工具,都会有这种问题,请务必谨慎处理**
Expand Down
3 changes: 2 additions & 1 deletion mkTranslation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*
"""Command line tools translated using Google、Youdao and i18ns.com"""
__all__ = 'mkTranslator',
__version__ = '1.5.5'
__version__ = '1.5.5.1'


from mkTranslation.translator import mkTranslator
Expand Down
3 changes: 2 additions & 1 deletion mkTranslation/translate_chinese.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import sys

from mkTranslation import utils
from mkTranslation import zh_data
from mkTranslation.zh_data import zh2Hant, zh2Hans

Expand Down Expand Up @@ -150,7 +151,7 @@ def feed(self, char, map):
self.state = START
new = self.feed(char, map)
elif self.state == FAIL:
raise print('Translate States Machine '
raise utils.printf('Translate States Machine '
'have error with input data %s' % node)
return new

Expand Down
7 changes: 4 additions & 3 deletions mkTranslation/translate_youdao.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
from requests.exceptions import RequestException
from bs4 import BeautifulSoup
from mkTranslation import utils

PY3 = sys.version_info > (3, )
unicode = str if PY3 else unicode
Expand Down Expand Up @@ -48,7 +49,7 @@ def trans1(self, text, dest, src):
if(resp.json()['translation']):
return resp.json()['translation'][0]
except:
print('使用有道备用翻译通道2')
utils.printf('使用有道备用翻译通道2')
return self.trans2(text,dest,src)

def trans2(self, text, dest, src):
Expand Down Expand Up @@ -78,7 +79,7 @@ def trans2(self, text, dest, src):
return self.trans3(text,dest,src)

def trans3(self, text, dest, src):
print('使用接口,仅支持中英互译,将翻译为英文')
utils.printf('使用接口,仅支持中英互译,将翻译为英文')
params = {
'keyfrom': 'woodcol',
'key': '1522180019',
Expand All @@ -93,5 +94,5 @@ def trans3(self, text, dest, src):
self.result = r.json()
return self.result['translation'][0]
except RequestException as e:
print('有道免费接口遭遇封锁,请联系作者:https://github.com/mythkiven/mkTranslate')
utils.printf('有道免费接口遭遇封锁,请联系作者:https://github.com/mythkiven/mkTranslate')
sys.exit()
31 changes: 16 additions & 15 deletions mkTranslation/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import string
import requests
from mkTranslation import utils
from mkTranslation import network
from mkTranslation.translate_chinese import mkConverter
from mkTranslation.translate_google import mkGoogleTranslator
Expand Down Expand Up @@ -47,7 +48,7 @@ def translate_i18ns(self,dest,word,language):
if(isExist):
for x in txd:
if(dest == x['lang']):
print('use \'https://i18ns.com/\' translation '+word+' to '+x[dest][0] + ',others use google translation')
utils.printf('use \'https://i18ns.com/\' translation '+word+' to '+x[dest][0] + ',others use google translation')
return x[dest][0]+''
return 'null'

Expand All @@ -59,7 +60,7 @@ def translate(self,word,destination,language,channel):
if(tx != 'null'):
return tx
except Exception as e:
print('')
utils.printf('')
if(destination.lower() == 'zh-hant'):
return mkConverter('zh-hant').convert(word)
elif(destination.lower() == 'zh-hans'):
Expand All @@ -73,7 +74,7 @@ def translate(self,word,destination,language,channel):
return mkYouDaoTranslator().translate(word,destination,language)

def fix_tx(self,txt):
print('《《《'+txt)
utils.printf('《《《'+txt)
if(txt.find('% ld')!=-1 or txt.find('% @')!=-1):
tsing = re.search(r'%\s*(@|ld)\s*/\s*%\s*(ld|@)',txt)
if(tsing):
Expand All @@ -91,22 +92,22 @@ def write_tx(self,oldfile,newfile,reg,creg,des,lan,channel):
line = f.readline()
continue
originLine = line
print('original:'+originLine)
utils.printf('original:'+originLine)
line = re.findall(reg,line)
if(len(line) and line[0]):
txc = self.translate(line[0],des,lan,channel)
if(len(txc)):
originLine = re.sub(reg,creg.replace('content',txc), originLine)
else:
print('translate fail:' + line)
utils.printf('translate fail:' + line)
elif(reg==creg==r'text'):
originLine = self.translate(originLine,des,lan,channel)
else:
print('skip: '+originLine)
utils.printf('skip: '+originLine)
originLine = self.fix_tx(originLine)
print('translated:'+originLine)
utils.printf('translated:'+originLine)
txd += originLine + '\n'
print('-----')
utils.printf('-----')
line = f.readline()
f = open(newfile,'w+')
f.write(txd)
Expand All @@ -116,12 +117,12 @@ def log(self,channel,dest):
if(dest == 'zh-hant' or dest == 'zh-hans'):
return
if(channel == 'None'):
print('[No network, no translation!]')
utils.printf('[No network, no translation!]')
sys.exit()
elif(channel == 'google'):
print('[Use google translation]')
utils.printf('[Use google translation]')
else:
print('[Use youdao translation]')
utils.printf('[Use youdao translation]')

def translate_text(self,text, destination,sourcelanguage,channel):
channel = network.select_network(channel)
Expand All @@ -130,9 +131,9 @@ def translate_text(self,text, destination,sourcelanguage,channel):
if(channel == 'None'):
pass
elif(channel == 'google'):
print(mkGoogleTranslator().translate_text(text, dest=destination).text)
utils.printf(mkGoogleTranslator().translate_text(text, dest=destination).text)
else:
print(mkYouDaoTranslator().translate(text,destination,sourcelanguage))
utils.printf(mkYouDaoTranslator().translate(text,destination,sourcelanguage))

def translate_doc(self,filepath, destination,sourcelanguage,channel):
channel = network.select_network(channel)
Expand All @@ -149,7 +150,7 @@ def translate_doc(self,filepath, destination,sourcelanguage,channel):
else:
newFile = os.path.join(currentPath, 'translate_'+destination+'_by_'+channel+'_'+oldFileName)
txd = ''
print('translating..')
utils.printf('translating..')

# text
if(fileType.lower() == 'text' or fileType.lower() == 'txt'):
Expand All @@ -171,4 +172,4 @@ def translate_doc(self,filepath, destination,sourcelanguage,channel):
f = open(newFile,'w+')
f.write(txd)
f.close()
print('translation completed,file saved in ['+newFile+']')
utils.printf('translation completed,file saved in ['+newFile+']')
2 changes: 2 additions & 0 deletions mkTranslation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def legacy_format_json(original):
converted = json.loads(text)
return converted

def printf(text):
print(text)

def format_json(original):
try:
Expand Down
12 changes: 12 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

translate -t '增加繁体简体互译兼容性' -d t
echo "====="
translate -t '增加繁體簡體互譯兼容性' -d s
echo "====="
translate -t '增加繁體簡體互譯兼容性' -d 'ja'
echo "====="
translate -p ./test.txt -d 'zh-TW'
echo "====="
translate -p ./ios.strings -d
echo "====="
translate -p ./android.xml -d 'pt'
6 changes: 0 additions & 6 deletions test/translate_en_by_google_android.xml

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_en_by_google_ios.strings

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_en_by_google_test.txt

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_gu_by_google_ios.strings

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_gu_by_youdao_ios.strings

This file was deleted.

5 changes: 0 additions & 5 deletions test/translate_ja_by_google_android.xml

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_ja_by_google_ios.strings

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_ja_by_google_test.txt

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_ka_by_google_ios.strings

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_ko_by_google_ios.strings

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_pt_by_google_test.txt

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_pt_by_youdao_test.txt

This file was deleted.

5 changes: 0 additions & 5 deletions test/translate_zh-cn_by_google_chinese.txt

This file was deleted.

5 changes: 0 additions & 5 deletions test/translate_zh-hans_chinese.txt

This file was deleted.

3 changes: 0 additions & 3 deletions test/translate_zh-hant_android.xml

This file was deleted.

5 changes: 0 additions & 5 deletions test/translate_zh-hant_chinese.txt

This file was deleted.

2 changes: 0 additions & 2 deletions test/translate_zh-hant_ios.strings

This file was deleted.

5 changes: 3 additions & 2 deletions trans
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import sys
import re
from mkTranslation import utils
from mkTranslation.translator import mkTranslator

def main():
Expand All @@ -21,13 +22,13 @@ def main():
args.dest_language = 'zh-hant'
elif(args.dest_language == 'zh-CN'):
args.dest_language = 'zh-hans'
print('Target language '+args.dest_language)
utils.printf('Target language '+args.dest_language)
if args.detect:
mkTranslator().translate_text(text=args.text, destination=args.dest_language,sourcelanguage=args.sourcelanguage,channel=args.channel)
elif(args.text):
mkTranslator().translate_text(text=args.text, destination=args.dest_language,sourcelanguage=args.sourcelanguage,channel=args.channel)
else:
print('-p:path of the document being translated -d:target language (Default: "en",繁体:"t",简体:"s") -c:translation channel:[-c "google"] or [-c "youdao"] ')
utils.printf('-p:path of the document being translated -d:target language (Default: "en",繁体:"t",简体:"s") -c:translation channel:[-c "google"] or [-c "youdao"] ')
if __name__ == '__main__':
main()

Expand Down
5 changes: 3 additions & 2 deletions translate
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import sys
import re
from mkTranslation import utils
from mkTranslation.translator import mkTranslator

def main():
Expand All @@ -21,13 +22,13 @@ def main():
args.dest_language = 'zh-hant'
elif(args.dest_language == 'zh-CN'):
args.dest_language = 'zh-hans'
print('Target language: '+args.dest_language)
utils.printf('Target language: '+args.dest_language)
if args.text:
mkTranslator().translate_text(text=args.text, destination=args.dest_language,sourcelanguage=args.sourcelanguage,channel=args.channel)
elif(args.file_path):
mkTranslator().translate_doc(filepath=args.file_path, destination=args.dest_language,sourcelanguage=args.sourcelanguage,channel=args.channel)
else:
print('-p:path of the document being translated -t:translated text -d:target language (Default: "en",繁体:"t",简体:"s") -c:translation channel:[-c "google"] or [-c "youdao"] -s:original language, when using "youdao" translation, it is best to set otherwise translation will not be accurate')
utils.printf('-p:path of the document being translated -t:translated text -d:target language (Default: "en",繁体:"t",简体:"s") -c:translation channel:[-c "google"] or [-c "youdao"] -s:original language, when using "youdao" translation, it is best to set otherwise translation will not be accurate')
if __name__ == '__main__':
main()

0 comments on commit 6674790

Please sign in to comment.