The AI.TRANSLATE function
This document describes the AI.TRANSLATE function, which translates text
strings between languages. AI.TRANSLATE brings high-quality
Translation LLM (TLLM) capabilities
to your SQL workflows, while also supporting standard
Neural Machine Translation (NMT)
workloads.
The AI.TRANSLATE function uses your end-user credentials
by default for standard translation tasks, reducing the need for additional
set up steps and authentication.
Syntax
AI.TRANSLATE( content, target_language [, source_language => source_language] [, model => model_name] [, return_details => return_details] )
Arguments
The AI.TRANSLATE function takes the following arguments:
content: aSTRINGvalue that represents the text to translate. This can be a string literal, column name, or string expression.target_language: aSTRINGvalue that specifies a supported language code to translate the text to.source_language: aSTRINGvalue that specifies the language code of the original content. If omitted, the underlying API uses automatic language detection.model_name: aSTRINGvalue that indicates the backend translation engine. To use the TLLM model, set the value totranslation-llm. If omitted, the value defaults tonmt.return_details: aBOOLvalue. If set toTRUE, the function returns aSTRUCTwith additional details about the translation process, including the translated text, the full response from the Translation API, and any error messages. If omitted, the value defaults toFALSE, and only the translated text is returned.
Output
If return_details is set to FALSE, the function returns a STRING value:
- If the translation succeeds, the value contains the translated text.
- If an error occurs, the value is
NULL.
If return_details is set to TRUE, the AI.TRANSLATE function returns a
STRUCT with the following fields:
result: aSTRINGvalue that contains the successfully translated text. If an error occurs, then this value isNULL.status: aSTRINGvalue that contains the top-level execution status. If the operation is successful, the string contains"OK". If there is an error, the string contains the API error message or HTTP code.details: aJSONtype that contains one of the following:- If the translation succeeds, the Translation API result for that row,
including the
translated_textandmodelfields, and thedetected_language_codefield if the source language was detected automatically. - If an error occurs, the error code and reason, for example
{"error": {"code": 3, "reason": "INVALID_ARGUMENT"}}.
- If the translation succeeds, the Translation API result for that row,
including the
Examples
The following examples demonstrate how to use the AI.TRANSLATE function.
Translate with the default model
The following example translates text from an unspecified language to French using the default NMT model:
SELECT AI.TRANSLATE('Hello, how are you?', 'fr');
The output is similar to the following:
"Bonjour, comment allez-vous ?"
Specify a source language
The following example specifies that the original text is in English:
SELECT AI.TRANSLATE(
'Hello, how are you?',
'fr',
source_language => 'en'
);
The output is similar to the following:
"Bonjour, comment allez-vous ?"
Use a different model
The following example uses the TLLM model to translate the text:
SELECT AI.TRANSLATE(
'Hello, how are you?',
'fr',
model => 'translation-llm'
);
The output is similar to the following:
"Bonjour comment allez-vous ?"
Return translation details
The following example sets return_details to TRUE to retrieve additional
details about the translation process:
SELECT AI.TRANSLATE(
'Hello, how are you?',
'fr',
return_details => TRUE
);
The output is similar to the following:
{"result":"Bonjour, comment allez-vous ?","status":"OK","details":"..."}
Locations
Where the Cloud Translation API processes your data depends on the location of your query:
USmulti-region: processed in the US.EUmulti-region: processed in the EU.- All other locations, including single regions such as
us-central1andeurope-west1: processed by the Cloud Translation API global endpoint, which doesn't guarantee a processing location.
If you have data processing location requirements, run AI.TRANSLATE in the
US or EU multi-region.
What's next
- Learn more about the
ML.TRANSLATEfunction. - Learn how to process unstructured documents with the
ML.PROCESS_DOCUMENTfunction. - Learn how to analyze text with the
ML.UNDERSTAND_TEXTfunction.