TranslateTextÚselo con un AWS SDKo CLI - Amazon Translate

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

TranslateTextÚselo con un AWS SDKo CLI

En los siguientes ejemplos de código, se muestra cómo utilizar TranslateText.

.NET
AWS SDK for .NET
nota

Hay más información GitHub. Consulta el ejemplo completo y aprende a configurarlo y a ejecutarlo en AWS Repositorio de ejemplos de código.

using System; using System.IO; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Transfer; using Amazon.Translate; using Amazon.Translate.Model; /// <summary> /// Take text from a file stored a Amazon Simple Storage Service (Amazon S3) /// object and translate it using the Amazon Transfer Service. /// </summary> public class TranslateText { public static async Task Main() { // If the region you want to use is different from the region // defined for the default user, supply it as a parameter to the // Amazon Translate client object constructor. var client = new AmazonTranslateClient(); // Set the source language to "auto" to request Amazon Translate to // automatically detect te language of the source text. // You can get a list of the languages supposed by Amazon Translate // in the Amazon Translate Developer's Guide here: // https://docs.aws.amazon.com/translate/latest/dg/what-is.html string srcLang = "en"; // English. string destLang = "fr"; // French. // The Amazon Simple Storage Service (Amazon S3) bucket where the // source text file is stored. string srcBucket = "DOC-EXAMPLE-BUCKET"; string srcTextFile = "source.txt"; var srcText = await GetSourceTextAsync(srcBucket, srcTextFile); var destText = await TranslatingTextAsync(client, srcLang, destLang, srcText); ShowText(srcText, destText); } /// <summary> /// Use the Amazon S3 TransferUtility to retrieve the text to translate /// from an object in an S3 bucket. /// </summary> /// <param name="srcBucket">The name of the S3 bucket where the /// text is stored. /// </param> /// <param name="srcTextFile">The key of the S3 object that /// contains the text to translate.</param> /// <returns>A string representing the source text.</returns> public static async Task<string> GetSourceTextAsync(string srcBucket, string srcTextFile) { string srcText = string.Empty; var s3Client = new AmazonS3Client(); TransferUtility utility = new TransferUtility(s3Client); using var stream = await utility.OpenStreamAsync(srcBucket, srcTextFile); StreamReader file = new System.IO.StreamReader(stream); srcText = file.ReadToEnd(); return srcText; } /// <summary> /// Use the Amazon Translate Service to translate the document from the /// source language to the specified destination language. /// </summary> /// <param name="client">The Amazon Translate Service client used to /// perform the translation.</param> /// <param name="srcLang">The language of the source text.</param> /// <param name="destLang">The destination language for the translated /// text.</param> /// <param name="text">A string representing the text to ranslate.</param> /// <returns>The text that has been translated to the destination /// language.</returns> public static async Task<string> TranslatingTextAsync(AmazonTranslateClient client, string srcLang, string destLang, string text) { var request = new TranslateTextRequest { SourceLanguageCode = srcLang, TargetLanguageCode = destLang, Text = text, }; var response = await client.TranslateTextAsync(request); return response.TranslatedText; } /// <summary> /// Show the original text followed by the translated text. /// </summary> /// <param name="srcText">The original text to be translated.</param> /// <param name="destText">The translated text.</param> public static void ShowText(string srcText, string destText) { Console.WriteLine("Source text:"); Console.WriteLine(srcText); Console.WriteLine(); Console.WriteLine("Translated text:"); Console.WriteLine(destText); } }
  • Para API obtener más información, consulte TranslateTexten AWS SDK for .NET APIReferencia.

PowerShell
Herramientas para PowerShell

Ejemplo 1: convierte el texto en inglés especificado en francés. El texto que se va a convertir también se puede pasar como parámetro -Text.

"Hello World" | ConvertTo-TRNTargetLanguage -SourceLanguageCode en -TargetLanguageCode fr
  • Para API obtener más información, consulte TranslateTexten AWS Tools for PowerShell Referencia de cmdlets.

SAP ABAP
SDKpara SAP ABAP
nota

Hay más información GitHub. Consulta el ejemplo completo y aprende a configurarlo y a ejecutarlo en AWS Repositorio de ejemplos de código.

"Translates input text from the source language to the target language." TRY. oo_result = lo_xl8->translatetext( "oo_result is returned for testing purposes." EXPORTING iv_text = iv_text iv_sourcelanguagecode = iv_sourcelanguagecode iv_targetlanguagecode = iv_targetlanguagecode ). MESSAGE 'Translation completed.' TYPE 'I'. CATCH /aws1/cx_xl8detectedlanguage00 . MESSAGE 'The confidence that Amazon Comprehend accurately detected the source language is low.' TYPE 'E'. CATCH /aws1/cx_xl8internalserverex . MESSAGE 'An internal server error occurred.' TYPE 'E'. CATCH /aws1/cx_xl8invalidrequestex . MESSAGE 'The request that you made is not valid.' TYPE 'E'. CATCH /aws1/cx_xl8resourcenotfoundex . MESSAGE 'The resource you are looking for has not been found.' TYPE 'E'. CATCH /aws1/cx_xl8serviceunavailex . MESSAGE 'The Amazon Translate service is temporarily unavailable.' TYPE 'E'. CATCH /aws1/cx_xl8textsizelmtexcdex . MESSAGE 'The size of the text you submitted exceeds the size limit. ' TYPE 'E'. CATCH /aws1/cx_xl8toomanyrequestsex . MESSAGE 'You have made too many requests within a short period of time.' TYPE 'E'. CATCH /aws1/cx_xl8unsuppedlanguage00 . MESSAGE 'Amazon Translate does not support translation from the language of the source text into the requested target language. ' TYPE 'E'. ENDTRY.
  • Para API obtener más información, consulte TranslateTexten AWS SDKcomo SAP ABAP API referencia.

Para obtener una lista completa de AWS SDKguías para desarrolladores y ejemplos de código, consulteUso de este servicio con un AWS SDK. En este tema también se incluye información sobre cómo empezar y detalles sobre SDK las versiones anteriores.