Module: Gcloud::Translate
- Defined in:
- lib/gcloud/translate.rb,
lib/gcloud/translate/api.rb,
lib/gcloud/translate/errors.rb,
lib/gcloud/translate/language.rb,
lib/gcloud/translate/detection.rb,
lib/gcloud/translate/connection.rb,
lib/gcloud/translate/translation.rb
Overview
# Google Translate API
[Google Translate API](cloud.google.com/translate/) 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 Translate API for fast, dynamic translation of source text. Language detection is also available in cases where the source language is unknown.
Translate API supports more than ninety 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.
## Authenticating
Unlike other Cloud Platform services, which authenticate using a project ID and OAuth 2.0 credentials, Translate API requires a public API access key. (This may change in future releases of Translate API.) 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).
## Translating texts
Translating text from one language to another is easy (and extremely fast.) The only required arguments to Api#translate are a string and the [ISO 639-1](en.wikipedia.org/wiki/List_of_ISO_639-1_codes) code of the language to which you wish to translate.
“‘ruby 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!” “‘
You may want to use the from option to specify the language of the source text, as the following example illustrates. (Single words do not give Translate API much to work with.)
“‘ruby require “gcloud”
gcloud = Gcloud.new translate = gcloud.translate
translation = translate.translate “chat”, to: “en”
translation.detected? #=> true translation.from #=> “en” translation.text #=> “chat”
translation = translate.translate “chat”, from: “fr”, to: “en”
translation.detected? #=> false translation.from #=> “fr” translation.text #=> “cat” “‘
You can pass multiple texts to Api#translate.
“‘ruby require “gcloud”
gcloud = Gcloud.new translate = gcloud.translate
translations = translate.translate “chien”, “chat”, from: “fr”, to: “en”
translations.size #=> 2 translations.origin #=> “chien” translations.text #=> “dog” translations.origin #=> “chat” translations.text #=> “cat” “‘
By default, any HTML in your source text will be preserved.
“‘ruby require “gcloud”
gcloud = Gcloud.new translate = gcloud.translate
translation = translate.translate “<strong>Hello</strong> world!”,
to: :la
translation.text #=> “<strong>Salve</strong> mundi!” “‘
## Detecting languages
You can use Api#detect to see which language the Translate API ranks as the most likely source language for a text. The confidence score is a float value between 0 and 1.
“‘ruby require “gcloud”
gcloud = Gcloud.new translate = gcloud.translate
detection = translate.detect “chat”
detection.text #=> “chat” detection.language #=> “en” detection.confidence #=> 0.59922177 “‘
You can pass multiple texts to Api#detect.
“‘ruby require “gcloud”
gcloud = Gcloud.new translate = gcloud.translate
detections = translate.detect “chien”, “chat”
detections.size #=> 2 detections.text #=> “chien” detections.language #=> “fr” detections.confidence #=> 0.7109375 detections.text #=> “chat” detections.language #=> “en” detections.confidence #=> 0.59922177 “‘
## Listing supported languages
Translate API adds new languages frequently. You can use Api#languages to query the list of supported languages.
“‘ruby require “gcloud”
gcloud = Gcloud.new translate = gcloud.translate
languages = translate.languages
languages.size #=> 104 languages.code #=> “af” languages.name #=> nil “‘
To receive the names of the supported languages, as well as their [ISO 639-1](en.wikipedia.org/wiki/List_of_ISO_639-1_codes) codes, provide the code for the language in which you wish to receive the names.
“‘ruby require “gcloud”
gcloud = Gcloud.new translate = gcloud.translate
languages = translate.languages “en”
languages.size #=> 104 languages.code #=> “af” languages.name #=> “Afrikaans” “‘
## Configuring Backoff
The Backoff class allows users to globally configure how Cloud API requests are automatically retried in the case of some errors, such as a 500 or 503 status code, or a specific internal error code such as rateLimitExceeded.
If an API call fails, the response will be inspected to see if the call should be retried. If the response matches the criteria, then the request will be retried after a delay. If another error occurs, the delay will be increased incrementally before a subsequent attempt. The first retry will be delayed one second, the second retry two seconds, and so on.
“‘ruby require “gcloud” require “gcloud/backoff”
Gcloud::Backoff.retries = 5 # Raise the maximum number of retries from 3 “‘
Defined Under Namespace
Classes: Api, ApiError, Connection, Detection, Error, Language, Translation