Reference documentation and code samples for the Cloud Translation V2 API class Google::Cloud::Translate::V2::Api.
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.
Returns text translations from one language to another.
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 language code.
from (String) (defaults to: nil) — The source language of the text or texts. This is an ISO
639-1 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 Translation API default is :html.
model (String) (defaults to: nil) — The model used by the service to perform the translation. Can be either base to use
the Phrase-Based Machine Translation (PBMT) model, or nmt to use the Neural Machine Translation (NMT)
model. The default is nmt.
If the model is nmt, and the requested language translation pair is not supported for the NMT model,
then the request is translated using the PBMT model.
cid (String) (defaults to: nil) — The customization id for translate. This is optional.
require"google/cloud/translate/v2"translate=Google::Cloud::Translate::V2.newtranslations=translate.translate"Hello my friend.","See you soon.",from:"en",to:"la"translations.count#=> 2translations[0].text#=> "Salve amice."translations[1].text#=> "Vide te mox."
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Translation V2 API - Class Google::Cloud::Translate::V2::Api (v1.1.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.1.0 (latest)](/ruby/docs/reference/google-cloud-translate-v2/latest/Google-Cloud-Translate-V2-Api)\n- [1.0.1](/ruby/docs/reference/google-cloud-translate-v2/1.0.1/Google-Cloud-Translate-V2-Api)\n- [0.5.1](/ruby/docs/reference/google-cloud-translate-v2/0.5.1/Google-Cloud-Translate-V2-Api)\n- [0.4.1](/ruby/docs/reference/google-cloud-translate-v2/0.4.1/Google-Cloud-Translate-V2-Api)\n- [0.3.1](/ruby/docs/reference/google-cloud-translate-v2/0.3.1/Google-Cloud-Translate-V2-Api) \nReference documentation and code samples for the Cloud Translation V2 API class Google::Cloud::Translate::V2::Api.\n\nApi\n---\n\nRepresents top-level access to the Google Cloud Translation API. Translation API supports more than one\nhundred different languages, from Afrikaans to Zulu. Used in combination, this enables translation between\nthousands of language pairs. Also, you can send in HTML and receive HTML with translated text back. You don't\nneed to extract your source text or reassemble the translated content. \n\nInherits\n--------\n\n- Object\n\nExample\n-------\n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\ntranslation = translate.translate \"Hello world!\", to: \"la\"\n\ntranslation.to_s #=\u003e \"Salve mundi!\"\n\ntranslation.from #=\u003e \"en\"\ntranslation.origin #=\u003e \"Hello world!\"\ntranslation.to #=\u003e \"la\"\ntranslation.text #=\u003e \"Salve mundi!\"\n```\n\nMethods\n-------\n\n### #detect\n\n def detect(*text) -\u003e Detection, Array\u003cDetection\u003e\n\nDetect the most likely language or languages of a text or multiple texts. \n**Parameter**\n\n- **text** (String) --- The text or texts upon which language detection should be performed. \n**Returns**\n\n- ([Detection](./Google-Cloud-Translate-V2-Detection), Array\\\u003c[Detection](./Google-Cloud-Translate-V2-Detection)\\\u003e) --- A single [Detection](/ruby/docs/reference/google-cloud-translate-v2/latest/Google-Cloud-Translate-V2-Detection \"Google::Cloud::Translate::V2::Detection (class)\") object if just one text was given, or an array of [Detection](/ruby/docs/reference/google-cloud-translate-v2/latest/Google-Cloud-Translate-V2-Detection \"Google::Cloud::Translate::V2::Detection (class)\") objects if multiple texts were given.\n**Examples** \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\ndetection = translate.detect \"Hello world!\"\ndetection.language #=\u003e \"en\"\ndetection.confidence #=\u003e 0.7100697\n```\n\nDetecting multiple texts. \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\ndetections = translate.detect \"Hello world!\",\n \"Bonjour le monde!\"\ndetections.count #=\u003e 2\ndetections.first.language #=\u003e \"en\"\ndetections.first.confidence #=\u003e 0.7100697\ndetections.last.language #=\u003e \"fr\"\ndetections.last.confidence #=\u003e 0.40440267\n```\n\n### #languages\n\n def languages(language = nil) -\u003e Array\u003cLanguage\u003e\n\nList the languages supported by the API. These are the languages to and from which text can be translated. \n**Parameter**\n\n- **language** (String) --- The language and collation in which the names of the languages are returned. If this is `nil` then no names are returned. \n**Returns**\n\n- (Array\\\u003c[Language](./Google-Cloud-Translate-V2-Language)\\\u003e) --- An array of [Language](/ruby/docs/reference/google-cloud-translate-v2/latest/Google-Cloud-Translate-V2-Language \"Google::Cloud::Translate::V2::Language (class)\") objects supported by the API.\n**Examples** \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\nlanguages = translate.languages\nlanguages.count #=\u003e 104\n\nenglish = languages.detect { |l| l.code == \"en\" }\nenglish.name #=\u003e nil\n```\n\nGet all languages with their names in French. \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\nlanguages = translate.languages \"fr\"\nlanguages.count #=\u003e 104\n\nenglish = languages.detect { |l| l.code == \"en\" }\nenglish.name #=\u003e \"Anglais\"\n```\n\n### #project\n\n def project()\n\n**Alias Of** : [#project_id](./Google-Cloud-Translate-V2-Api#Google__Cloud__Translate__V2__Api_project_id_instance_) \nThe Cloud Translation API project connected to.\n**Example** \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new(\n project_id: \"my-todo-project\",\n credentials: \"/path/to/keyfile.json\"\n)\n\ntranslate.project_id #=\u003e \"my-todo-project\"\n```\n\n### #project_id\n\n def project_id()\n\n**Aliases**\n\n- [#project](./Google-Cloud-Translate-V2-Api#Google__Cloud__Translate__V2__Api_project_instance_) \nThe Cloud Translation API project connected to.\n**Example** \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new(\n project_id: \"my-todo-project\",\n credentials: \"/path/to/keyfile.json\"\n)\n\ntranslate.project_id #=\u003e \"my-todo-project\"\n```\n\n### #translate\n\n def translate(*text, to: nil, from: nil, format: nil, model: nil, cid: nil) -\u003e Translation, Array\u003cTranslation\u003e\n\nReturns text translations from one language to another. \n**Parameters**\n\n- **text** (String) --- The text or texts to translate.\n- **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](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code.\n- **from** (String) *(defaults to: nil)* --- The source language of the text or texts. This is an [ISO\n 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code. This is optional.\n- **format** (String) *(defaults to: nil)* --- The format of the text. Possible values include `:text` and `:html`. This is optional. The Translation API default is `:html`.\n- **model** (String) *(defaults to: nil)* --- The model used by the service to perform the translation. Can be either `base` to use the Phrase-Based Machine Translation (PBMT) model, or `nmt` to use the Neural Machine Translation (NMT) model. The default is `nmt`.\n\n\n If the model is `nmt`, and the requested language translation pair is not supported for the NMT model,\n then the request is translated using the PBMT model.\n- **cid** (String) *(defaults to: nil)* --- The customization id for translate. This is optional. \n**Returns**\n\n- ([Translation](./Google-Cloud-Translate-V2-Translation), Array\\\u003c[Translation](./Google-Cloud-Translate-V2-Translation)\\\u003e) --- A single [Translation](/ruby/docs/reference/google-cloud-translate-v2/latest/Google-Cloud-Translate-V2-Translation \"Google::Cloud::Translate::V2::Translation (class)\") object if just one text was given, or an array of [Translation](/ruby/docs/reference/google-cloud-translate-v2/latest/Google-Cloud-Translate-V2-Translation \"Google::Cloud::Translate::V2::Translation (class)\") objects if multiple texts were given. \n**Raises**\n\n- (ArgumentError)\n**Examples** \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\ntranslation = translate.translate \"Hello world!\", to: \"la\"\n\ntranslation.to_s #=\u003e \"Salve mundi!\"\n\ntranslation.detected? #=\u003e true\ntranslation.from #=\u003e \"en\"\ntranslation.origin #=\u003e \"Hello world!\"\ntranslation.to #=\u003e \"la\"\ntranslation.text #=\u003e \"Salve mundi!\"\ntranslation.model #=\u003e \"base\"\n```\n\nUsing the neural machine translation model: \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\ntranslation = translate.translate \"Hello world!\",\n to: \"la\", model: \"nmt\"\n\ntranslation.to_s #=\u003e \"Salve mundi!\"\ntranslation.model #=\u003e \"nmt\"\n```\n\nSetting the `from` language. \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\ntranslation = translate.translate \"Hello world!\",\n from: :en, to: :la\ntranslation.detected? #=\u003e false\ntranslation.text #=\u003e \"Salve mundi!\"\n```\n\nRetrieving multiple translations. \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\ntranslations = translate.translate \"Hello my friend.\",\n \"See you soon.\",\n from: \"en\", to: \"la\"\ntranslations.count #=\u003e 2\ntranslations[0].text #=\u003e \"Salve amice.\"\ntranslations[1].text #=\u003e \"Vide te mox.\"\n```\n\nPreserving HTML tags. \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\ntranslation = translate.translate \"\u003cstrong\u003eHello\u003c/strong\u003e world!\",\n to: :la\ntranslation.text #=\u003e \"\u003cstrong\u003eSalve\u003c/strong\u003e mundi!\"\n```"]]