Translate text with Cloud Translation Advanced
This document shows you how to translate a sample text by using Cloud Translation Advanced.
Before you begin
Before you can start using the Cloud Translation API, you must have a project that has the Cloud Translation API enabled, and you must have a private key with the appropriate credentials. You can also install client libraries for common programming languages to help you make calls to the API. For more information, see the Setup page.
Translate text example
The following example demonstrates how to use Cloud Translation - Advanced to translate text to a given target language.
REST
Use curl
or PowerShell to make a request.
The source and target languages are identified using the ISO-639 codes. The source language is English (en) and the target language is Russian (ru). The format of the query is noted as "text" for plain text.
Before using any of the request data, make the following replacements:
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID:translateText
Request JSON body:
{ "sourceLanguageCode": "en", "targetLanguageCode": "ru", "contents": ["Dr. Watson, come here!"], "mimeType": "text/plain" }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "translations": [{ "translatedText": "Доктор Ватсон, иди сюда!" }] }
Go
Before trying this sample, follow the Go setup instructions in the Translation quickstart using client libraries. For more information, see the Translation Go API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in the Translation quickstart using client libraries. For more information, see the Translation Java API reference documentation.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Translation quickstart using client libraries. For more information, see the Translation Node.js API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in the Translation quickstart using client libraries. For more information, see the Translation Python API reference documentation.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the Translation reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the Translation reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the Translation reference documentation for Ruby.
Additional resources
- For more information about translating text, see the Translating text (Advanced) how-to guide.
- For help on resolving common issues or errors, see the Troubleshooting page.
- For answers to general questions about Cloud Translation, see the General FAQ page.
- Translation is available in two editions. For more information about each edition, see Comparing Basic and Advanced.