2018-10-30 22:02:13 -07:00
< ? php
2018-12-28 10:35:52 -08:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2018-10-30 22:02:13 -07:00
// SPDX-License-Identifier: Apache-2.0
2024-02-05 10:49:20 -07:00
2018-10-30 22:02:13 -07:00
/*
* ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/iam-examples-working-with-policies.html
2024-02-05 10:49:20 -07:00
2018-10-30 22:02:13 -07:00
*
*/
2019-02-01 19:59:35 -08:00
// snippet-start:[translate.php.traslate_text.complete]
// snippet-start:[translate.php.traslate_text.import]
2024-02-05 10:49:20 -07:00
2018-10-30 22:02:13 -07:00
require 'vendor/autoload.php' ;
2024-02-05 10:49:20 -07:00
2018-10-30 22:02:13 -07:00
use Aws\Exception\AwsException ;
2024-02-05 10:49:20 -07:00
2019-02-01 19:59:35 -08:00
// snippet-end:[translate.php.traslate_text.import]
2024-02-05 10:49:20 -07:00
2018-10-30 22:02:13 -07:00
/**
2018-12-05 12:59:35 -08:00
* Translate a text from Arabic (ar), Chinese (Simplified) (zh)
2018-10-30 22:02:13 -07:00
* 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:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
2019-02-01 19:59:35 -08:00
// snippet-start:[translate.php.traslate_text.main]
2018-10-30 22:02:13 -07:00
//Create a Translate Client
$client = new Aws\Translate\TranslateClient ([
'profile' => 'default' ,
'region' => 'us-west-2' ,
'version' => '2017-07-01'
]);
2024-02-05 10:49:20 -07:00
2018-10-30 22:02:13 -07:00
// Arabic (ar), Chinese (Simplified) (zh), English (en)
// French (fr), German (de), Portuguese (pt), or Spanish (es)
2024-02-05 10:49:20 -07:00
2018-12-05 12:59:35 -08:00
$currentLanguage = 'es' ;
2024-02-05 10:49:20 -07:00
2018-10-30 22:02:13 -07:00
// If the TargetLanguageCode is not "en", the SourceLanguageCode must be "en".
2018-12-05 12:59:35 -08:00
$targetLanguage = 'en' ;
2024-02-05 10:49:20 -07:00
2018-12-05 12:59:35 -08:00
$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 aws.zip o aws.phar independiente' ;
2024-02-05 10:49:20 -07:00
2018-10-30 22:02:13 -07:00
try {
$result = $client -> translateText ([
2018-12-05 12:59:35 -08:00
'SourceLanguageCode' => $currentLanguage ,
'TargetLanguageCode' => $targetLanguage ,
'Text' => $textToTranslate ,
2018-10-30 22:02:13 -07:00
]);
var_dump ( $result );
} catch ( AwsException $e ) {
// output error message if fails
echo $e -> getMessage ();
echo " \n " ;
}
2024-02-05 10:49:20 -07:00
2019-02-01 19:59:35 -08:00
// snippet-end:[translate.php.traslate_text.main]
// snippet-end:[translate.php.traslate_text.complete]