Translate a language into English

Translation is one of the three Vertex AI pre-trained APIs on Google Distributed Cloud (GDC) air-gapped. Use Translation to translate more than twenty languages into English text.

To learn which languages are supported, see Supported languages. To optionally define and use a glossary, see Define terms to translate.

Before you begin

Before using the Translation API, you must do the following:

Translate text

The TranslateText method translates input text and returns text translated into English.

You can enter input text as plain or HTML, but TranslateText doesn't translate any HTML tags. TranslateText only translates the text between the tags. However, it does translate attributes in HTML5 tags, such as alt attributes. An example in using HTML5 tags and attributes is used in the syntax to exclude text from being translated into English.

The output retains the untranslated HTML tags and includes the translated text between the tags. Differences might exist between the source and target languages. The order of HTML tags in the output might differ from the order in the input text due to word-order changes in the translation.

The following code samples show how to translate the Spanish text "Hola, esto es una prueba" into "Hello, this is a test," calling the TranslateText method and specifying the endpoint.

gRPC

HOME/go/bin/grpcurl -plaintext -d '{"source_language_code": "es", "target_language_code": "en", "contents": ["Hola, esto es una prueba"]}' ENDPOINT google.cloud.translation.v3.TranslationService/TranslateText

Use the mime_type field to specify a type of file. Set the mime_type field to one of the following values:

  • text/plain
  • text/html

If the mime_type field is empty, text/html is the default value.

The following example uses the mime_type field:

HOME/go/bin/grpcurl -plaintext -d '{"mime_type": "text/html", "source_language_code": "es", "target_language_code": "en", "contents": ["Hola, esto es una prueba."]}' ENDPOINT google.cloud.translation.v3.TranslationService/TranslateText

curl

curl -X POST -H "Content-Type: application/json" http://ENDPOINT/v3/projects/PROJECT_ID:translateText -d '{"source_language_code": "es", "target_language_code": "en", "contents": ["Hola, esto es una prueba"]}'

Use the mime_type field to specify a type of file. Set the mime_type field to one of the following values:

  • text/plain
  • text/html

If the mime_type field is empty, text/html is the default value.

The following example uses the mime_type field:

curl -X POST -H "Content-Type: application/json" http://ENDPOINT/v3/projects/PROJECT_ID:translateText -d '{"mime_type": "text/html", "source_language_code": "es", "target_language_code": "en", "contents": ["Hola, esto es una prueba."]}'

Python

# Imports the Google Cloud Translation library
from google.cloud import translate

# Initialize Translation client
def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="PROJECT_ID"):
    """Translating Text."""

    client = translate.TranslationServiceClient()

    location = "global"

    # Translate text from English to French
    response = client.translate_text(
        request={
            "contents": [TEXT],
            "mime_type": "text/plain",  # mime types: text/plain, text/html
            "source_language_code": en,
            "target_language_code": fr,
        }
    )

    # Display the translation for each input text provided
    for translation in response.translations:
        print("Translated text: {}".format(translation.translated_text))

Exclude text from Translation

Use one of the following HTML tag examples to exclude parts of your text:

  • <span translate="no"> </span>
  • <span class="notranslate"> </span>

For example, if you have the following text in Spanish:

Hola, esto es una prueba.

which translates in English to:

Hello, this is a test.

You only want to translate part of the text.

esto es una prueba.

which translates in English to

this is a test.

Use the HTML tags to exclude "Hello," as follows:

    curl -X POST -H "Content-Type: application/json" http://ENDPOINT/v3/projects/PROJECT_ID:translateText -d '{"mime_type": "text/html", "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. To call the DetectLanguage method, specify the endpoint.

gRPC

HOME/go/bin/grpcurl -plaintext -d '{"content": "Hello, this is a test"}' ENDPOINT google.cloud.translation.v3.TranslationService/DetectLanguage

curl

curl -X POST -H "Content-Type: application/json" http://ENDPOINT/v3/projects/PROJECT_ID:detectLanguage -d '{"content": "Hola, esto es una prueba"}'

Get supported languages

The GetSupportedLanguages method returns a list of languages supported by the Translation API. To call the GetSupportedLanguages method, specify the endpoint.

gRPC

HOME/go/bin/grpcurl -plaintext -d '{}' ENDPOINT google.cloud.translation.v3.TranslationService/GetSupportedLanguages

curl

curl -X POST -H "Content-Type: application/json" http://ENDPOINT/v3/projects/ENDPOINT:getSupportedLanguages -d "{}"

Get an operation

The get method returns the latest state of a long-running operation. Use this method to poll the operation result generated by the Translation API service. To call the get method, specify your project ID and the endpoint.

gRPC

$HOME/go/bin/grpcurl -plaintext -d '{"name": "projects/PROJECT_ID/operations/PROJECT_ID"}' ENDPOINT google.longrunning.Operations/get

curl

curl -X GET http://ENDPOINT/v3/projects/PROJECT_ID/operations/PROJECT_ID

List operations

The list method returns a list of the operations that match a specified filter in the request. To call the list method, specify your project ID and the endpoint.

gRPC

$HOME/go/bin/grpcurl -plaintext -d '{"name": "projects/PROJECT_ID/operations", "page_size": 10}' ENDPOINT google.longrunning.Operations/list

curl

curl -X GET http://ENDPOINT/v3/projects/PROJECT_ID/operations?page_size=10

Supported languages

The following languages translate to English:

Language Language code
Afrikaans af
Albanian sq
Amharic am
Arabic ar
Armenian hy
Azerbaijani az
Basque eu
Belarusian be
Bengali bn
Bosnian bs
Bulgarian bg
Catalan ca
Cebuano ceb
Chinese (Simplified) zh
Chichewa ny
Corsican co
Croatian hr
Czech cs
Danish da
Dutch nl
Estonian et
Farsi/Persian fa
Filipino fil
Finnish fi
French fr
Frisian fy
Galician gl
Georgian ka
German de
Greek el
Gujarati gu
Haitian Creole ht
Hausa ha
Hawaiian haw
Hebrew iw
Hindi hi
Hmong hmn
Hungarian hu
Icelandic is
Igbo ig
Indonesian id
Irish ga
Italian it
Japanese ja
Javanese jv
Lao lo
Kannada kn
Kazakh kk
Khmer km
Kinyarwanda rw
Korean ko
Kurdish (Kurmanji) ku
Kyrgyz ky
Latvian lv
Lithuanian lt
Luxembourgish lb
Macedonian mk
Malagasy mg
Malay ms
Maori mi
Malayalam ml
Maltese mt
Marathi mr
Mongolian mn
Myanmar (Burmese) my
Nepali ne
Norwegian no
Odia (Oriya) or
Pashto ps
Polish pl
Portuguese pt
Punjabi pa
Romanian ro
Russian ru
Samoan sm
Scots Gaelic gd
Serbian sr
Sesotho st
Sindhi sd
Sinhala si
Shona sn
Slovak sk
Slovenian sl
Somali so
Spanish es
Sundanese su
Swahili sw
Tajik tg
Tamil ta
Tatar tt
Telugu te
Thai th
Turkish tr
Turkmen tk
Ukrainian uk
Urdu ur
Uyghur ug
Uzbek uz
Vietnamese vi
Xhosa xh
Yiddish yi
Yoruba yo
Welsh cy
Zulu zu