Class GoogleCloudTranslationClient
java.lang.Object
fr.becpg.repo.multilingual.translation.GoogleCloudTranslationClient
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 Summary
FieldsModifier and TypeFieldDescriptionprivate Stringprivate Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate Stringprivate static final org.apache.commons.logging.Logprivate static final Stringprivate Stringprivate final org.springframework.web.client.RestTemplateprivate GoogleAccessTokenProviderprivate static final String -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for GoogleCloudTranslationClient.GoogleCloudTranslationClient(org.springframework.web.client.RestTemplate restTemplate, GoogleAccessTokenProvider tokenProvider) -
Method Summary
Modifier and TypeMethodDescription(package private) org.springframework.http.HttpEntity<String> buildRequest(String text, String sourceLanguage, String targetLanguage) (package private) URIprivate GoogleAccessTokenProviderbooleanisEnabled.parseTranslatedText(String responseBody) voidsetCredentialsFile(String credentialsFile) Authenticates with the key of a service account instead of the metadata server, when set.voidsetEndpoint(String endpoint) Setter for the fieldendpoint.voidsetLocation(String location) Setter for the fieldlocation.voidsetProjectId(String projectId) Setter for the fieldprojectId.Translates a plain text.
-
Field Details
-
logger
private static final org.apache.commons.logging.Log logger -
TRANSLATE_PATH
- See Also:
-
JSON_CONTENTS
- See Also:
-
JSON_SOURCE_LANGUAGE_CODE
- See Also:
-
JSON_TARGET_LANGUAGE_CODE
- See Also:
-
JSON_MIME_TYPE
- See Also:
-
MIME_TYPE_TEXT_PLAIN
- See Also:
-
JSON_TRANSLATIONS
- See Also:
-
JSON_TRANSLATED_TEXT
- See Also:
-
restTemplate
private final org.springframework.web.client.RestTemplate restTemplate -
tokenProvider
-
projectId
-
location
-
endpoint
-
credentialsFile
-
-
Constructor Details
-
GoogleCloudTranslationClient
public GoogleCloudTranslationClient()Constructor for GoogleCloudTranslationClient.
-
GoogleCloudTranslationClient
GoogleCloudTranslationClient(org.springframework.web.client.RestTemplate restTemplate, GoogleAccessTokenProvider tokenProvider)
-
-
Method Details
-
setProjectId
Setter for the field
projectId.- Parameters:
projectId- the Google Cloud project billed for the translations
-
setLocation
Setter for the field
location.- Parameters:
location- the Cloud Translation location, matching the endpoint region
-
setEndpoint
Setter for the field
endpoint.- Parameters:
endpoint- the Cloud Translation endpoint base URL
-
setCredentialsFile
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
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 translatesourceLanguage- the ISO 639-1 code of the text languagetargetLanguage- the ISO 639-1 code of the translation language- Returns:
- the translated text, empty when the translation failed
-
buildTranslateUri
URI buildTranslateUri() -
buildRequest
-
getTokenProvider
-
parseTranslatedText
-