Skip to content

Instantly share code, notes, and snippets.

@tomill
Created April 11, 2010 11:20
Show Gist options
  • Save tomill/362661 to your computer and use it in GitHub Desktop.
Save tomill/362661 to your computer and use it in GitHub Desktop.
require("json");
require("socket.http")
require("socket.url")
function babelfish(lang, query)
lang = lang or "en"
query = query or ""
if lang == "en" or query == "" then
return query
end
local result, status = socket.http.request(
"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="
.. socket.url.escape(query)
.. "&langpair=en|"
.. lang
)
if not (status == 200 or status == 201) then
return "api error: " .. status
end
local data = json.decode(result)
if data.responseStatus == 200 then
return data.responseData.translatedText
else
return "status " .. data.responseStatus .. ": " .. data.responseDetails
end
end
local text = babelfish(arg[1], arg[2])
print(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment