Amazon AWS Translate is a pretty cool translation service. You can get started free of charge. Let’s give it a try. This demo assumes you’ve got an AWS account (if not, first go get that). I’m using PHP 7.0 on an Ubuntu 16.04 box.

First, create a new IAM (Identity and Access Management) group. Let’s call it TranslateGroup. Give it TranslateReadOnly permissions. Don’t know how to do this? Sign into your AWS console, and search for “IAM”. That will take you to the right place for dealing with IAM.

Add a new user to this group. Let’s call this user TranslateUser. Give it programmatic access only.

When you see your Access key ID and secret, copy them into your AWS credentials file (in Linux, this is located under ~/.aws/credentials). Set the header for the profile to be [TranslateUser].

Now that you’ve created a user, make sure you’ve installed the AWS PHP SDK. I did this in my demo directory, just by downloading the SDK and unzipping it. The contents of my directory are pretty simple:

~/TranslateDemo$ ls -lairt
total 164
18226436 drwxr-xr-x   3 fullstackdev fullstackdev     4096 Jul 11 15:06 Psr
18226304 drwxr-xr-x   2 fullstackdev fullstackdev     4096 Jul 11 15:06 JmesPath
18226324 drwxr-xr-x   7 fullstackdev fullstackdev     4096 Jul 11 15:06 GuzzleHttp
18226301 -rw-r--r--   1 fullstackdev fullstackdev   129259 Jul 11 15:06 aws-autoloader.php
18226446 drwxr-xr-x 197 fullstackdev fullstackdev    12288 Jul 11 15:06 Aws
   6961244 -rw-rw-r-- 1 fullstackdev fullstackdev      958 Sep 16 20:32 test_translate.php
...

It’s quick and easy to code up the rest. Here’s some demo code (test_translate.php):

<?php
require './aws-autoloader.php';

use Aws\Translate\TranslateClient;
use Aws\Exception\AwsException;

$client = new Aws\Translate\TranslateClient([
    'profile' => 'TranslateUser',
    'region' => 'us-west-2',
    'version' => 'latest'
]);

// Translate from English (en) to Spanish (es).
$currentLanguage = 'en';
$targetLanguage= 'es';
$textToTranslate = "Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.";

echo "Calling translateText function on '".$textToTranslate."'\n";

try {
    $result = $client->translateText([
        'SourceLanguageCode' => $currentLanguage,
        'TargetLanguageCode' => $targetLanguage,
        'Text' => $textToTranslate,
    ]);
    echo $result['TranslatedText']."\n";
} catch(AwsException $e) {
    // output error message if fails
    echo "Failed: ".$e->getMessage()."\n";
}

Run this from the command line: php test_translate.php. The output is:

Calling translateText function on 'Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.'
Llámame Ishmael. Hace algunos años, no importa cuánto tiempo precisamente— teniendo poco o ningún dinero en mi bolso, y nada particular que me interesara en la costa, pensé que navegaría un poco y vería la parte acuosa del mundo.

Pretty easy, right?