`
roseanne
  • 浏览: 2651 次
  • 性别: Icon_minigender_2
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

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

阅读更多
See original post by killme2008 from the following link

ruby版google在线翻译:)

My post there
引用

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.

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 


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!!!!


E:\2006_f\ex>translate.rb love
amor
E:\2006_f\ex>translate.rb love fr
amour


Thanks again!


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!
分享到:
评论
1 楼 boin 2006-11-19  
thanks for sharing .. it's awesome

相关推荐

Global site tag (gtag.js) - Google Analytics