google.translate
#
Description#
Plugin to interact with the Google Translate API. You’ll need a Google Cloud active project and a set of credentials to use this plugin:
Create a project on the Google Cloud console if you don’t have one already.
In the menu navigate to the Artificial Intelligence section and select Translations and enable the API.
From the menu select APIs & Services and create a service account. You can leave role and permissions empty.
Create a new private JSON key for the service account and download it. By default platypush will look for the credentials file under
~/.credentials/platypush/google/translate.json
.
Configuration#
google.translate:
# [Optional]
# Default target language (default: 'en').
# target_language: en # type=str
# [Optional]
# Google service account JSON credentials file.
# If none is specified then the plugin will search for the credentials
# file in the following order:
#
# 1. ``~/.credentials/platypush/google/translate.json``
# 2. Context from the ``GOOGLE_APPLICATION_CREDENTIALS``
# environment variable.
# credentials_file: # type=Optional[str]
Dependencies#
pip
pip install google-api-python-client oauth2client google-auth google-cloud-translate httplib2
Alpine
apk add py3-httplib2 py3-google-api-python-client py3-oauth2client py3-google-auth
Debian
apt install python3-httplib2 python3-oauth2client python3-google-auth
Fedora
yum install python-httplib2 python-oauth2client python-google-auth python-google-api-client
Arch Linux
pacman -S python-httplib2 python-google-api-python-client python-oauth2client python-google-auth
Actions#
Module reference#
- class platypush.plugins.google.translate.GoogleTranslatePlugin(target_language: str = 'en', credentials_file: str | None = None, **kwargs)[source]#
Bases:
Plugin
Plugin to interact with the Google Translate API. You’ll need a Google Cloud active project and a set of credentials to use this plugin:
Create a project on the Google Cloud console if you don’t have one already.
In the menu navigate to the Artificial Intelligence section and select Translations and enable the API.
From the menu select APIs & Services and create a service account. You can leave role and permissions empty.
Create a new private JSON key for the service account and download it. By default platypush will look for the credentials file under
~/.credentials/platypush/google/translate.json
.
- __init__(target_language: str = 'en', credentials_file: str | None = None, **kwargs)[source]#
- Parameters:
target_language – Default target language (default: ‘en’).
credentials_file –
Google service account JSON credentials file. If none is specified then the plugin will search for the credentials file in the following order:
~/.credentials/platypush/google/translate.json
Context from the
GOOGLE_APPLICATION_CREDENTIALS
environment variable.
- translate(text: str, target_language: str | None = None, source_language: str | None = None, format: str | None = None) dict [source]#
Translate a piece of text or HTML.
- Parameters:
text – Input text.
target_language – target_language override.
source_language – source_language (default: auto-detect).
format – Input format (available formats:
text
,html
).
- Returns:
dict
{ "translated_text": "Translated text", "source_text": "Source text", "detected_source_language": "Detected source language" }