Search code examples
rubygoogle-apitranslategoogle-api-client

Google-api-ruby-client Translate API Examples


I was very happy to see the google code: google-api-ruby-client project, because it meant to me that Ruby people can polish code with the Google API-s.

For now though I'm stumped because the only example given uses Buzz and from my experiments, the Google Translate (v2) api must behave quite differently to Buzz in the google-api-ruby-client.

I was intrigued by the 'Explorer' demo example -- But it isn't much of an explorer as far as I can see. All it does is call up a Buzz service and then pokes about in things it ALREADY knows about Buzz the services. To me, an explorer ought to let you 'discover' the services and the methods/functions exposed without necessarily knowing them already.

I'd love to hear of Ruby command line and desktop applications using this: google-api-ruby-client for services other than Buzz and in particular the Translate api (I'm less interested in the existing Ruby gems using the translate service at this point).

thanks ... will


Solution

  • Code for making calls to the translate API looks like this:

    require 'google/api_client'
    client = Google::APIClient.new(:key => YOUR_DEVELOPER_KEY)
    translate = client.discovered_api('translate', 'v2')
    result = client.execute(
      :api_method => translate.translations.list,
      :parameters => {
        'format' => 'text',
        'source' => 'en',
        'target' => 'es',
        'q' => 'The quick brown fox jumped over the lazy dog.'
      }
    )