Skip to content

Instantly share code, notes, and snippets.

@ur4ltz
Forked from tomill/googletranslate.lua
Created October 20, 2021 04:18
Show Gist options
  • Save ur4ltz/b6744e1c5ef733c98980f776c962ff6a to your computer and use it in GitHub Desktop.
Save ur4ltz/b6744e1c5ef733c98980f776c962ff6a 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