DEV Community

Cover image for Quick Start: Google Translation API in Rails

Quick Start: Google Translation API in Rails

nodefiend profile image chowderhead ・3 min read

Photo cred: Alexander Smagin

first run in the directory

//NOTE we need to have google cloud dependency library installed to run google cloud-translate

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"

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'

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": ""

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



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)

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}"

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)

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)

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} #{}'

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




Editor guide
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!

pubgapk2 profile image


ankitsaini80772 profile image
ankit saini

Here I get the best conclusion for the make secure of the fixes connection to bluetooth audios device and wirelesses displays in windows 10 and with this all type fo the audio and video option you can choose every time.

sarasmith077 profile image

The Google API is the best interface to do mail, share the documents and many more things. I personally use it to complete my work and I can share and do coursework writing online through it. I will enable the API rail server to get access to Google API...

ninonurmadi profile image

Hello Dear , Visit Our Blog, Thanks you For Sharing , Make Share It And Keep Going To