Quick Start: Google Translation API in Rails

nodefiend

chowderhead

Posted on October 5, 2018

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: https://cloud.google.com/ruby/docs/setup

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!

application.rb

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

Obtain a project_id & json key from google : https://cloud.google.com/docs/authentication/production?authuser=1#auth-cloud-implicit-ruby

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

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)

google_cloud.json

{
  "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": "random@random.iam.gserviceaccount.com",
  "client_id": "111111111111111",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/random%random.iam.gserviceaccount.com"
}
Enter fullscreen mode Exit fullscreen mode

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

.gitignore

/config/local_env.yml
/config/google_cloud.json
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(File.open(env_file)).each do |key, value|
    ENV[key.to_s] = value
  end if File.exists?(env_file)
end
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 = Google::Cloud::Translate.new 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 https://console.developers.google.com/apis/api/storage-api.googleapis.com/overview?project=1231214123123 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 = Google::Cloud::Translate.new project: project_id
languages = translate.languages language.code

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

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

sources:
GOOGLE TRANSLATION CLOUTH AUTH:
https://cloud.google.com/docs/authentication/production?authuser=1#auth-cloud-implicit-ruby

SETTING LOCAL KEYS IN RAILS:
https://qiita.com/alokrawat050/items/0d7791b3915579f95791

💖 💪 🙅 🚩
nodefiend
chowderhead

Posted on October 5, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related