Xbase++
Xbase++
Amazon Translate Text
See more AWS Translate Examples
Demonstrates how to use the AWS Translate service to translate text from one language to another.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRest
LOCAL oAuthAws
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oJson
LOCAL oSbRequestBody
LOCAL oSbResponseBody
LOCAL nRespStatusCode
LOCAL oJsonResponse
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oRest := CreateObject("Chilkat.Rest")
oAuthAws := CreateObject("Chilkat.AuthAws")
oAuthAws:AccessKey := "AWS_ACCESS_KEY"
oAuthAws:SecretKey := "AWS_SECRET_KEY"
oAuthAws:Region := "us-west-2"
oAuthAws:ServiceName := "translate"
oRest:SetAuthAws(oAuthAws)
// URL: https://translate.us-west-2.amazonaws.com/
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("translate.us-west-2.amazonaws.com", nPort, nBTls, nBAutoReconnect)
IF (nSuccess != 1)
? "ConnectFailReason: " + Str(oRest:ConnectFailReason)
? oRest:LastErrorText
oRest:destroy()
oAuthAws:destroy()
RETURN
ENDIF
// Translate text from English to German
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("SourceLanguageCode", "en")
oJson:UpdateString("TargetLanguageCode", "de")
oJson:UpdateString("Text", "This is the text to be translated")
oRest:AddHeader("Content-Type", "application/x-amz-json-1.1")
oRest:AddHeader("X-Amz-Target", "AWSShineFrontendService_20170701.TranslateText")
oSbRequestBody := CreateObject("Chilkat.StringBuilder")
oJson:EmitSb(oSbRequestBody)
oSbResponseBody := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestSb("POST", "/", oSbRequestBody, oSbResponseBody)
IF (nSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
oAuthAws:destroy()
oJson:destroy()
oSbRequestBody:destroy()
oSbResponseBody:destroy()
RETURN
ENDIF
nRespStatusCode := oRest:ResponseStatusCode
IF (nRespStatusCode >= 400)
? "Response Status Code = " + Str(nRespStatusCode)
? "Response Header:"
? oRest:ResponseHeader
? "Response Body:"
? oSbResponseBody:GetAsString()
oRest:destroy()
oAuthAws:destroy()
oJson:destroy()
oSbRequestBody:destroy()
oSbResponseBody:destroy()
RETURN
ENDIF
oJsonResponse := CreateObject("Chilkat.JsonObject")
oJsonResponse:LoadSb(oSbResponseBody)
oJsonResponse:EmitCompact := 0
? oJsonResponse:Emit()
// {
// "SourceLanguageCode": "en",
// "TargetLanguageCode": "de",
// "TranslatedText": "Dies ist der zu �bersetzende Text"
// }
? oJsonResponse:StringOf("TranslatedText")
oRest:destroy()
oAuthAws:destroy()
oJson:destroy()
oSbRequestBody:destroy()
oSbResponseBody:destroy()
oJsonResponse:destroy()