Module: Google::Cloud::Translate

Defined in:
lib/google/cloud/translate.rb,
lib/google/cloud/translate/v2.rb,
lib/google/cloud/translate/v3.rb,
lib/google/cloud/translate/v2/api.rb,
lib/google/cloud/translate/version.rb,
lib/google/cloud/translate/v2/service.rb,
lib/google/cloud/translate/v2/language.rb,
lib/google/cloud/translate/v2/detection.rb,
lib/google/cloud/translate/v2/credentials.rb,
lib/google/cloud/translate/v2/translation.rb,
lib/google/cloud/translate/v3/credentials.rb,
lib/google/cloud/translate/v3/translation_service_client.rb,
lib/google/cloud/translate/v3/doc/google/cloud/translate/v3/translation_service.rb

Overview

Google Cloud Translation API

Google Cloud Translation API provides a simple, programmatic interface for translating an arbitrary string into any supported language. It is highly responsive, so websites and applications can integrate with Translation API for fast, dynamic translation of source text. Language detection is also available in cases where the source language is unknown.

Translation API supports more than one hundred different languages, from Afrikaans to Zulu. Used in combination, this enables translation between thousands of language pairs. Also, you can send in HTML and receive HTML with translated text back. You don't need to extract your source text or reassemble the translated content.

The google-cloud-translate 2.0 gem contains a generated v3 client and a legacy hand-written v2 client. To use the legacy v2 client, call Translate.new and specify version: :v2. See Migrating to Translation v3 for details regarding differences between v2 and v3.

See Translation Overview.

Defined Under Namespace

Modules: V2, V3

Constant Summary collapse

FILE_DIR =
File.realdirpath Pathname.new(__FILE__).join("..").join("translate")
AVAILABLE_VERSIONS =
Dir["#{FILE_DIR}/*"]
.select { |file| File.directory? file }
.select { |dir| Google::Gax::VERSION_MATCHER.match File.basename(dir) }
.select { |dir| File.exist? dir + ".rb" }
.map { |dir| File.basename dir }
VERSION =
"2.1.0".freeze

Class Method Summary collapse

Class Method Details

.configure {|Google::Cloud.configure.translate| ... } ⇒ Google::Cloud::Config

Note:

These values are only used by the legacy v2 client.

Configure the Google Cloud Translate library.

The following Translate configuration parameters are supported:

  • project_id - (String) Identifier for a Translate project.
  • 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::V2::Credentials)
  • scope - (String, Array) The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access.
  • retries - (Integer) Number of times to retry requests on server error.
  • timeout - (Integer) Default timeout to use in requests.
  • endpoint - (String) Override of the endpoint host name, or nil to use the default endpoint.

Yields:

Returns:

  • (Google::Cloud::Config)

    The configuration object the Google::Cloud::Translate library uses.



207
208
209
210
211
# File 'lib/google/cloud/translate.rb', line 207

def self.configure
  yield Google::Cloud.configure.translate if block_given?

  Google::Cloud.configure.translate
end

.new(version: , credentials: , scopes: , client_config: , timeout: ) ⇒ Object .new(version: , project_id: , credentials: , key: , scope: , retries: , timeout: , endpoint: ) ⇒ Object

Provides natural language translation operations.

Examples:

Using the v3 client.

require "google/cloud/translate"

client = Google::Cloud::Translate.new

project_id = "my-project-id"
location_id = "us-central1"
model_id = "my-automl-model-id"

# The `model` type requested for this translation.
model = "projects/#{project_id}/locations/#{location_id}/models/#{model_id}"
# The content to translate in string format
contents = ["Hello, world!"]
# Required. The BCP-47 language code to use for translation.
target_language = "fr"
# Optional. The BCP-47 language code of the input text.
source_language = "en"
# Optional. Can be "text/plain" or "text/html".
mime_type = "text/plain"
parent = client.class.location_path project_id, location_id

response = client.translate_text contents, target_language, parent,
  source_language_code: source_language, model: model, mime_type: mime_type

# Display the translation for each input text provided
response.translations.each do |translation|
  puts "Translated text: #{translation.translated_text}"
end

Using the legacy v2 client.

require "google/cloud/translate"

translate = Google::Cloud::Translate.new(
  version: :v2,
  project_id: "my-todo-project",
  credentials: "/path/to/keyfile.json"
)

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

Using the legacy v2 client with an API Key.

require "google/cloud/translate"

translate = Google::Cloud::Translate.new(
  version: :v2,
  key: "api-key-abc123XYZ789"
)

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

Using API Key from the environment variable.

require "google/cloud/translate"

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

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

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

Overloads:

  • .new(version: , credentials: , scopes: , client_config: , timeout: ) ⇒ Object

    Parameters:

    • version (Symbol, String)

      The major version of the service to be used. By default :v3 is used.

    • credentials (Google::Auth::Credentials, String, Hash, GRPC::Core::Channel, GRPC::Core::ChannelCredentials, Proc)

      Provides the means for authenticating requests made by the client. This parameter can be many types. A Google::Auth::Credentials uses a the properties of its represented keyfile for authenticating requests made by this client. A String will be treated as the path to the keyfile to be used for the construction of credentials for this client. A Hash will be treated as the contents of a keyfile to be used for the construction of credentials for this client. A GRPC::Core::Channel will be used to make calls through. A GRPC::Core::ChannelCredentials for the setting up the RPC client. The channel credentials should already be composed with a GRPC::Core::CallCredentials object. A Proc will be used as an updater_proc for the Grpc channel. The proc transforms the metadata for requests, generally, to give OAuth credentials.

    • scopes (Array<String>)

      The OAuth scopes for this service. This parameter is ignored if an updater_proc is supplied.

    • client_config (Hash)

      A Hash for call options for each method. See Google::Gax#construct_settings for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points.

    • timeout (Numeric)

      The default timeout, in seconds, for calls made through this client.

    • metadata (Hash)

      Default metadata to be sent with each request. This can be overridden on a per call basis.

    • service_address (String)

      Override for the service hostname, or nil to leave as the default.

    • service_port (Integer)

      Override for the service port, or nil to leave as the default.

    • exception_transformer (Proc)

      An optional proc that intercepts any exceptions raised during an API call to inject custom error handling.

  • .new(version: , project_id: , credentials: , key: , scope: , retries: , timeout: , endpoint: ) ⇒ Object

    Parameters:

    • version (Symbol, String)

      The major version of the service to be used. Specifying :v2 will return the legacy client.

    • 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::V2::Credentials)

    • key (String)

      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.

    • endpoint (String)

      Override of the endpoint host name. Optional. If the param is nil, uses the default endpoint.



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/google/cloud/translate.rb', line 174

def self.new *args, version: :v3, **kwargs
  unless AVAILABLE_VERSIONS.include? version.to_s.downcase
    raise "The version: #{version} is not available. The available versions " \
      "are: [#{AVAILABLE_VERSIONS.join ', '}]"
  end

  require "#{FILE_DIR}/#{version.to_s.downcase}"
  version_module = Google::Cloud::Translate
                   .constants
                   .select { |sym| sym.to_s.casecmp(version.to_s).zero? }
                   .first
  Google::Cloud::Translate.const_get(version_module).new(*args, **kwargs)
end