rubybook = Blog.new :title => “Gerald Bauer’s Ruby Notebook”, :about => “Internet Professional in Vancouver, B.C.”

Gerald Bauer’s Ruby Notebook

How to use Google’s Translation API with Ruby – Translate ‘Hello, World’ into 25+ Languages with Ruby in 10 Minutes

Google offers a free translation service as a “classic” web form or as a programmable web service that lets you translate text or web pages into more than twenty-five languages.

Let’s build a Ruby wrapper using Google’s Translation API and let’s translate “Hello, World” into 25+ languages.

To use Google’s Translation API send a plain old HTTP get request to ajax.googleapis.com/ajax/services/language/translate and include 1) the language pair (the language to translate from and to two-letter-encoded and separated by a pipe e.g. en|de – English to German), 2) the text and 3) the API version (that is, 1.0) as query parameters and Google’s Translation service will send you back the JSON-encoded result.

Example Request: Translate ‘Hello World’ from English to German

http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=en|de&q=Hello+World

Example Response (JSON-Encoded):

{"responseData"=>{"translatedText"=>"Hallo Welt"},
 "responseStatus"=>200,
 "responseDetails"=>nil}

Now let’s send the request with Ruby using the net/http standard library. Note, you can grab the sources for this article from the Vancouver.rb repository.

def translate( text, to, from='en' )

  base = 'http://ajax.googleapis.com/ajax/services/language/translate' 

  # assemble query params
  params = {
    :langpair => "#{from}|#{to}",
    :q => text,
    :v => 1.0
  }

  query = params.map{ |k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')

  # send request
  response = Net::HTTP.get_response( URI.parse( "#{base}?#{query}" ) )

  ... # to be continued
end

And let’s get the translated text from the JSON-encoded response:

def translate( text, to, from='en' )  # continued
  ...

  json = JSON.parse( response.body )

  if json['responseStatus'] == 200
    json['responseData']['translatedText']
  else
    raise StandardError, response['responseDetails']
  end
end

That’s it. Now you can use the Google translation service by using the new translate method in Ruby.

Example 1: Translate ‘Hello World’ to German (de) from English (en)

text_de = translate( 'Hello World', 'de' )
=> 'Hallo Welt'

Example 2: Translate ‘Hallo Welt’ to Spanish (es) from German (de)

text_es = translate( 'Hallo Welt', 'es', 'de')
=> 'Hola Mundo'

If you try it on more languages:

languages = %W{ar bg ca cs da de el en es fr hi id it ja ko pt-PT sk sr sv vi zh-CN} 

languages.each do |lang|
  puts translate( 'Hello, World', lang  )
end

You will get:

مرحبا ، العالمية Здравей, Свят Hola Món Dobrý den, Světová Hej Verden Hallo, Welt Γεια σας, Παγκόσμια Hello, World Hola, Mundo Bonjour, monde हैलो, विश्व Halo, Dunia Ciao, Mondo こんにちは、世界 Olá, Mundo Dobrý deň, Svetová Хелло, Уорлд Hej Världen Xin chào, thế giới 哈罗,世界!

References:

Alternatives in Ruby:

Questions? Comments? Corrections? Send them along to the Vancouver.rb forum/mailing list. Thanks!

Written by Gerald

March 3, 2009 at 6:43 pm