Class: Google::Cloud::Translate::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/translate/api.rb

Overview

Api

Represents top-level access to the Google Cloud Translation API. 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.

Examples:

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

translation = translate.translate "Hello world!", to: "la"

translation.to_s #=> "Salve mundi!"

translation.from #=> "en"
translation.origin #=> "Hello world!"
translation.to #=> "la"
translation.text #=> "Salve mundi!"

See Also:

Instance Method Summary collapse

Instance Method Details

#detect(*text) ⇒ Detection+

Detect the most likely language or languages of a text or multiple texts.

Examples:

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

detection = translate.detect "Hello world!"
detection.language #=> "en"
detection.confidence #=> 0.7100697

Detecting multiple texts.

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

detections = translate.detect "Hello world!",
                              "Bonjour le monde!"
detections.count #=> 2
detections.first.language #=> "en"
detections.first.confidence #=> 0.7100697
detections.last.language #=> "fr"
detections.last.confidence #=> 0.40440267

Parameters:

  • text (String)

    The text or texts upon which language detection should be performed.

Returns:

See Also:



234
235
236
237
238
239
# File 'lib/google/cloud/translate/api.rb', line 234

def detect *text
  return nil if text.empty?
  text = Array(text).flatten
  gapi = service.detect(text)
  Detection.from_gapi gapi, text
end

#languages(language = nil) ⇒ Array<Language>

List the languages supported by the API. These are the languages to and from which text can be translated.

Examples:

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

languages = translate.languages
languages.count #=> 104

english = languages.detect { |l| l.code == "en" }
english.name #=> nil

Get all languages with their names in French.

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

languages = translate.languages "fr"
languages.count #=> 104

english = languages.detect { |l| l.code == "en" }
english.name #=> "Anglais"

Parameters:

  • language (String) (defaults to: nil)

    The language and collation in which the names of the languages are returned. If this is nil then no names are returned.

Returns:

See Also:



277
278
279
280
281
# File 'lib/google/cloud/translate/api.rb', line 277

def languages language = nil
  language = language.to_s if language
  gapi = service.languages language
  Array(gapi["languages"]).map { |g| Language.from_gapi g }
end

#projectObject

The Cloud Translation API project connected to.

Examples:

require "google/cloud/translate"

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

translate.project #=> "my-todo-project"


78
79
80
# File 'lib/google/cloud/translate/api.rb', line 78

def project
  service.project
end

#translate(*text, to: nil, from: nil, format: nil, model: nil, cid: nil) ⇒ Translation+

Returns text translations from one language to another.

Examples:

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

translation = translate.translate "Hello world!", to: "la"

translation.to_s #=> "Salve mundi!"

translation.detected? #=> true
translation.from #=> "en"
translation.origin #=> "Hello world!"
translation.to #=> "la"
translation.text #=> "Salve mundi!"
translation.model #=> "base"

Using the neural machine translation model:

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

translation = translate.translate "Hello world!",
                                  to: "la", model: "nmt"

translation.to_s #=> "Salve mundi!"
translation.model #=> "nmt"

Setting the from language.

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

translation = translate.translate "Hello world!",
                                  from: :en, to: :la
translation.detected? #=> false
translation.text #=> "Salve mundi!"

Retrieving multiple translations.

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

translations = translate.translate "Hello my friend.",
                                   "See you soon.",
                                   from: "en", to: "la"
translations.count #=> 2
translations[0].text #=> "Salve amice."
translations[1].text #=> "Vide te mox."

Preserving HTML tags.

require "google/cloud/translate"

translate = Google::Cloud::Translate.new

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

Parameters:

  • text (String)

    The text or texts to translate.

  • to (String)

    The target language into which the text should be translated. This is required. The value must be an ISO 639-1 language code.

  • from (String)

    The source language of the text or texts. This is an ISO 639-1 language code. This is optional.

  • format (String)

    The format of the text. Possible values include :text and :html. This is optional. The Translation API default is :html.

  • model (String)

    The model used by the service to perform the translation. The neural machine translation model (nmt) is billed as a premium edition feature. If this is set to base, then the service will return translation using the current standard model. The default value is base.

    Acceptable values are:

    • nmt - Use the neural machine translation model
    • base - Use the current standard model
  • cid (String)

    The customization id for translate. This is optional.

Returns:

See Also:



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/google/cloud/translate/api.rb', line 185

def translate *text, to: nil, from: nil, format: nil, model: nil,
              cid: nil
  return nil if text.empty?
  fail ArgumentError, "to is required" if to.nil?
  to = to.to_s
  from = from.to_s if from
  format = format.to_s if format
  text = Array(text).flatten
  gapi = service.translate text, to: to, from: from,
                                 format: format, model: model, cid: cid
  Translation.from_gapi_list gapi, text, to, from
end