Class: Gcloud::Translate::Api
- Inherits:
-
Object
- Object
- Gcloud::Translate::Api
- 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.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#detect(*text) ⇒ Detection+
Detect the most likely language or languages of a text or multiple texts.
-
#initialize(service) ⇒ Api
constructor
See Gcloud.translate.
-
#languages(language = nil) ⇒ Array<Language>
List the languages supported by the API.
-
#translate(*text, to: nil, from: nil, format: nil, cid: nil) ⇒ Translation+
Returns text translations from one language to another.
Constructor Details
#initialize(service) ⇒ Api
See Gcloud.translate
61 62 63 |
# File 'lib/gcloud/translate/api.rb', line 61 def initialize service @service = service end |
Instance Attribute Details
#service ⇒ Object
55 56 57 |
# File 'lib/gcloud/translate/api.rb', line 55 def service @service end |
Instance Method Details
#detect(*text) ⇒ Detection+
Detect the most likely language or languages of a text or multiple texts.
189 190 191 192 193 194 |
# File 'lib/gcloud/translate/api.rb', line 189 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.
234 235 236 237 238 |
# File 'lib/gcloud/translate/api.rb', line 234 def languages language = nil language = language.to_s if language gapi = service.languages language Array(gapi.languages).map { |g| Language.from_gapi g } end |
#translate(*text, to: nil, from: nil, format: nil, cid: nil) ⇒ Translation+
Returns text translations from one language to another.
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/gcloud/translate/api.rb', line 139 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 text = Array(text).flatten gapi = service.translate text, to: to, from: from, format: format, cid: cid Translation.from_gapi_list gapi, text, to, from end |