0

i've installed google/cloud/translate in my project, the version in the Gemfile.lock is:

google-cloud-translate (2.1.0) 

With the below code:

require "google/cloud/translate" project_id = "<Project ID>" # from my Google Cloud Platform translate = Google::Cloud::Translate.new version: :v2, project_id: project_id 

That is what the documentation says and also what this answer related suggest (please note that i'm using v2 instead of v3)

RuntimeError: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information 

This part returns true:

require "google/cloud/translate" 

Update I already followed all the steps in:

https://cloud.google.com/docs/authentication/production

Created a service account, a credential key and set the env variable (on Windows), then I tried testing the credential configuration with the google/cloud/storage example and it's worked fine, but when I tried with: google/cloud/translate gem with

translate = Google::Cloud::Translate.new version: :v2, project_id: project_id

I still got the same error

What can be the error? Thanks in advance for any help.

1
  • 1
    Did you not read developers.google.com/accounts/docs/…? It's telling you to go to that page to find out why it isn't working. And when you go to that page, it gives multiple examples of how to load your credentials into your app including export GOOGLE_APPLICATION_CREDENTIALS. There are even Ruby code examples on that page to show how to do it. What am I missing here? Commented Dec 10, 2019 at 18:36

2 Answers 2

2

Hi for doing this you will need to have your .json file with all the keys of your google service account, once that you have that file in the same path of your project you could do something like the following code:

 module GoogleTranslator require "google/cloud/translate" ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "yourfile.json" class Translate attr_reader :project_id, :location_id, :word, :target_language def initialize(project_id, location_id, word, target_language) @project_id = project_id @location_id = location_id @word = word @target_language = target_language end def translate client = Google::Cloud::Translate.translation_service parent = client.location_path project: project_id, location: location_id response = client.translate_text(parent: parent, contents: word.words.to_a, target_language_code: target_language) translate_content(response) end def translate_content(response) word.translation(response) end end end class Word attr_reader :words def initialize(words) @words = words end def translation(words) words.translations.each do |word| puts word.translated_text end end end module GoogleTranslatorWrapper def self.translate(project_id:, location_id:, word:, target_language:) GoogleTranslator::Translate.new(project_id, location_id, word, target_language) end end GoogleTranslatorWrapper.translate(project_id: "your_project_id_on_google", location_id: "us-central1", word: Word.new(["your example word"]), target_language: "es-mx").translate 

Hope this can be clear :)...!

Sign up to request clarification or add additional context in comments.

2 Comments

What's this location_id? Where does that come from?
when you create a new instance of translator you need to pass it, watch: ` def initialize(project_id, location_id, word, target_language)` this means that when you need to perform as follows, GoogleTranslatorWrapper.translate(project_id: "your_project_id_on_google", location_id: "us-central1", word: Word.new(["your example word"]), target_language: "es-mx").translatethe location_id is provided by google
1
  1. Create a service account:

    gcloud iam service-accounts create rubyruby --description "rubyruby" --display-name "rubyruby" 
  2. Get the service account name:

    gcloud iam service-accounts list 
  3. Create the credential key file for your service account:

    gcloud iam service-accounts keys create key.json --iam-account [email protected] 
  4. Set the env variable:

    export GOOGLE_APPLICATION_CREDENTIALS=key.json 
  5. Enable the translate API

  6. Install the library:

    gem install google-cloud-translate 
  7. Edit the ruby.rb script

 # project_id = "Your Google Cloud project ID" # text = "The text you would like to detect the language of" require "google/cloud/translate" text = 'I am home' translate = Google::Cloud::Translate.new version: :v2, project_id: project_id detection = translate.detect text puts "'#{text}' detected as language: #{detection.language}" puts "Confidence: #{detection.confidence}" 
  1. Run the script:

    ruby ruby.rb 
  2. Output:

    'I am home' detected as language: en Confidence: 1 

2 Comments

I already followed all the steps from cloud.google.com/docs/authentication/production, created a service account, a credential key and set the env variable (on Windows), then I tried testing the credential configuration with the "google/cloud/storage" example and it's worked fine, but when I tried with "google/cloud/translate" gem with translate = Google::Cloud::Translate.new version: :v2, project_id: project_id, I still got the same error
@ManuelCarrero Go back to the page and read the whole thing. There is a dead simple example of using keyfile during your .new call to tell it to load a specific keyfile. All the answers to your question are on that page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.