与 AWS SDK或TranslateText一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或TranslateText一起使用 CLI

以下代码示例演示如何使用 TranslateText

.NET
AWS SDK for .NET
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 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); } }
  • 有关API详细信息,请参阅 “AWS SDK for .NET API参考 TranslateText” 中的。

PowerShell
用于 PowerShell

示例 1:将指定的英文文本转换为法语。要转换的文本也可以作为-Text参数传递。

"Hello World" | ConvertTo-TRNTargetLanguage -SourceLanguageCode en -TargetLanguageCode fr
  • 有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考TranslateText中的。

SAP ABAP
SDK对于 SAP ABAP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 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.
  • 有关API详细信息,请参阅TranslateText中的AWS SDK以供SAPABAPAPI参考