TranslateText.php - AWS Code Sample


TranslateText.php demonstrates how to translate a piece of Arabic (ar), Chinese (Simplified) (zh), English (en), French (fr), German (de), Portuguese (pt), or Spanish (es) text into English with the Translate API.

<?php /** * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * * ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at * * */ require 'vendor/autoload.php'; use Aws\Translate\TranslateClient; use Aws\Exception\AwsException; /** * Translate a text from Arabic (ar), Chinese (Simplified) (zh) * French (fr), German (de), Portuguese (pt), or Spanish (es) * into English (en) with Translate client. * * This code expects that you have AWS credentials set up per: * */ //Create a Translate Client $client = new Aws\Translate\TranslateClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2017-07-01' ]); // Arabic (ar), Chinese (Simplified) (zh), English (en) // French (fr), German (de), Portuguese (pt), or Spanish (es) $currentLanguage = 'es'; // If the TargetLanguageCode is not "en", the SourceLanguageCode must be "en". $targetLanguage= 'en'; $textToTranslate = 'El AWS SDK for PHP versión 3 permite a los desarrolladores de PHP utilizar Amazon Web Services en su código PHP y crear aplicaciones y software robustos utilizando servicios como Amazon S3, Amazon DynamoDB, Amazon Glacier, etc. Puede empezar rápidamente instalando el SDK mediante Composer (solicitando el paquete aws/aws-sdk-php) o descargando el archivo o aws.phar independiente'; try { $result = $client->translateText([ 'SourceLanguageCode' => $currentLanguage, 'TargetLanguageCode' => $targetLanguage, 'Text' => $textToTranslate, ]); var_dump($result); }catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Sample Details

Service: translate

Last tested: 2018-12-27

Author: jschwarzwalder (AWS)

Type: full-example