The Vertex AI Translation service lets you translate text written in a language into any of the other supported languages.
This page shows you how to translate a sample text using the Vertex AI Translation API on Google Distributed Cloud (GDC) air-gapped.
Before you begin
Before you can start using the Vertex AI Translation API, you must have a project with the Vertex AI Translation API enabled, and you must have the appropriate credentials. You can also install client libraries to help you make calls to the API. For more information, see Set up a translation project.
Translate text
The translateText
method takes input text in a particular language and returns
the translated text into another language. You can enter plain or HTML text as
input.
If you enter HTML text, the translateText
method translates only the text
between the HTML tags without translating the tags. However, it translates
attributes in HTML5 tags, such as alt
attributes. An example of using HTML5
tags and attributes is used in the syntax to exclude text from translation.
The output retains the untranslated HTML tags and includes the translated text
between them.
Make a curl
request to the Vertex AI Translation pre-trained
API. Otherwise, interact with the Vertex AI Translation pre-trained API from
a Python script to translate text from one language to another.
The following examples show how to translate an input text from one language to another:
curl
Follow these steps to make a curl
request:
Make the request:
curl -vv -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID:translateText -d '{"parent": "projects/PROJECT_ID", "source_language_code": "SOURCE_LANGUAGE", "target_language_code": "TARGET_LANGUAGE", "contents": ["INPUT_TEXT"]}'
Replace the following:
TOKEN
: the authentication token you obtained.ENDPOINT
: the Vertex AI Translation endpoint that you use for your organization. For more information, view service status and endpoints.PROJECT_ID
: your project ID.SOURCE_LANGUAGE
: the language code of your input text. See the list of supported languages and their respective language codes.TARGET_LANGUAGE
: the language code you want to translate your text into. See the list of supported languages and their respective language codes.INPUT_TEXT
: your input text in the source language.
Use the mime_type
field to specify a type of file. Set the mime_type
field to one of the following values:
text/plain
: your input is plain text.text/html
: your input is HTML text.
If the mime_type
field is empty, text/html
is the default value.
The following example uses the mime_type
field:
curl -vv -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID:translateText -d '{"mime_type": "text/html", "parent": "projects/PROJECT_ID", "source_language_code": "SOURCE_LANGUAGE", "target_language_code": "TARGET_LANGUAGE", "contents": ["INPUT_TEXT"]}'
The output returns the translated text.
Python
Follow these steps to use the Vertex AI Translation service from a Python script:
Install the latest version of the Vertex AI Translation client library.
Add the following code to the Python script you created:
from google.cloud import translate import google.auth from google.auth.transport import requests from google.api_core.client_options import ClientOptions audience = "https://ENDPOINT:443" api_endpoint="ENDPOINT:443" def translate_client(creds): opts = ClientOptions(api_endpoint=api_endpoint) return translate.TranslationServiceClient(credentials=creds, client_options=opts) def main(): creds = None try: creds, project_id = google.auth.default() creds = creds.with_gdch_audience(audience) req = requests.Request() creds.refresh(req) print("Got token: ") print(creds.token) except Exception as e: print("Caught exception" + str(e)) raise e return creds def translate_func(creds): tc = translate_client(creds) req = { "parent": "projects/PROJECT_ID", "source_language_code": "SOURCE_LANGUAGE", "target_language_code": "TARGET_LANGUAGE", "mime_type": "text/plain", "contents": ["INPUT_TEXT"] } resp = tc.translate_text(req) print(resp) if __name__=="__main__": creds = main() translate_func(creds)
Replace the following:
ENDPOINT
: the Vertex AI Translation endpoint that you use for your organization. For more information, view service status and endpoints.PROJECT_ID
: your project ID.SOURCE_LANGUAGE
: the language code of your input text. See the list of supported languages and their respective language codes.TARGET_LANGUAGE
: the language code you want to translate your text into. See the list of supported languages and their respective language codes.INPUT_TEXT
: your input text in the source language.
Use the
mime_type
field to specify a type of file. Set themime_type
field to one of the following values:text/plain
: your input is plain text.text/html
: your input is HTML text.
If the
mime_type
field is empty,text/html
is the default value.Save the Python script.
Run the Python script to translate the text:
python SCRIPT_NAME
Replace
SCRIPT_NAME
with the name you gave to your Python script, such astranslation.py
.
For more information on the translateText
method, see the Python client library.
Exclude text from translation
Use one of the following HTML tags on the contents
field of requests to
exclude parts of your text from translation:
<span translate="no">"TEXT"</span>
<span class="notranslate">"TEXT"</span>
Replace TEXT
with the portion of text you want to exclude
from translation.
For example, if you have the following input text in Spanish:
Hola, esto es una prueba.
Then, that text translates in English to the following sentence:
Hello, this is a test.
Suppose you only want to translate the following part of the text, excluding
Hola,
from the input text:
esto es una prueba.
That part of the text translates in English to the following sentence:
this is a test.
Use the HTML tags to exclude text from the translation. For example, the
following curl
request uses the <span class="notranslate">"TEXT"</span>
tag to exclude Hola,
from the previous input text in Spanish when translating
the text to English:
curl -vv -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID:translateText -d '{"parent": "projects/PROJECT_ID", "source_language_code": "es", "target_language_code": "en", "contents": [<span class="notranslate">"Hola,"</span>"esto es una prueba."]}'
Detect language
The detectLanguage
method returns the language of a text string by sending an
HTTP request.
For example, the following request detects English as the language from the
input text Hello, this is a test
:
curl
curl -vv -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID:detectLanguage -d '{"parent": "projects/PROJECT_ID", "contents": ["Hello, this is a test"]}'
Get an operation
The getOperation
method returns the latest state of a
long-running operation.
Use this method to retrieve the operation result generated by the
Vertex AI Translation API service. To use this method, specify your
project ID and the Vertex AI Translation endpoint.
For example, the following request returns the state of a long-running operation, such as creating a glossary, that is running in your project:
curl
curl -vv -X GET -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID/operations/PROJECT_ID
List operations
The listOperations
method returns a list of the long-running operations
that match a specified filter in the request. To use this method, specify your
project ID and the Vertex AI Translation endpoint.
For example, the following request returns the list of operations running in your project and limit the page size to ten results per page:
curl
curl -vv -X GET -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID/operations?page_size=10
Get supported languages
The getSupportedLanguages
method returns the list of languages that the
Vertex AI Translation API supports.
For example, the following request returns the supported languages by specifying the Vertex AI Translation endpoint:
curl
curl -vv -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID:getSupportedLanguages -d "{}"
For the complete list of the supported languages, see Supported languages for Vertex AI Translation.