See original post by killme2008 from the following link
[url=http://www.railscn.com/viewtopic.php?p=12892#12892] ruby版google在线翻译:)[/url]
My post there
[quote]
Good work, Thank you!
I modified yours, and make it a more flexible utility.
If you type translate.rb, it will show you help usage. Also I made it a little rubified, no html any more. Default is from English to Spanish, since my PC does not display Chinese. It can translate from any language to any other language google supported.
[code]
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
[/code]
It will be a great utility to help me to learn Spanish, and compare the result from Spanish to French, I see they have the same Latin root!!!! :)
[code]
E:\2006_f\ex>translate.rb love
amor
E:\2006_f\ex>translate.rb love fr
amour
[/code]
Thanks again!
[/quote]
I think it might be useful for others. Copy it here.
If it violates any rule of javaeye, do whatever you ought to do.
Thanks!
[url=http://www.railscn.com/viewtopic.php?p=12892#12892] ruby版google在线翻译:)[/url]
My post there
[quote]
Good work, Thank you!
I modified yours, and make it a more flexible utility.
If you type translate.rb, it will show you help usage. Also I made it a little rubified, no html any more. Default is from English to Spanish, since my PC does not display Chinese. It can translate from any language to any other language google supported.
[code]
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
[/code]
It will be a great utility to help me to learn Spanish, and compare the result from Spanish to French, I see they have the same Latin root!!!! :)
[code]
E:\2006_f\ex>translate.rb love
amor
E:\2006_f\ex>translate.rb love fr
amour
[/code]
Thanks again!
[/quote]
I think it might be useful for others. Copy it here.
If it violates any rule of javaeye, do whatever you ought to do.
Thanks!
本文介绍了一个使用Ruby编写的Google在线翻译工具。该工具能够从一种语言翻译成另一种语言,支持Google提供的所有语言,并且可以显示从英语到西班牙语的默认翻译。文章提供了具体的Ruby代码实现,并展示了如何通过命令行进行调用。
1万+

被折叠的 条评论
为什么被折叠?



