Class: Gcloud::Translate::Api

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

Overview

# Api

Represents top-level access to the Google Translate API. Each instance requires a public API access key. To create a key, follow the general instructions at [Identifying your application to Google](cloud.google.com/translate/v2/using_rest#auth), and the specific instructions for [Server keys](cloud.google.com/translate/v2/using_rest#creating-server-api-keys). See Gcloud#translate.

Examples:

require "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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

puts translation #=> Salve mundi!

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

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Api



62
63
64
65
66
67
68
69
# File 'lib/gcloud/translate/api.rb', line 62

def initialize key
  key ||= ENV["TRANSLATE_KEY"]
  if key.nil?
    key_mising_msg = "An API key is required to use the Translate API."
    fail ArgumentError, key_mising_msg
  end
  @connection = Connection.new key
end

Instance Attribute Details

#connectionObject



56
57
58
# File 'lib/gcloud/translate/api.rb', line 56

def connection
  @connection
end

Instance Method Details

#detect(*text) ⇒ Detection+

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

Examples:

require "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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

Detecting multiple texts.

require "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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

Parameters:

  • text (String)

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

Returns:

See Also:



195
196
197
198
199
200
# File 'lib/gcloud/translate/api.rb', line 195

def detect *text
  return nil if text.empty?
  resp = connection.detect(*text)
  fail ApiError.from_response(resp) unless resp.success?
  Detection.from_response resp, 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 "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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 "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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:



240
241
242
243
244
245
# File 'lib/gcloud/translate/api.rb', line 240

def languages language = nil
  language = language.to_s if language
  resp = connection.languages language
  fail ApiError.from_response(resp) unless resp.success?
  Array(resp.data["languages"]).map { |gapi| Language.from_gapi gapi }
end

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

Returns text translations from one language to another.

Examples:

require "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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

puts translation #=> Salve mundi!

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

Setting the ‘from` language.

require "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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

Retrieving multiple translations.

require "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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 "gcloud"

gcloud = Gcloud.new
translate = gcloud.translate

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) (defaults to: nil)

    The target language into which the text should be translated. This is required. The value must be an [ISO 639-1](en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code.

  • from (String) (defaults to: nil)

    The source language of the text or texts. This is an [ISO 639-1](en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code. This is optional.

  • format (String) (defaults to: nil)

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

  • cid (String) (defaults to: nil)

    The customization id for translate. This is optional.

Returns:

See Also:



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/gcloud/translate/api.rb', line 145

def translate *text, to: nil, from: nil, format: 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
  resp = connection.translate(*text, to: to, from: from,
                                     format: format, cid: cid)
  fail ApiError.from_response(resp) unless resp.success?
  Translation.from_response resp, text, to, from
end