一个弱爆的google 翻译客户端

2012年10月10日 02:06

在ubuntu下每次想翻译一个词或者一句话都要开浏览器上google翻译,感觉挺麻烦。于是自己写了一个python小脚本(googleTranslate.py),简化这个流程。

 

#!/usr/bin/python
# coding=utf-8

import urllib,urllib2
import sys
if __name__ == '__main__':
    if len(sys.argv) < 2:
        exit()
    tolanguage = 'en'
    text = ""
    if sys.argv[1] == 'en':
        tolanguage = 'en'
    elif sys.argv[1] == 'ch':
        tolanguage = 'zh-CN'
    else:
        text = sys.argv[1]
    for i in range(2, len(sys.argv)):
        text += sys.argv[i] + " "
    values = {'client':'t', 'text': text, 'hl':'en', 'sl':'auto', 'tl':tolanguage, 'ie':'UTF-8', 'oe':'UTF-8', 'multires':'1', 'otf':'2', 'ssel':'0', 'tsel':'0', 'sc':'1'}
    url = 'http://translate.google.cn/translate_a/t'
    request = urllib2.Request(url, urllib.urlencode(values))
    request.add_header('User-Agent', "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4")
    response = urllib2.urlopen(request)
    data = response.read()
    #print data
    frags = data[4:data.find("]]")].split("\"],[\"")
    totext = ""
    for frag in frags:
        totext += frag.split("\",\"")[0]
    print totext

将这个脚本放在某个目录下,此处以/path/to为例子。使用以下命令添加脚本可执行属性

chmod +x googleTranslate.py

然后在.bashrc最后添加一行

alias t='/path/to/googleTranslate.py'

于是乎在任何界面下想要翻译词句子,直接ctrl+alt+t,输入

t 想要翻译的词句

即可。默认是将词句翻译成英文,如果需要将其他语言翻译成中文,执行

t ch 想要翻译的词句

即可。方便快捷,o(∩∩)o...哈哈

 

而后觉得写成一个小软件还是很靠谱的,于是又折腾了下python Tkinter,把上面的代码加了一个GUI的外壳。废话少说,上图

GUI的代码也不复杂,顺便也贴了吧,供大家交流。BUG比较多,有待后期改进,谢谢阅读!

#!/usr/bin/python
# coding=utf-8

import urllib, urllib2
from Tkinter import *
from ttk import Combobox
 
def translate():
	tolanguage = languageMap[option.get()]
	text = startText.get('1.0', 'end').encode('utf-8')
	
	values = {'client':'t', 'text': text, 'hl':'en', 'sl':'auto', 'tl':tolanguage, 'ie':'UTF-8', 'oe':'UTF-8', 'multires':'1', 'otf':'2', 'ssel':'0', 'tsel':'0', 'sc':'1'}
	url = 'http://translate.google.cn/translate_a/t'
	request = urllib2.Request(url, urllib.urlencode(values))
	request.add_header('User-Agent', "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4")
	response = urllib2.urlopen(request)
	data = response.read()
	print data
	
	frags = data[4:data.find("]]")].split("\"],[\"")
	totext = ""
	for frag in frags:
		totext += frag.split("\",\"")[0]
	
	toText.delete('1.0', 'end')
	toText.insert('1.0', totext.replace("\\n", "\n"))

if __name__ == "__main__":
	
	mainWindow = Tk()
	mainWindow.title(u"Google 翻译 by hustsxh@gmail.com")
	#mainWindow.geometry('640x480+0+0')
	
	frame = Frame(mainWindow)
	frame.pack()
	
	Label(frame, text = u'翻译成:').pack(side = LEFT)
	
	languageMap = {'English': 'en', u'中文简体': 'zh-CN', u'中文繁體': 'zh-TW', u'日本語': 'ja', u'한국의': 'ko', u'Deutsch': 'de', u'русский': 'ru', u'française': 'fr'}
	defaultLanguage = StringVar(frame, 'English')
	option = Combobox(frame, text = defaultLanguage, values = languageMap.keys())
	option.pack(side = LEFT)
	
	transBotton = Button(frame, text =u"翻译", command = translate)
	transBotton.pack(side = LEFT)
	
	startText = Text(mainWindow, height = 15)
	startText.pack(fill = BOTH, padx = 10, pady = 10)
	
	toText = Text(mainWindow, height = 15)
	toText.pack(fill = BOTH, padx = 10, pady = 10)
	
	mainWindow.mainloop()