It seems like the more I watch the Mountain West sessions the more I stumble across solutions to itches I've had recently. Just yesterday at work I was hacking with a friend on a simple CLI for Google Translate and we were struggling a bit getting 'curb' to do exactly what we wanted. After watching James Britt's session on Black-boxing with Ruby I realized that mechanize would probably be a better choice for what we were trying to do. 10 Minutes later:
require 'rubygems'
require 'mechanize'
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Linux Mozilla'
page = agent.get("http://translate.google.com/translate_t?langpair=en|#{ARGV.shift}")
translate_form = page.forms[0]
translate_form.text = ARGV.join(' ')
translate_form.ie = "UTF8"
translate_form.hl = "en"
results = agent.submit(translate_form)
puts Hpricot(results.body).search("//div[@id='result_box']")[0].to_plain_text
# Examples
ruby translate.rb zh-CN "I love to eat tacos on Tuesdays" => 我爱吃tacos周二
ruby translate.rb es "I love to eat tacos on Tuesdays" => Amo comer el tacos el martes
While these may not be accurate translations, this script definitely works for testing out i18n functionality until the real translations come in. If and whenever Google decides to release an API for their site it will be painless to throw this script away.
Subscribe to:
Post Comments (Atom)
Blog Archive
About Me
Tags
- activerecord (1)
- camping (1)
- closures (1)
- cygwin (1)
- denver (1)
- drb (1)
- dsl (1)
- erlang (1)
- erlounge (1)
- fedora (8)
- freedom (1)
- FUDcon (1)
- GEB (3)
- git (1)
- gnu (1)
- gstreamer (1)
- hotmail (1)
- hpricot (1)
- java (1)
- jruby (1)
- kernel (1)
- kino (3)
- laptop (1)
- linux (3)
- machanize (1)
- meta programming (3)
- metaprogramming (1)
- mutt (2)
- open source (3)
- oss (1)
- pitivi (2)
- raleigh.rb (1)
- ruby (13)
- ruby curb hpricot (1)
- rubyconf (1)
- slashdot (1)
- svn (1)
- tux (1)
- ubuntu (1)
- unix (1)
- vim (1)
- vnc (1)
- windows (3)
No comments:
Post a Comment