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/credentials.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, project_id: nil, credentials: nil, scope: nil, retries: nil, timeout: nil, project: nil, keyfile: nil) ⇒ Google::Cloud::Translate::Api

Creates a new object for connecting to the Cloud Translation API. Each call creates a new connection.

Like other Cloud Platform services, Google Cloud Translation API supports authentication using a project ID and OAuth 2.0 credentials. In addition, it supports authentication using a public API access key. (If both the API key and the project and OAuth 2.0 credentials are provided, the API key will be used.) Instructions and configuration options are covered in the Authentication Guide.

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)

  • project_id (String)

    Project identifier for the Cloud Translation service you are connecting to. If not present, the default project for the credentials is used.

  • credentials (String, Hash, Google::Auth::Credentials)

    The path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object. (See Google::Cloud::Translate::Credentials)

  • scope (String, Array<String>)

    The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access. See Using OAuth 2.0 to Access Google APIs.

    The default scope is:

    • https://www.googleapis.com/auth/cloud-platform
  • 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.

  • project (String)

    Alias for the project_id argument. Deprecated.

  • keyfile (String)

    Alias for the credentials argument. Deprecated.

Returns:



132
133
134
135
136
137
138
139
140
# File 'lib/google-cloud-translate.rb', line 132

def self.translate key = nil, project_id: nil, credentials: nil, scope: nil,
                   retries: nil, timeout: nil, project: nil, keyfile: nil
  require "google/cloud/translate"
  Google::Cloud::Translate.new key: key, project_id: project_id,
                               credentials: credentials,
                               scope: scope, retries: retries,
                               timeout: timeout,
                               project: project, keyfile: keyfile
end

Instance Method Details

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

Creates a new object for connecting to the Cloud Translation API. Each call creates a new connection.

Like other Cloud Platform services, Google Cloud Translation API supports authentication using a project ID and OAuth 2.0 credentials. In addition, it supports authentication using a public API access key. (If both the API key and the project and OAuth 2.0 credentials are provided, the API key will be used.) Instructions and configuration options are covered in the Authentication Guide.

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)

  • scope (String, Array<String>)

    The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access. See Using OAuth 2.0 to Access Google APIs.

    The default scope is:

    • https://www.googleapis.com/auth/cloud-platform
  • 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:



72
73
74
75
76
77
# File 'lib/google-cloud-translate.rb', line 72

def translate key = nil, scope: nil, retries: nil, timeout: nil
  Google::Cloud.translate key, project_id: @project, credentials: @keyfile,
                               scope: scope,
                               retries: (retries || @retries),
                               timeout: (timeout || @timeout)
end