Google Cloud Translate Operators¶
Prerequisite Tasks¶
To use these operators, you must do a few things:
Select or create a Cloud Platform project using the Cloud Console.
Enable billing for your project, as described in the Google Cloud documentation.
Enable the API, as described in the Cloud Console documentation.
Install API libraries via pip.
pip install 'apache-airflow[google]'Detailed information is available for Installation.
CloudTranslateTextOperator¶
Translate a string or list of strings.
For parameter definition, take a look at
CloudTranslateTextOperator
Using the operator¶
Basic usage of the operator:
product_set_create = CloudTranslateTextOperator(
task_id="translate",
values=["zażółć gęślą jaźń"],
target_language="en",
format_="text",
source_language=None,
model="base",
)
The result of translation is available as dictionary or array of dictionaries accessible via the usual XCom mechanisms of Airflow:
translation_access = BashOperator(
task_id="access", bash_command="echo '{{ task_instance.xcom_pull(\"translate\")[0] }}'"
)
Templating¶
template_fields: Sequence[str] = (
"values",
"target_language",
"format_",
"source_language",
"model",
"gcp_conn_id",
"impersonation_chain",
)