Unfortunately, the callback bit takes the text being returned and wraps it as a function call to whatever text is given in the callback field, which produces an error when you try to have yajl.to_value parse it. While obviously I can still pull the number out that way, I'm not sure I would call it "cleaner". But, I suppose since it won't be translated, and there is the chance that the number will be if it is part of the translation string, it is potentially less problematic.
But, if you just use a single string.match call, you can avoid the issues with the new string and yajl.to_value, and simplify things a bit. So, here is the ever so slightly more optimized version.
-- Automatic Translation Script
-- Pick from the languages below, using the full language name as listed in the languages table
-- If the original language is unknown, leave the translate_from variable nil
local translate_to = "english"
local translate_from = nil
local languages = {
azerbaijan = "az", maltese = "mt",
albanian = "sq", macedonian = "mk",
amharic = "am", maori = "mi",
english = "en", marathi = "mr",
arabic = "ar", mari = "mhr",
armenian = "hy", mongolian = "mn",
afrikaans = "af", german = "de",
basque = "eu", nepali = "ne",
bashkir = "ba", norwegian = "no",
belarusian = "be", punjabi = "pa",
bengali = "bn", papiamento = "pap",
bulgarian = "bg", persian = "fa",
bosnian = "bs", polish = "pl",
welsh = "cy", portuguese = "pt",
hungarian = "hu", romanian = "ro",
vietnamese = "vi", russian = "ru",
haitian = "ht", cebuano = "ceb",
galician = "gl", serbian = "sr",
dutch = "nl", sinhala = "si",
["hill mari"] = "mrj", slovakian = "sk",
greek = "el", slovenian = "sl",
georgian = "ka", swahili = "sw",
gujarati = "gu", sundanese = "su",
danish = "da", tajik = "tg",
hebrew = "he", thai = "th",
yiddish = "yi", tagalog = "tl",
indonesian = "id", tamil = "ta",
irish = "ga", tatar = "tt",
italian = "it", telugu = "te",
icelandic = "is", turkish = "tr",
spanish = "es", udmurt = "udm",
kazakh = "kk", uzbek = "uz",
kannada = "kn", ukrainian = "uk",
catalan = "ca", urdu = "ur",
kyrgyz = "ky", finnish = "fi",
chinese = "zh", french = "fr",
korean = "ko", hindi = "hi",
xhosa = "xh", croatian = "hr",
latin = "la", czech = "cs",
latvian = "lv", swedish = "sv",
lithuanian = "lt", scottish = "gd",
luxembourgish = "lb", estonian = "et",
malagasy = "mg", esperanto = "eo",
malay = "ms", javanese = "jv",
malayalam = "ml", japanese = "ja",
}
function downloadedTranslation(_, filename)
if not filename:find("translation", 1, true) then return end
local f, text, ln = io.open(filename)
if f then text = f:read("*a"); io.close(f) end
ln, text = string.match(text,[[(%w+)%({"code":%w+,"lang":"[%w-]+","text":%["(.*)"%]}%)]])
if ln ~= "print" then
moveCursor(0,ln)
cinsertText(string.format("<dark_slate_gray>(%s)<reset> ",text))
else
print(text)
end
end
registerAnonymousEventHandler("sysDownloadDone", "downloadedTranslation")
function translate(text,reverse)
local apikey = "trnsl.1.1.20170412T072751Z.75e604ad6edc1b26.805ffe27a7253c7b241b3e27204ab61e20e7fc79"
local translationid = (reverse and "print") or getLineNumber()
local target_language
if reverse then
target_language = languages[translate_to] .. "-" .. (languages[reverse] or languages[translate_from])
else
target_language = languages[translate_to]
if languages[translate_from] then
target_language = languages[translate_from] .. "-" .. target_language
end
end
if text == "" then return end
local url = string.format([[https://translate.yandex.net/api/v1.5/tr.json/translate?key=%s&text=%s&lang=%s&format=plain&callback=%s]],
apikey, text, target_language, translationid)
downloadFile(getMudletHomeDir().."/translation", url)
end
Edit: Added a little something so you could prompt it to give a reverse translation printed out for you, in case you wanted something from your native language translated into the target language, perhaps to send back to the game you are playing). To do that, pass the translate function the name of your desired language to translate to, or true (the boolean value) if you have a "translate_from" language set.
Ex: translate("This is a test.","czech")
or
translate("This is a test.", true)