Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift) Amazon Translate TextDemonstrates how to use the AWS Translate service to translate text from one language to another.
func chilkatTest() { // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. let rest = CkoRest()! var success: Bool let authAws = CkoAuthAws()! authAws.accessKey = "AWS_ACCESS_KEY" authAws.secretKey = "AWS_SECRET_KEY" authAws.region = "us-west-2" authAws.serviceName = "translate" rest.setAuthAws(authAws) // URL: https://translate.us-west-2.amazonaws.com/ var bTls: Bool = true var port: Int = 443 var bAutoReconnect: Bool = true success = rest.connect("translate.us-west-2.amazonaws.com", port: port, tls: bTls, autoReconnect: bAutoReconnect) if success != true { print("ConnectFailReason: \(rest.connectFailReason.intValue)") print("\(rest.lastErrorText!)") return } // Translate text from English to German let json = CkoJsonObject()! json.update("SourceLanguageCode", value: "en") json.update("TargetLanguageCode", value: "de") json.update("Text", value: "This is the text to be translated") rest.addHeader("Content-Type", value: "application/x-amz-json-1.1") rest.addHeader("X-Amz-Target", value: "AWSShineFrontendService_20170701.TranslateText") let sbRequestBody = CkoStringBuilder()! json.emitSb(sbRequestBody) let sbResponseBody = CkoStringBuilder()! success = rest.fullRequestSb("POST", uriPath: "/", requestBody: sbRequestBody, responseBody: sbResponseBody) if success != true { print("\(rest.lastErrorText!)") return } var respStatusCode: Int = rest.responseStatusCode.intValue if respStatusCode >= 400 { print("Response Status Code = \(respStatusCode)") print("Response Header:") print("\(rest.responseHeader!)") print("Response Body:") print("\(sbResponseBody.getAsString()!)") return } let jsonResponse = CkoJsonObject()! jsonResponse.loadSb(sbResponseBody) jsonResponse.emitCompact = false print("\(jsonResponse.emit()!)") // { // "SourceLanguageCode": "en", // "TargetLanguageCode": "de", // "TranslatedText": "Dies ist der zu �bersetzende Text" // } print("\(jsonResponse.string(of: "TranslatedText")!)") } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.