Translate a text with Amazon Translate using AWS SDK for PHP

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.

  1. Sign in to the AWS Management Console
  2. Navigate to the IAM dashboard
  3. In the navigation pane, choose Policies
  4. Choose create policy
  5. Click JSON tab
  6. Paste the JSON policy bellow
  7. Click Next:Tags
  8. Click Next:Review
  9. Name your policy like AWSTranslateText
  10. 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:

  1. Sign in to the AWS Management Console
  2. Navigate to the IAM dashboard
  3. In the navigation pane, choose Users
  4. Choose Add user
  5. Enter a user name and select the Programmatic access check box
  6. Choose Next: Permissions
  7. Select the policy previously created
  8. 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.
  9. Choose Next: Review.
  10. 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 and YOUR_AWS_SECRET_ACCESS_KEY with your own AWS region, access key ID and secret access key collected via your plugin settings.

Leave a Reply

About Rafasashi

Currently managing RECUWEB an IT Company providing cloud hosting and SaaS delivery model.

Related posts