diff --git a/README.md b/README.md index 3008989..e20ac50 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Toolshed asks for updates once a day and installs one only when you accept it, a | `projects` | Opens repos with the `dev` command: `p toolshed` | | `scripts` | A place for your own small commands, each a tool with a hotkey | | `secrets` | Searches lazysecrets: `s db-password` | +| `translate` | Translates what you type or select with Google Translate, and replaces a selection from a hotkey: `tr god morgen` | | `wikipedia` | Finds articles and shows their summaries: `wiki rust` | | `yr` | The weather from yr.no: `yr bergen` | diff --git a/translate/init.lua b/translate/init.lua new file mode 100644 index 0000000..a04af96 --- /dev/null +++ b/translate/init.lua @@ -0,0 +1,201 @@ +-- Translate: "tr god morgen" translates what's typed into your language, or out of it when +-- that's what it's already in; "tr >de good morning" names the language to translate to, and +-- "tr en>de good morning" the one it's in as well. "tr" alone translates what's selected in the +-- app Toolshed was opened from. Enter pastes the translation; ⌘K copies it, shows both texts or +-- opens Google Translate. Translate Selection replaces what's selected with its translation, +-- from a hotkey without opening Toolshed. +-- +-- It asks the address Google Translate's own page asks, which needs no key but is nobody's +-- promise: Google may change it or turn requests away. What's translated is sent to Google. + +-- Said plainly, as an automated client should. +local agent = "Toolshed-Translate-extension/1.0" + +local address = "https://translate.googleapis.com/translate_a/single" + +-- Google takes far more, but a launcher isn't for translating documents. +local longest = 5000 + +local encode = toolshed.url_encode +local escape = toolshed.escape + +-- The languages offered in Settings and named in results, by Google's codes. +local languages = { + { "en", "English" }, { "no", "Norsk" }, { "sv", "Svenska" }, { "da", "Dansk" }, { "fi", "Suomi" }, + { "is", "Íslenska" }, { "de", "Deutsch" }, { "nl", "Nederlands" }, { "fr", "Français" }, { "es", "Español" }, + { "pt", "Português" }, { "it", "Italiano" }, { "pl", "Polski" }, { "uk", "Українська" }, { "ru", "Русский" }, + { "tr", "Türkçe" }, { "ar", "العربية" }, { "hi", "हिन्दी" }, { "zh-CN", "中文" }, { "ja", "日本語" }, { "ko", "한국어" }, +} + +local names, options = {}, {} +for _, l in ipairs(languages) do + names[l[1]] = l[2] + table.insert(options, { value = l[1], title = l[2] }) +end + +-- Norwegian goes by three codes; Google answers with "no" whichever it was asked for. +local function same(a, b) + local function plain(code) return (code == "nb" or code == "nn") and "no" or code end + return plain(a) == plain(b) +end + +local function name(code) + return names[code] or (same(code, "no") and names.no) or code +end + +-- Asks Google for text in the language to, from the language given or "auto" to have it found. +-- Returns the translation and the language it was in. +local function ask(text, to, from, max_seconds) + local reply = toolshed.json.decode(toolshed.exec({ + "curl", "-sSf", "--max-time", tostring(max_seconds), "-A", agent, + "-d", "client=gtx", "-d", "dt=t", "-d", "dj=1", "-d", "sl=" .. from, "-d", "tl=" .. to, + "--data-urlencode", "q=" .. text, address, + })) + local parts = {} + for _, sentence in ipairs(reply.sentences or {}) do + table.insert(parts, sentence.trans or "") + end + return table.concat(parts), reply.src or from +end + +-- What's typed after the word: "en>de text" names both languages, ">de text" the one to +-- translate to. A bare code isn't read as one, as "no" and "en" are words too. +local function parse(rest) + local from, to, text = rest:match("^([%a-]*)>([%a-]+)%s+(.+)$") + if to then return text, to, from ~= "" and from or "auto" end + return rest, nil, "auto" +end + +-- Translates text as the settings say: into your language, or, when it's in that already, into +-- the other one. A language named in what was typed is used as it is. +local function translate(text, settings, to, from, max_seconds) + if #text > longest then error(toolshed.t("too-long", { count = longest }), 0) end + if to then + local out, src = ask(text, to, from, max_seconds) + return { text = out, from = src, to = to, original = text } + end + local out, src = ask(text, settings.language, "auto", max_seconds) + if same(src, settings.language) and not same(settings.other, settings.language) then + out = ask(text, settings.other, src, max_seconds) + return { text = out, from = src, to = settings.other, original = text } + end + return { text = out, from = src, to = settings.language, original = text } +end + +local function one_line(text) + return (text:gsub("%s+", " ")) +end + +local function site(data) + return "https://translate.google.com/?sl=" .. encode(data.from) .. "&tl=" .. encode(data.to) .. "&text=" .. encode(data.original) .. "&op=translate" +end + +return toolshed.toolbox { + key = "translate", + title = "i18n:toolbox.title", + description = "i18n:toolbox.description", + icon = "languages", + tint = "blue", + + settings = { + { key = "language", title = "i18n:setting.language.title", description = "i18n:setting.language.description", type = "choice", default = "en", options = options }, + { key = "other", title = "i18n:setting.other.title", description = "i18n:setting.other.description", type = "choice", default = "no", options = options }, + }, + + tools = { + { + key = "translate", + title = "i18n:tool.translate.title", + words = { "tr", "translate" }, + examples = { "tr god morgen", "tr >de good morning", "tr en>fr good morning" }, + keywords = { "translate", "google", "language", "oversett", "språk" }, + -- Google is asked once typing pauses, not for every letter on the way. + debounce = true, + items = true, + + search = function(rest, settings) + local text, to, from = parse(rest) + local label + if rest == "" then + text, label = toolshed.selection(), toolshed.t("selected") + if not text or text:match("^%s*$") then return {} end + end + local ok, result = pcall(translate, text, settings, to, from, 4) + if not ok then + return { { title = toolshed.t("failed"), subtitle = tostring(result), icon = "info", action = "open", + data = { original = text, from = from, to = to or settings.language } } } + end + return { { + title = one_line(result.text), + subtitle = name(result.from) .. " → " .. name(result.to), + label = label, + data = result, + } } + end, + + actions = { + { + key = "paste", title = "i18n:action.paste", icon = "clipboard-paste", pastes = true, + run = function(ctx) + toolshed.clipboard.copy(ctx.data.text) + return { paste = true } + end, + }, + { + key = "copy", title = "i18n:action.copy", icon = "copy", shortcut = "cmd+shift+c", + run = function(ctx) + toolshed.clipboard.copy(ctx.data.text) + return toolshed.t("copied") + end, + }, + { + -- Both texts in full, for what's longer than a row shows. + key = "show", title = "i18n:action.show", icon = "book-a", + run = function(ctx) + local function block(code, text) + return '
' .. escape(name(code)) .. '
' + .. '
' .. escape(text) .. '
' + end + return { + html = '
' + .. block(ctx.data.to, ctx.data.text) .. block(ctx.data.from, ctx.data.original) .. '
', + source = "Google Translate", + jigs = { + { title = toolshed.t("action.paste"), icon = "clipboard-paste", action = "paste", data = ctx.data }, + { title = toolshed.t("action.copy"), icon = "copy", action = "copy", data = ctx.data }, + { title = toolshed.t("action.open"), icon = "external-link", action = "open", data = ctx.data }, + }, + } + end, + }, + { + key = "open", title = "i18n:action.open", icon = "external-link", shortcut = "cmd+enter", + run = function(ctx) toolshed.open(site(ctx.data)) end, + }, + }, + }, + { + -- A command: it puts the translation where the text was, so there's nothing to show. + key = "selection", + title = "i18n:tool.selection.title", + description = "i18n:tool.selection.description", + keywords = { "translate", "selection", "replace", "oversett", "merket" }, + background = true, + + actions = { + { + key = "replace", title = "i18n:tool.selection.title", pastes = true, + run = function(ctx) + local text = toolshed.selection() + if not text or text:match("^%s*$") then + return { message = toolshed.t("nothing-selected"), status = "warning" } + end + local result = translate(text, ctx.settings, nil, "auto", 8) + toolshed.clipboard.copy(result.text) + return { paste = true } + end, + }, + }, + }, + }, +} diff --git a/translate/locales/en.toml b/translate/locales/en.toml new file mode 100644 index 0000000..65e0f41 --- /dev/null +++ b/translate/locales/en.toml @@ -0,0 +1,53 @@ +[toolbox.title] +other = "Translate" + +[toolbox.description] +other = "Translates what you type or select, with Google Translate" + +[setting.language.title] +other = "Your language" + +[setting.language.description] +other = "What text is translated into" + +[setting.other.title] +other = "Other language" + +[setting.other.description] +other = "What text already in your language is translated into" + +[tool.translate.title] +other = "Translate" + +[tool.selection.title] +other = "Translate Selection" + +[tool.selection.description] +other = "Replaces the text you selected with its translation. Give it a hotkey to translate without opening Toolshed." + +[action.paste] +other = "Paste Translation" + +[action.copy] +other = "Copy Translation" + +[action.show] +other = "Show Both Texts" + +[action.open] +other = "Open in Google Translate" + +[selected] +other = "Selected" + +[copied] +other = "Translation copied" + +[failed] +other = "Couldn't translate it" + +[nothing-selected] +other = "Select some text first" + +[too-long] +other = "That's more than {{.count}} characters: open Google Translate for it" diff --git a/translate/locales/nb.toml b/translate/locales/nb.toml new file mode 100644 index 0000000..ef9df18 --- /dev/null +++ b/translate/locales/nb.toml @@ -0,0 +1,53 @@ +[toolbox.title] +other = "Oversett" + +[toolbox.description] +other = "Oversetter det du skriver eller har merket, med Google Translate" + +[setting.language.title] +other = "Språket ditt" + +[setting.language.description] +other = "Det tekst oversettes til" + +[setting.other.title] +other = "Annet språk" + +[setting.other.description] +other = "Det tekst som alt er på språket ditt, oversettes til" + +[tool.translate.title] +other = "Oversett" + +[tool.selection.title] +other = "Oversett det merkede" + +[tool.selection.description] +other = "Bytter ut teksten du har merket med oversettelsen. Gi den en hurtigtast for å oversette uten å åpne Toolshed." + +[action.paste] +other = "Lim inn oversettelsen" + +[action.copy] +other = "Kopier oversettelsen" + +[action.show] +other = "Vis begge tekstene" + +[action.open] +other = "Åpne i Google Translate" + +[selected] +other = "Merket" + +[copied] +other = "Oversettelsen er kopiert" + +[failed] +other = "Kunne ikke oversette" + +[nothing-selected] +other = "Merk litt tekst først" + +[too-long] +other = "Det er mer enn {{.count}} tegn: åpne Google Translate for det"