Class GoogleCloudTranslationClient

java.lang.Object
fr.becpg.repo.multilingual.translation.GoogleCloudTranslationClient

public class GoogleCloudTranslationClient extends Object
Cloud Translation v3 client authenticated by the service account of the pod, without API key, or by a service account key file on a server outside Google Cloud. Enabled only when a Google project is configured.
Author:
matthieu
  • Field Details

  • Constructor Details

    • GoogleCloudTranslationClient

      public GoogleCloudTranslationClient()

      Constructor for GoogleCloudTranslationClient.

    • GoogleCloudTranslationClient

      GoogleCloudTranslationClient(org.springframework.web.client.RestTemplate restTemplate, GoogleAccessTokenProvider tokenProvider)
  • Method Details

    • setProjectId

      public void setProjectId(String projectId)

      Setter for the field projectId.

      Parameters:
      projectId - the Google Cloud project billed for the translations
    • setLocation

      public void setLocation(String location)

      Setter for the field location.

      Parameters:
      location - the Cloud Translation location, matching the endpoint region
    • setEndpoint

      public void setEndpoint(String endpoint)

      Setter for the field endpoint.

      Parameters:
      endpoint - the Cloud Translation endpoint base URL
    • setCredentialsFile

      public void setCredentialsFile(String credentialsFile)
      Authenticates with the key of a service account instead of the metadata server, when set.
      Parameters:
      credentialsFile - path of the JSON key of the service account
    • isEnabled

      public boolean isEnabled()

      isEnabled.

      Returns:
      true when a Google project is configured
    • translate

      public Optional<String> translate(String text, String sourceLanguage, String targetLanguage)
      Translates a plain text. A failure is logged and yields no translation, so that the text is never sent to another endpoint than the configured one.
      Parameters:
      text - the text to translate
      sourceLanguage - the ISO 639-1 code of the text language
      targetLanguage - the ISO 639-1 code of the translation language
      Returns:
      the translated text, empty when the translation failed
    • buildTranslateUri

      URI buildTranslateUri()
    • buildRequest

      org.springframework.http.HttpEntity<String> buildRequest(String text, String sourceLanguage, String targetLanguage)
    • getTokenProvider

      private GoogleAccessTokenProvider getTokenProvider()
    • parseTranslatedText

      static Optional<String> parseTranslatedText(String responseBody)