Module: Google::Cloud

Defined in:
lib/google-cloud-translate.rb,
lib/google/cloud/translate.rb,
lib/google/cloud/translate/api.rb,
lib/google/cloud/translate/service.rb,
lib/google/cloud/translate/version.rb,
lib/google/cloud/translate/language.rb,
lib/google/cloud/translate/detection.rb,
lib/google/cloud/translate/translation.rb

Defined Under Namespace

Modules: Translate

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.translate(key = nil, retries: nil, timeout: nil) ⇒ Google::Cloud::Translate::Api

Creates a new object for connecting to the Translate service. Each call creates a new connection.

Unlike other Cloud Platform services, which authenticate using a project ID and OAuth 2.0 credentials, Google Translate API requires a public API access key. (This may change in future releases of Google Translate API.) Follow the general instructions at Identifying your application to Google, and the specific instructions for Server keys.

Examples:

require "google/cloud"

translate = Google::Cloud.translate "api-key-abc123XYZ789"

translation = translate.translate "Hello world!", to: "la"
translation.text #=> "Salve mundi!"

Using API Key from the environment variable.

require "google/cloud"

ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"

translate = Google::Cloud.translate

translation = translate.translate "Hello world!", to: "la"
translation.text #=> "Salve mundi!"

Parameters:

  • key (String) (defaults to: nil)

    a public API access key (not an OAuth 2.0 token)

  • retries (Integer)

    Number of times to retry requests on server error. The default value is 3. Optional.

  • timeout (Integer)

    Default timeout to use in requests. Optional.

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/google-cloud-translate.rb', line 107

def self.translate key = nil, retries: nil, timeout: nil
  require "google/cloud/translate"
  key ||= ENV["TRANSLATE_KEY"]
  key ||= ENV["GOOGLE_CLOUD_KEY"]
  if key.nil?
    key_missing_msg = "An API key is required to use the Translate API."
    fail ArgumentError, key_missing_msg
  end

  Google::Cloud::Translate::Api.new(
    Google::Cloud::Translate::Service.new(
      key, retries: retries, timeout: timeout))
end

Instance Method Details

#translate(key = nil, retries: nil, timeout: nil) ⇒ Google::Cloud::Translate::Api

Creates a new object for connecting to the Translate service. Each call creates a new connection.

Unlike other Cloud Platform services, which authenticate using a project ID and OAuth 2.0 credentials, Google Translate API requires a public API access key. (This may change in future releases of Google Translate API.) Follow the general instructions at Identifying your application to Google, and the specific instructions for Server keys.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
translate = gcloud.translate "api-key-abc123XYZ789"

translation = translate.translate "Hello world!", to: "la"
translation.text #=> "Salve mundi!"

Using API Key from the environment variable.

require "google/cloud"

ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"

gcloud = Google::Cloud.new
translate = gcloud.translate

translation = translate.translate "Hello world!", to: "la"
translation.text #=> "Salve mundi!"

Parameters:

  • key (String) (defaults to: nil)

    a public API access key (not an OAuth 2.0 token)

  • retries (Integer)

    Number of times to retry requests on server error. The default value is 3. Optional.

  • timeout (Integer)

    Default timeout to use in requests. Optional.

Returns:



65
66
67
68
# File 'lib/google-cloud-translate.rb', line 65

def translate key = nil, retries: nil, timeout: nil
  Google::Cloud.translate key, retries: (retries || @retries),
                               timeout: (timeout || @timeout)
end