Translate a text with Amazon Translate using AWS SDK for PHP
In a previous tutorial named “Integrating an AWS Service into a WP Plugin Boilerplate” we learned to use the free version of WP Package Editor (WP2E) to integrate the AWS Translate Service into a WordPress Plugin Boilerplate.
In this tutorial, we have prepared the necessary setup and installed the boilerplate project and AWS SDK dependency in order to integrate Amazon Translate into a WordPress environment.
Create an IAM Policy
To translate a string using the AWS Translate service, you will need to have at least the translate:TranslateText
action in your IAM policy.
- Sign in to the AWS Management Console
- Navigate to the IAM dashboard
- In the navigation pane, choose Policies
- Choose create policy
- Click JSON tab
- Paste the JSON policy bellow
- Click Next:Tags
- Click Next:Review
- Name your policy like
AWSTranslateText
- Click Create Policy to finalize
Here is an example of an JSON policy that grants the minimum permissions required to translate a string using the AWS Translate service:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "translate:TranslateText",
"Resource": "*"
}
]
}
This policy grants the translate:TranslateText
action on all resources, which allows the user to translate a string using the AWS Translate service.
Note that the AmazonTranslateFullAccess
policy includes permissions for all actions in the AWS Translate service, so if you attach this policy to a user, they will have more permissions than just the translate:TranslateText
action.
It is generally a good practice to grant the minimum permissions required for a user to perform their tasks. This helps to reduce the risk of unintended actions and increases security.
Create an IAM User
To get an IAM user access ID and key that you can use to authenticate the AWS SDK for PHP in your script, you will need to do the following:
- Sign in to the AWS Management Console
- Navigate to the IAM dashboard
- In the navigation pane, choose Users
- Choose Add user
- Enter a user name and select the Programmatic access check box
- Choose Next: Permissions
- Select the policy previously created
- Use the search bar to find the policies that your user will need to access the AWS services used in your script. For example, if you are using the Translate service, you will need to attach the
AmazonTranslateFullAccess
policy. - Choose Next: Review.
- Review the user details and choose Create user.
Use the Translate Client
Here is an example of how you can use the Translate
client from the AWS SDK for PHP to translate the phrase “hello” from English to French:
use Aws\Translate\TranslateClient;
use Aws\Exception\AwsException;
$client = new TranslateClient([
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => [
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
],
]);
try {
$result = $client->translateText([
'Text' => 'hello',
'SourceLanguageCode' => 'en',
'TargetLanguageCode' => 'fr'
]);
echo $result['TranslatedText'];
}
catch (AwsException $e) {
echo $e->getMessage();
}
This example will output the translation of the word “hello” from English to French, which is “Bonjour”.
If an error occurs while calling the translateText
operation, it will throw an AwsException
, which will be caught in the catch block. You can then print the error message or handle the error in some other way.
Note: You will need to replace
us-east-1
,YOUR_AWS_ACCESS_KEY_ID
andYOUR_AWS_SECRET_ACCESS_KEY
with your own AWS region, access key ID and secret access key collected via your plugin settings.