Utilisation TranslateText avec un AWS SDK ou une CLI - Amazon Translate

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Utilisation TranslateText avec un AWS SDK ou une CLI

Les exemples de code suivants montrent comment utiliserTranslateText.

.NET
AWS SDK for .NET
Note

Il y en a plus à ce sujet GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code AWS.

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); } }
  • Pour plus de détails sur l'API, reportez-vous TranslateTextà la section Référence des AWS SDK for .NET API.

PowerShell
Outils pour PowerShell

Exemple 1 : convertit le texte anglais spécifié en français. Le texte à convertir peut également être transmis en tant que paramètre -Text.

"Hello World" | ConvertTo-TRNTargetLanguage -SourceLanguageCode en -TargetLanguageCode fr
  • Pour plus de détails sur l'API, reportez-vous TranslateTextà la section Référence des AWS Tools for PowerShell applets de commande.

SAP ABAP
Kit SDK pour SAP ABAP
Note

Il y en a plus à ce sujet GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code AWS.

"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.
  • Pour plus de détails sur l'API, reportez-vous TranslateTextà la section de référence du AWS SDK pour l'API SAP ABAP.

Pour obtenir la liste complète des guides de développement du AWS SDK et des exemples de code, consultezUtilisation de ce service avec un AWS SDK. Cette rubrique comprend également des informations sur le démarrage et sur les versions précédentes de SDK.