刚学ruby时写的工具,我自己一直在用,放在桌面上,遇到不懂单词就得问它。发在railscn上,ranchgirl修改的更为ruby way。
受blogjava上的帖子启发,实在是个有趣的主意,写了个ruby版本的,对俺这等英语一般的同学来说,google在线翻译经常使用呐,原贴之java版
http://www.blogjava.net/ekinglong/archive/2006/11/12/80704.html?Pending=true#Post
ranchgirl修改的版本,没有使用递归:
受blogjava上的帖子启发,实在是个有趣的主意,写了个ruby版本的,对俺这等英语一般的同学来说,google在线翻译经常使用呐,原贴之java版
http://www.blogjava.net/ekinglong/archive/2006/11/12/80704.html?Pending=true#Post
require
'
net/http
'
def translate
txt = STDIN . gets
break if txt . strip == ' e ' or txt . strip == ' exit '
temp = txt . split ( ' ' )
if temp[ 1 ] == ' 1 ' or temp . size == 1
langpair = ' en|zh-CN '
else
langpair = ' zh-CN|en '
end
# 使用代理
$proxy_addr = '127.0.0.1 '
$proxy_port = 80
response = Net :: HTTP . Proxy( $proxy_addr , $proxy_port ) . post_form(URI . parse( " http://translate.google.com/translate_t " ) , { ' text ' => temp[ 0 ] , ' langpair ' => langpair})
response . body =~ /< div id = result_box dir = ltr > ( .* ) <\/ div >/
result = $ 1
puts ' 翻译内容: ' + temp[ 0 ]
puts ' google返回: ' + result
puts ' -------------------退出请打e或者exit--------------- '
translate
end
translate
def translate
txt = STDIN . gets
break if txt . strip == ' e ' or txt . strip == ' exit '
temp = txt . split ( ' ' )
if temp[ 1 ] == ' 1 ' or temp . size == 1
langpair = ' en|zh-CN '
else
langpair = ' zh-CN|en '
end
# 使用代理
$proxy_addr = '127.0.0.1 '
$proxy_port = 80
response = Net :: HTTP . Proxy( $proxy_addr , $proxy_port ) . post_form(URI . parse( " http://translate.google.com/translate_t " ) , { ' text ' => temp[ 0 ] , ' langpair ' => langpair})
response . body =~ /< div id = result_box dir = ltr > ( .* ) <\/ div >/
result = $ 1
puts ' 翻译内容: ' + temp[ 0 ]
puts ' google返回: ' + result
puts ' -------------------退出请打e或者exit--------------- '
translate
end
translate
ranchgirl修改的版本,没有使用递归:
require
'
net/http
'
def usage
" usage: word [lang2 [lang1]]\n " +
" Translate word from lang1 (default en, English) to lang2 (default es, Spanish)\n " +
" ISO language code: http://www.unicode.org/unicode/onlinedat/languages.html "
end
def translate
arr = ARGV
if ! arr[ 0 ] then puts usage; return end
arr[ 1 ] = " es " unless arr[ 1 ]
arr[ 2 ] = " en " unless arr[ 2 ]
langpair = " #{arr[2]}|#{arr[1]} "
response = Net :: HTTP . post_form(URI . parse( " http://translate.google.com/translate_t " ) ,
{ : text => arr[ 0 ] , : langpair => langpair})
response . body =~ /< div id = result_box dir = ltr > ( .* ) <\/ div >/
result = $ 1
result = " No #{langpair} translation available for #{arr[0]} " if result . size == 0
puts result
end
translate
def usage
" usage: word [lang2 [lang1]]\n " +
" Translate word from lang1 (default en, English) to lang2 (default es, Spanish)\n " +
" ISO language code: http://www.unicode.org/unicode/onlinedat/languages.html "
end
def translate
arr = ARGV
if ! arr[ 0 ] then puts usage; return end
arr[ 1 ] = " es " unless arr[ 1 ]
arr[ 2 ] = " en " unless arr[ 2 ]
langpair = " #{arr[2]}|#{arr[1]} "
response = Net :: HTTP . post_form(URI . parse( " http://translate.google.com/translate_t " ) ,
{ : text => arr[ 0 ] , : langpair => langpair})
response . body =~ /< div id = result_box dir = ltr > ( .* ) <\/ div >/
result = $ 1
result = " No #{langpair} translation available for #{arr[0]} " if result . size == 0
puts result
end
translate
文章转自庄周梦蝶 ,原文发布时间5.17