google.translate#

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:

  1. Create a project on the Google Cloud console if you don’t have one already.

  2. In the menu navigate to the Artificial Intelligence section and select Translations and enable the API.

  3. From the menu select APIs & Services and create a service account. You can leave role and permissions empty.

  4. 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.

Requires:

  • google-api-python-client (pip install google-api-python-client)

  • oauth2client (pip install oauth2client)

  • google-cloud-translate (pip install google-cloud-translate)

__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:

    1. ~/.credentials/platypush/google/translate.json

    2. 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) TranslateResponse[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:

platypush.message.response.translate.TranslateResponse.