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-cloud-translate google-auth google-api-python-client oauth2client httplib2
Alpine
apk add py3-google-api-python-client py3-oauth2client py3-httplib2 py3-google-auth
Debian
apt install python3-google-auth python3-httplib2 python3-oauth2client
Fedora
yum install python-google-auth python-google-api-client python-httplib2 python-oauth2client
Arch Linux
pacman -S python-google-auth python-httplib2 python-google-api-python-client python-oauth2client
Actions#
Module reference#
- class platypush.plugins.google.translate.GoogleTranslatePlugin(target_language: str = 'en', credentials_file: str | None = None, **kwargs)[source]#
Bases:
PluginPlugin 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.jsonContext from the
GOOGLE_APPLICATION_CREDENTIALSenvironment 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" }