Reference documentation and code samples for the Cloud Translation V2 API module Google::Cloud::Translate::V2.
Google Cloud Translation API
Google Cloud Translation API
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 Translation API for fast, dynamic
translation of source text. Language detection is also available in cases
where the source language is unknown.
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.
Creates a new object for connecting to Cloud Translation API. Each call creates a new connection.
Like other Cloud Platform services, Google Cloud Translation API supports authentication using a project ID
and OAuth 2.0 credentials. In addition, it supports authentication using a public API access key. (If both the
API key and the project and OAuth 2.0 credentials are provided, the API key will be used.) Instructions and
configuration options are covered in the Authentication Guide.
Parameters
project_id (String) (defaults to: nil) — Project identifier for the Cloud Translation service you are connecting to. If not
present, the default project for the credentials is used.
credentials (String, Hash, Google::Auth::Credentials) (defaults to: nil) — The path to the keyfile as a String, the contents
of the keyfile as a Hash, or a Google::Auth::Credentials object. (See Credentials)
key (String) (defaults to: nil) — a public API access key (not an OAuth 2.0 token)
scope (String, Array<String>) (defaults to: nil) —
[[["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 - Module Google::Cloud::Translate::V2 (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)\n- [1.0.1](/ruby/docs/reference/google-cloud-translate-v2/1.0.1/Google-Cloud-Translate-V2)\n- [0.5.1](/ruby/docs/reference/google-cloud-translate-v2/0.5.1/Google-Cloud-Translate-V2)\n- [0.4.1](/ruby/docs/reference/google-cloud-translate-v2/0.4.1/Google-Cloud-Translate-V2)\n- [0.3.1](/ruby/docs/reference/google-cloud-translate-v2/0.3.1/Google-Cloud-Translate-V2) \nReference documentation and code samples for the Cloud Translation V2 API module Google::Cloud::Translate::V2.\n\nGoogle Cloud Translation API\n----------------------------\n\n[Google Cloud Translation API](https://cloud.google.com/translation/)\nprovides a simple, programmatic interface for translating an arbitrary\nstring into any supported language. It is highly responsive, so websites\nand applications can integrate with Translation API for fast, dynamic\ntranslation of source text. Language detection is also available in cases\nwhere the source language is unknown.\n\nTranslation API supports more than one hundred different languages, from\nAfrikaans to Zulu. Used in combination, this enables translation between\nthousands of language pairs. Also, you can send in HTML and receive HTML\nwith translated text back. You don't need to extract your source text or\nreassemble the translated content.\n\nMethods\n-------\n\n### .new\n\n def self.new(project_id: nil, credentials: nil, key: nil, scope: nil, retries: nil, timeout: nil, endpoint: nil) -\u003e Google::Cloud::Translate::V2::Api\n\nCreates a new object for connecting to Cloud Translation API. Each call creates a new connection.\n\n\n\u003cbr /\u003e\n\nLike other Cloud Platform services, Google Cloud Translation API supports authentication using a project ID\nand OAuth 2.0 credentials. In addition, it supports authentication using a public API access key. (If both the\nAPI key and the project and OAuth 2.0 credentials are provided, the API key will be used.) Instructions and\nconfiguration options are covered in the [Authentication Guide](./AUTHENTICATION \"Authentication Guide\"). \n**Parameters**\n\n- **project_id** (String) *(defaults to: nil)* --- Project identifier for the Cloud Translation service you are connecting to. If not present, the default project for the credentials is used.\n- **credentials** (String, Hash, Google::Auth::Credentials) *(defaults to: nil)* --- The path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object. (See [Credentials](/ruby/docs/reference/google-cloud-translate-v2/latest/Google-Cloud-Translate-V2-Credentials \"Google::Cloud::Translate::V2::Credentials (class)\"))\n- **key** (String) *(defaults to: nil)* --- a public API access key (not an OAuth 2.0 token)\n- **scope** (String, Array\\\u003cString\\\u003e) *(defaults to: nil)* ---\n\n The OAuth 2.0 scopes controlling the set of resources and operations that\n the connection can access. See [Using OAuth 2.0 to Access Google\n APIs](https://developers.google.com/identity/protocols/OAuth2).\n\n The default scope is:\n - `https://www.googleapis.com/auth/cloud-platform`\n- **retries** (Integer) *(defaults to: nil)* --- Number of times to retry requests on server error. The default value is `3`. Optional.\n- **timeout** (Integer) *(defaults to: nil)* --- Default timeout to use in requests. Optional.\n- **endpoint** (String) *(defaults to: nil)* --- Override of the endpoint host name. Optional. If the param is nil, uses the default endpoint. \n**Returns**\n\n- ([Google::Cloud::Translate::V2::Api](./Google-Cloud-Translate-V2-Api)) \n**Raises**\n\n- (ArgumentError)\n**Examples** \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new(\n version: :v2,\n project_id: \"my-todo-project\",\n credentials: \"/path/to/keyfile.json\"\n)\n\ntranslation = translate.translate \"Hello world!\", to: \"la\"\ntranslation.text #=\u003e \"Salve mundi!\"\n```\n\nUsing API Key. \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\ntranslate = Google::Cloud::Translate::V2.new(\n key: \"api-key-abc123XYZ789\"\n)\n\ntranslation = translate.translate \"Hello world!\", to: \"la\"\ntranslation.text #=\u003e \"Salve mundi!\"\n```\n\nUsing API Key from the environment variable. \n\n```ruby\nrequire \"google/cloud/translate/v2\"\n\nENV[\"TRANSLATE_KEY\"] = \"api-key-abc123XYZ789\"\n\ntranslate = Google::Cloud::Translate::V2.new\n\ntranslation = translate.translate \"Hello world!\", to: \"la\"\ntranslation.text #=\u003e \"Salve mundi!\"\n```\n\nConstants\n---------\n\n### VERSION\n\n**value:**\"1.1.0\".freeze"]]