Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local rest = chilkat.Rest.new()

local authAws = chilkat.AuthAws.new()
authAws.access_key = "AWS_ACCESS_KEY"
authAws.secret_key = "AWS_SECRET_KEY"
authAws.region = "us-west-2"
authAws.service_name = "translate"
rest:set_auth_aws(authAws)

-- URL: https://translate.us-west-2.amazonaws.com/
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("translate.us-west-2.amazonaws.com", port, bTls, bAutoReconnect)
if not success then
    print("ConnectFailReason: " .. rest.connect_fail_reason)
    print(rest.last_error_text)
    return
end

-- Translate text from English to German
local json = chilkat.JsonObject.new()
json:update_string("SourceLanguageCode", "en")
json:update_string("TargetLanguageCode", "de")
json:update_string("Text", "This is the text to be translated")

rest:add_header("Content-Type", "application/x-amz-json-1.1")
rest:add_header("X-Amz-Target", "AWSShineFrontendService_20170701.TranslateText")

local sbRequestBody = chilkat.StringBuilder.new()
json:emit_sb(sbRequestBody)
local sbResponseBody = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/", sbRequestBody, sbResponseBody)
if not success then
    print(rest.last_error_text)
    return
end

local respStatusCode = rest.response_status_code
if respStatusCode >= 400 then
    print("Response Status Code = " .. respStatusCode)
    print("Response Header:")
    print(rest.response_header)
    print("Response Body:")
    print(sbResponseBody:get_as_string())
    return
end

local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load_sb(sbResponseBody)
jsonResponse.emit_compact = false

print(jsonResponse:emit())

-- {
--   "SourceLanguageCode": "en",
--   "TargetLanguageCode": "de",
--   "TranslatedText": "Dies ist der zu �bersetzende Text"
-- }

print(jsonResponse:string_of("TranslatedText"))