AWS Translate
Since Camel 3.1
Only producer is supported
The AWS2 Translate component translates text from a source language into a target language with Amazon Translate.
Prerequisites
You must have a valid Amazon Web Services developer account, and be signed up to use Amazon Translate. More information is available at Amazon Translate.
URI Format
aws2-translate://label[?options]
You can append query options to the URI in the following format:
?options=value&option2=value&…
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The AWS Translate component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Whether Amazon Translate detects the language of the text. The producer then sends auto as the source language code, and Amazon Translate detects the language with Amazon Comprehend, which must be available in the region; when the detection has a low confidence, the request fails with a DetectedLanguageLowConfidenceException. The sourceLanguage option and the CamelAwsTranslateSourceLanguage header are then not used, and the detected language is not returned. It is not used when pojoRequest is true. | false | boolean | |
The configuration that the endpoints of this component start from. Each endpoint gets a copy of it, and the options of the endpoint replace its values. | Translate2Configuration | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Required The operation that the producer performs. translateText is the only operation. The CamelAwsTranslateOperation header replaces this option. Enum values:
| translateText | Translate2Operations | |
Whether to send the requests to the URI of the uriEndpointOverride option instead of the endpoint of the region. It has no effect when uriEndpointOverride is not set. | false | boolean | |
Whether the body is a TranslateTextRequest of the AWS SDK, for example to use the formality, profanity or brevity settings, which have no option. If true, the producer sends that request as it is, without the language options, autodetectSourceLanguage or the headers, and a body of another type fails. If false, the producer builds the request from the text of the body, the options and the headers. | false | boolean | |
The AWS region of Amazon Translate, as a region ID such as eu-west-1, which is the value of Region.EU_WEST_1.id(). If not set, the default region provider chain of the AWS SDK is used. Amazon Translate is not available in every region. Enum values:
| String | ||
The language code of the text to translate, such as en, fr-CA or zh-TW, from the supported languages of Amazon Translate, or auto to let Amazon Translate detect the language. If not set, the CamelAwsTranslateSourceLanguage header is used, and the producer fails when neither is set. It is not used when autodetectSourceLanguage or pojoRequest is true. | String | ||
The language code of the language to translate the text into, such as de, fr-CA or zh-TW, from the supported languages of Amazon Translate. If not set, the CamelAwsTranslateTargetLanguage header is used, and the producer fails when neither is set. It is not used when pojoRequest is true. | String | ||
The URI of the endpoint to use instead of the endpoint of the region, for example http://localhost:4566 for a local emulator. It is used only when overrideEndpoint is true. | String | ||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
Autowired To use a custom software.amazon.awssdk.services.translate.TranslateClient, which is autowired when the registry has a single one. When this option is set, the credentials, region, proxy, endpoint override and trustAllCertificates options are not used. The endpoint does not close a client that is set or autowired. | TranslateClient | ||
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | boolean | |
The host name of the proxy that the HTTP client of Amazon Translate connects through. The proxy is used only when the proxyPort option is set too. | String | ||
The port of the proxy that the HTTP client of Amazon Translate connects through. The proxy is used only when the proxyHost option is set too. | Integer | ||
The protocol that the HTTP client of Amazon Translate uses to connect to the proxy. It is used only when the proxyHost and proxyPort options are set. Enum values:
| HTTPS | Protocol | |
The access key ID of the AWS credentials, used together with the secretKey option. The endpoint requires it, unless translateClient is set or useDefaultCredentialsProvider or useProfileCredentialsProvider is true. It is also required when useSessionCredentials is true. | String | ||
The name of the profile in the AWS shared credentials and config files, used when useProfileCredentialsProvider is true. If not set, the AWS SDK uses its default profile, which the AWS_PROFILE environment variable can change. | String | ||
The secret access key of the AWS credentials, used together with the accessKey option. The endpoint requires it, unless translateClient is set or useDefaultCredentialsProvider or useProfileCredentialsProvider is true. It is also required when useSessionCredentials is true. | String | ||
The session token of temporary AWS credentials, for example of an assumed IAM role. It is used only when useSessionCredentials is true, together with the accessKey and secretKey options. All three are then required: if one of the three is missing, creating the client fails. | String | ||
Whether the HTTP client of Amazon Translate trusts every TLS certificate without checking it. It applies to any endpoint, not only to an overridden one, so use it only for testing, for example against a local emulator. | false | boolean | |
Whether to load the credentials with the default credentials provider chain of the AWS SDK, which looks in system properties, environment variables, the shared credentials and config files, and the container or instance role. It takes precedence over the other credentials options. | false | boolean | |
Whether to load the credentials from a profile of the AWS shared credentials and config files, the one of the profileCredentialsName option. It is not used when useDefaultCredentialsProvider is true, and it takes precedence over the session and static credentials. | false | boolean | |
Whether to use temporary credentials made of the accessKey, secretKey and sessionToken options, for example those of an assumed IAM role. It is not used when useDefaultCredentialsProvider or useProfileCredentialsProvider is true. | false | boolean |
Endpoint Options
The AWS Translate endpoint is configured using URI syntax:
aws2-translate:label
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Whether Amazon Translate detects the language of the text. The producer then sends auto as the source language code, and Amazon Translate detects the language with Amazon Comprehend, which must be available in the region; when the detection has a low confidence, the request fails with a DetectedLanguageLowConfidenceException. The sourceLanguage option and the CamelAwsTranslateSourceLanguage header are then not used, and the detected language is not returned. It is not used when pojoRequest is true. | false | boolean | |
Required The operation that the producer performs. translateText is the only operation. The CamelAwsTranslateOperation header replaces this option. Enum values:
| translateText | Translate2Operations | |
Whether to send the requests to the URI of the uriEndpointOverride option instead of the endpoint of the region. It has no effect when uriEndpointOverride is not set. | false | boolean | |
Whether the body is a TranslateTextRequest of the AWS SDK, for example to use the formality, profanity or brevity settings, which have no option. If true, the producer sends that request as it is, without the language options, autodetectSourceLanguage or the headers, and a body of another type fails. If false, the producer builds the request from the text of the body, the options and the headers. | false | boolean | |
The AWS region of Amazon Translate, as a region ID such as eu-west-1, which is the value of Region.EU_WEST_1.id(). If not set, the default region provider chain of the AWS SDK is used. Amazon Translate is not available in every region. Enum values:
| String | ||
The language code of the text to translate, such as en, fr-CA or zh-TW, from the supported languages of Amazon Translate, or auto to let Amazon Translate detect the language. If not set, the CamelAwsTranslateSourceLanguage header is used, and the producer fails when neither is set. It is not used when autodetectSourceLanguage or pojoRequest is true. | String | ||
The language code of the language to translate the text into, such as de, fr-CA or zh-TW, from the supported languages of Amazon Translate. If not set, the CamelAwsTranslateTargetLanguage header is used, and the producer fails when neither is set. It is not used when pojoRequest is true. | String | ||
The URI of the endpoint to use instead of the endpoint of the region, for example http://localhost:4566 for a local emulator. It is used only when overrideEndpoint is true. | String | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Autowired To use a custom software.amazon.awssdk.services.translate.TranslateClient, which is autowired when the registry has a single one. When this option is set, the credentials, region, proxy, endpoint override and trustAllCertificates options are not used. The endpoint does not close a client that is set or autowired. | TranslateClient | ||
The host name of the proxy that the HTTP client of Amazon Translate connects through. The proxy is used only when the proxyPort option is set too. | String | ||
The port of the proxy that the HTTP client of Amazon Translate connects through. The proxy is used only when the proxyHost option is set too. | Integer | ||
The protocol that the HTTP client of Amazon Translate uses to connect to the proxy. It is used only when the proxyHost and proxyPort options are set. Enum values:
| HTTPS | Protocol | |
The access key ID of the AWS credentials, used together with the secretKey option. The endpoint requires it, unless translateClient is set or useDefaultCredentialsProvider or useProfileCredentialsProvider is true. It is also required when useSessionCredentials is true. | String | ||
The name of the profile in the AWS shared credentials and config files, used when useProfileCredentialsProvider is true. If not set, the AWS SDK uses its default profile, which the AWS_PROFILE environment variable can change. | String | ||
The secret access key of the AWS credentials, used together with the accessKey option. The endpoint requires it, unless translateClient is set or useDefaultCredentialsProvider or useProfileCredentialsProvider is true. It is also required when useSessionCredentials is true. | String | ||
The session token of temporary AWS credentials, for example of an assumed IAM role. It is used only when useSessionCredentials is true, together with the accessKey and secretKey options. All three are then required: if one of the three is missing, creating the client fails. | String | ||
Whether the HTTP client of Amazon Translate trusts every TLS certificate without checking it. It applies to any endpoint, not only to an overridden one, so use it only for testing, for example against a local emulator. | false | boolean | |
Whether to load the credentials with the default credentials provider chain of the AWS SDK, which looks in system properties, environment variables, the shared credentials and config files, and the container or instance role. It takes precedence over the other credentials options. | false | boolean | |
Whether to load the credentials from a profile of the AWS shared credentials and config files, the one of the profileCredentialsName option. It is not used when useDefaultCredentialsProvider is true, and it takes precedence over the session and static credentials. | false | boolean | |
Whether to use temporary credentials made of the accessKey, secretKey and sessionToken options, for example those of an assumed IAM role. It is not used when useDefaultCredentialsProvider or useProfileCredentialsProvider is true. | false | boolean |
Message Headers
The AWS Translate component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelAwsTranslateSourceLanguage (producer) Constant: | The language code of the text to translate, such as en or zh-TW, or auto to let Amazon Translate detect the language. It is used only when the sourceLanguage option is not set, and not when autodetectSourceLanguage or pojoRequest is true. | String | |
CamelAwsTranslateTargetLanguage (producer) Constant: | The language code of the language to translate the text into, such as de or zh-TW. It is used only when the targetLanguage option is not set, and not when pojoRequest is true. | String | |
CamelAwsTranslateTerminologyNames (producer) Constant: | The names of the custom terminologies that Amazon Translate applies to the translation, as a Collection of String, a String array, or a String with one name or names separated by commas. Amazon Translate accepts one custom terminology per request. It is not used when pojoRequest is true. | Collection | |
CamelAwsTranslateOperation (producer) Constant: | The operation that the producer performs, which replaces the operation option. The only value is translateText. | String |
Message Body
The message body at each side of the AWS Translate component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The text to translate: bytes, a stream or a file are read as text, a Map or a POJO is translated as its toString(), so take the text out of it first, and no body fails. With pojoRequest=true the body is a TranslateTextRequest of the AWS SDK: another body fails. |
Producer output |
| The translated text. The source language that AWS detects with autodetectSourceLanguage=true is not kept. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Required Translate component options
You have to provide the translateClient in the Registry or your accessKey and secretKey to access the Amazon Translate service.
Usage
Static credentials, Default Credential Provider and Profile Credentials Provider
You have the possibility of avoiding the usage of explicit static credentials by specifying the useDefaultCredentialsProvider option and set it to true.
The order of evaluation for Default Credentials Provider is the following:
-
Java system properties -
aws.accessKeyIdandaws.secretKey -
Environment variables -
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY. -
Web Identity Token from AWS STS.
-
The shared credentials and config files.
-
Amazon ECS container credentials - loaded from the Amazon ECS if the environment variable
AWS_CONTAINER_CREDENTIALS_RELATIVE_URIis set. -
Amazon EC2 Instance profile credentials.
You have also the possibility of using Profile Credentials Provider, by specifying the useProfileCredentialsProvider option to true and profileCredentialsName to the profile name.
Only one of static, default and profile credentials could be used at the same time.
For more information about this you can look at AWS credentials documentation
Translate Producer operations
Camel-AWS Translate component provides the following operation on the producer side:
-
translateText
Source and target language
Each language is taken from its option, or else from its header:
-
the source language from the
sourceLanguageoption, or else theCamelAwsTranslateSourceLanguageheader; -
the target language from the
targetLanguageoption, or else theCamelAwsTranslateTargetLanguageheader.
So one language can be set as an option and the other as a header. With autodetectSourceLanguage=true, the source language is auto, and Amazon Translate detects it.
When the target language is missing, or the source language is missing and autodetectSourceLanguage is not enabled, the producer fails with an IllegalArgumentException that names the option and the header to set.
Examples
Translate Text example
-
Java
-
YAML
from("direct:start")
.setHeader(Translate2Constants.SOURCE_LANGUAGE, constant(Translate2LanguageEnum.ITALIAN))
.setHeader(Translate2Constants.TARGET_LANGUAGE, constant(Translate2LanguageEnum.GERMAN))
.setBody(constant("Ciao"))
.to("aws2-translate://test?translateClient=#amazonTranslateClient&operation=translateText"); In YAML, the headers are the language codes, it and de:
- route:
from:
uri: direct:start
steps:
- setHeader:
name: CamelAwsTranslateSourceLanguage
expression:
constant:
expression: it
- setHeader:
name: CamelAwsTranslateTargetLanguage
expression:
constant:
expression: de
- setBody:
expression:
constant:
expression: Ciao
- to:
uri: aws2-translate:test
parameters:
translateClient: "#amazonTranslateClient"
operation: translateText As a result, you’ll get an exchange containing the translated text.
Using a POJO as body
Sometimes building an AWS Request can be complex because of multiple options. We introduce the possibility to use a POJO as the body. In AWS Translate, the only operation available is TranslateText, so you can do something like:
from("direct:start")
.setBody(constant(TranslateTextRequest.builder().sourceLanguageCode(Translate2LanguageEnum.ITALIAN.toString())
.targetLanguageCode(Translate2LanguageEnum.GERMAN.toString()).text("Ciao").build()))
.to("aws2-translate://test?translateClient=#amazonTranslateClient&operation=translateText&pojoRequest=true"); In this way, you’ll pass the request directly without the need of passing headers and options specifically related to this operation.
Dependencies
Maven users will need to add the following dependency to their pom.xml.
pom.xml
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-aws2-translate</artifactId>
<version>${camel-version}</version>
</dependency> where ${camel-version} must be replaced by the actual version of Camel.