Archive for January, 2008

Translate directly at your console!

Monday, January 7th, 2008

Ever wanted to be able to translate directly at your cosole?

[hans@zommuntu:~/rails]$ de2en häuser
Homes
[hans@zommuntu:~/rails]$ en2de houses
Häuser

Save this ruby-script as en2de in your ~/bin:

#! /usr/bin/ruby

#
# Copyright (c) 2008 gauda.de
# All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Ruby itself.
#

require 'net/http'
require 'iconv'
require 'uri'

arg = URI.escape(Iconv.new('ISO-8859-15', 'UTF-8').iconv(ARGV[0]))
str = Net::HTTP.new('www.google.com', 80).get("/translate_t?text=" + arg + "&langpair=de|en").body.match(/id\=result_box\ dir\="ltr">(\S*)<\/div>/)
if str
  puts Iconv.new('UTF-8', 'ISO-8859-15').iconv(str[1])
else
  puts "error.."
end

Of course you can change the languages! Just edit the langpair=de|en at your will. Be sure to have ruby installed!