DEV Community

Cover image for Quick Start: Google Translation API in Rails

Posted on

Quick Start: Google Translation API in Rails

Photo cred: Alexander Smagin

first run in the directory

//NOTE we need to have google cloud dependency library installed to run google cloud-translate
Enter fullscreen mode Exit fullscreen mode

gem install google-cloud
gem install google-cloud-translate
bundle add google-cloud-translate

Read here for information about setting up google cloud api for your rails app:

Setting up authentication:

To run the client library, you must first set up authentication by creating a service account and setting the appropriate environment variables.

cloud docs Authentication

Lets get into it!


# Imports the Google Cloud client library
require "google/cloud/translate"
Enter fullscreen mode Exit fullscreen mode

Obtain a project_id & json key from google :

you will get your project_id this page, as well as download a copy of the json key you will need

now that you have your key create this file config/local_env.yml


CLOUD_PROJECT_ID: 'your_project_id'
GOOGLE_APPLICATION_CREDENTIALS: 'config/google_cloud.json'
Enter fullscreen mode Exit fullscreen mode

now create a google_cloud.json file inside of /config and paste the json file that you received from google into it .

(it will look something like this)


  "type": "service_account",
  "project_id": "randomApp",
  "private_key_id": "04e0c8e8470",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBg...p0Tg7WibPz76wqUFpGj/qshvY2pqFc2H94\nxkgZT44GHXagW5WOW5ofXJo=\n-----END PRIVATE KEY-----\n",
  "client_email": "",
  "client_id": "111111111111111",
  "auth_uri": "",
  "token_uri": "",
  "auth_provider_x509_cert_url": "",
  "client_x509_cert_url": ""
Enter fullscreen mode Exit fullscreen mode

Now we need to add our new enviornment file as well as our JSON key into .gitignore


Enter fullscreen mode Exit fullscreen mode

Now we have to setup the rails app to read this environment file:

config.before_configuration do
  env_file = File.join(Rails.root, 'config', 'local_env.yml')
  YAML.load( do |key, value|
    ENV[key.to_s] = value
  end if File.exists?(env_file)
Enter fullscreen mode Exit fullscreen mode

Hello world with Google Translate

# Your Google Cloud Platform project ID
project_id = ENV["CLOUD_PROJECT_ID"]

# Instantiates a client
translate = project: project_id

# The text to translate
text = "Hello, world!"
# The target language
target = "ru"

# Translates some text into Russian
translation = translate.translate text, to: target

puts "Text: #{text}"
puts "Translation: #{translation}"
Enter fullscreen mode Exit fullscreen mode

When you try to run your rails server you might get :

accessNotConfigured: Access Not Configured. Cloud Storage JSON API has not been used in project 12314123123123 before or it is disabled. Enable it by visiting then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. (Google::Cloud::PermissionDeniedError)
Enter fullscreen mode Exit fullscreen mode

make sure that it is enabled in your developer console just like the error says to do

if you get :

`rescue in execute': invalid: Unknown project id: 0 (Google::Cloud::InvalidArgumentError)
Enter fullscreen mode Exit fullscreen mode

you might have the wrong project_id set for your application

check the config/google_cloud.json file for your project_id it will be in there

project_id = ENV["CLOUD_PROJECT_ID"]
language_code = 'en'

translate = project: project_id
languages = translate.languages language.code

 puts 'Supported languages:'
 languages.each do |language|
  puts '#{language.code} #{}'
Enter fullscreen mode Exit fullscreen mode

Great! now our rails server should be up and running with google cloud translation api !



Top comments (5)

davisondaisy profile image

The Google Translate API is presently in its second Cheap Custom Essay form. As a major aspect of Google's bigger Cloud Machine Learning API family, the Translate API can translate text between a huge number of language sets.

sharpthomas profile image

As Google Translate API has now propelled, I glanced around for a gem we could rapidly connect and do my dissertation for me was shocked to discover just the google-api-ruby-customer which for our necessities appeared to accompany a great deal of dead weight.

fauxncotton profile image

Its really best article i have seen today, I appreciated your work and experience. Ladies Brown Blazer I would love to share your article to other networks. Thanks for sharing!

nickhun84195020 profile image
Nick Hunter • Edited

Wow. This is an outstanding article regarding a quick start with google translation API. I want to know more about this topic. I quickly learned how to enable the API rail server to access Google API.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.