A little Language Utility stealed from killme2008's work :)

本文介绍了一个使用Ruby编写的Google在线翻译工具。该工具能够从一种语言翻译成另一种语言,支持Google提供的所有语言,并且可以显示从英语到西班牙语的默认翻译。文章提供了具体的Ruby代码实现,并展示了如何通过命令行进行调用。
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!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值