Translate takes a language by its name or the start of it, in English, Norwegian or its own (>swe, >swedish, >svensk, >tysk), as well as by its code, and says so when what's named isn't a language rather than send it and get the text back unchanged
This commit is contained in:
3 files changed
+51
-16
No files matched your search
+45
-16
@@ -1,6 +1,6 @@
|
|||||||
-- Translate: "tr god morgen" translates what's typed into your language, or out of it when
|
-- 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
|
-- that's what it's already in; "tr >de good morning" names the language to translate to, by its
|
||||||
-- "tr en>de good morning" the one it's in as well. "tr" alone translates what's selected in the
|
-- code or its name (">swedish", ">tysk"), 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
|
-- 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,
|
-- opens Google Translate. Translate Selection replaces what's selected with its translation,
|
||||||
-- from a hotkey without opening Toolshed.
|
-- from a hotkey without opening Toolshed.
|
||||||
@@ -19,12 +19,16 @@ local longest = 5000
|
|||||||
local encode = toolshed.url_encode
|
local encode = toolshed.url_encode
|
||||||
local escape = toolshed.escape
|
local escape = toolshed.escape
|
||||||
|
|
||||||
-- The languages offered in Settings and named in results, by Google's codes.
|
-- The languages offered in Settings and named in results: Google's code, the language's own
|
||||||
|
-- name, which is shown, and what else it's called, in English and Norwegian, to name it by.
|
||||||
local languages = {
|
local languages = {
|
||||||
{ "en", "English" }, { "no", "Norsk" }, { "sv", "Svenska" }, { "da", "Dansk" }, { "fi", "Suomi" },
|
{ "en", "English", "engelsk" }, { "no", "Norsk", "norwegian", "bokmål", "nb", "nn" }, { "sv", "Svenska", "swedish", "svensk" },
|
||||||
{ "is", "Íslenska" }, { "de", "Deutsch" }, { "nl", "Nederlands" }, { "fr", "Français" }, { "es", "Español" },
|
{ "da", "Dansk", "danish" }, { "fi", "Suomi", "finnish", "finsk" }, { "is", "Íslenska", "icelandic", "islandsk" },
|
||||||
{ "pt", "Português" }, { "it", "Italiano" }, { "pl", "Polski" }, { "uk", "Українська" }, { "ru", "Русский" },
|
{ "de", "Deutsch", "german", "tysk" }, { "nl", "Nederlands", "dutch", "nederlandsk" }, { "fr", "Français", "french", "fransk" },
|
||||||
{ "tr", "Türkçe" }, { "ar", "العربية" }, { "hi", "हिन्दी" }, { "zh-CN", "中文" }, { "ja", "日本語" }, { "ko", "한국어" },
|
{ "es", "Español", "spanish", "spansk" }, { "pt", "Português", "portuguese", "portugisisk" }, { "it", "Italiano", "italian", "italiensk" },
|
||||||
|
{ "pl", "Polski", "polish", "polsk" }, { "uk", "Українська", "ukrainian", "ukrainsk" }, { "ru", "Русский", "russian", "russisk" },
|
||||||
|
{ "tr", "Türkçe", "turkish", "tyrkisk" }, { "ar", "العربية", "arabic", "arabisk" }, { "hi", "हिन्दी", "hindi" },
|
||||||
|
{ "zh-CN", "中文", "chinese", "kinesisk", "zh" }, { "ja", "日本語", "japanese", "japansk" }, { "ko", "한국어", "korean", "koreansk" },
|
||||||
}
|
}
|
||||||
|
|
||||||
local names, options = {}, {}
|
local names, options = {}, {}
|
||||||
@@ -33,6 +37,28 @@ for _, l in ipairs(languages) do
|
|||||||
table.insert(options, { value = l[1], title = l[2] })
|
table.insert(options, { value = l[1], title = l[2] })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- The code of the language typed: its code, or the start of any of its names, so "swe",
|
||||||
|
-- "swedish" and "svensk" are all Swedish. A code Google has that isn't listed here goes as it is;
|
||||||
|
-- anything else isn't a language, and saying so beats Google handing the text back unchanged.
|
||||||
|
local function code_of(typed)
|
||||||
|
local wanted = typed:lower()
|
||||||
|
for _, l in ipairs(languages) do
|
||||||
|
if l[1]:lower() == wanted then return l[1] end
|
||||||
|
end
|
||||||
|
for _, l in ipairs(languages) do
|
||||||
|
for i = 2, #l do
|
||||||
|
if #wanted >= 3 and l[i]:lower():sub(1, #wanted) == wanted then return l[1] end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
for _, l in ipairs(languages) do
|
||||||
|
for i = 3, #l do
|
||||||
|
if l[i] == wanted then return l[1] end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if wanted:match("^%a%a$") or wanted:match("^%a%a%-%a+$") then return typed end
|
||||||
|
error(toolshed.t("no-language", { name = typed }), 0)
|
||||||
|
end
|
||||||
|
|
||||||
-- Norwegian goes by three codes; Google answers with "no" whichever it was asked for.
|
-- Norwegian goes by three codes; Google answers with "no" whichever it was asked for.
|
||||||
local function same(a, b)
|
local function same(a, b)
|
||||||
local function plain(code) return (code == "nb" or code == "nn") and "no" or code end
|
local function plain(code) return (code == "nb" or code == "nn") and "no" or code end
|
||||||
@@ -59,10 +85,11 @@ local function ask(text, to, from, max_seconds)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- What's typed after the word: "en>de text" names both languages, ">de text" the one to
|
-- 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.
|
-- translate to, each by its code or its name. A language on its own isn't read as one, as "no"
|
||||||
|
-- and "en" are words too.
|
||||||
local function parse(rest)
|
local function parse(rest)
|
||||||
local from, to, text = rest:match("^([%a-]*)>([%a-]+)%s+(.+)$")
|
local from, to, text = rest:match("^([^%s>]*)>([^%s>]+)%s+(.+)$")
|
||||||
if to then return text, to, from ~= "" and from or "auto" end
|
if to then return text, code_of(to), from ~= "" and code_of(from) or "auto" end
|
||||||
return rest, nil, "auto"
|
return rest, nil, "auto"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -107,24 +134,26 @@ return toolshed.toolbox {
|
|||||||
key = "translate",
|
key = "translate",
|
||||||
title = "i18n:tool.translate.title",
|
title = "i18n:tool.translate.title",
|
||||||
words = { "tr", "translate" },
|
words = { "tr", "translate" },
|
||||||
examples = { "tr god morgen", "tr >de good morning", "tr en>fr good morning" },
|
examples = { "tr god morgen", "tr >swedish good morning", "tr en>fr good morning" },
|
||||||
keywords = { "translate", "google", "language", "oversett", "språk" },
|
keywords = { "translate", "google", "language", "oversett", "språk" },
|
||||||
-- Google is asked once typing pauses, not for every letter on the way.
|
-- Google is asked once typing pauses, not for every letter on the way.
|
||||||
debounce = true,
|
debounce = true,
|
||||||
items = true,
|
items = true,
|
||||||
|
|
||||||
search = function(rest, settings)
|
search = function(rest, settings)
|
||||||
local text, to, from = parse(rest)
|
local failed = function(why, text, from, to)
|
||||||
|
return { { title = toolshed.t("failed"), subtitle = tostring(why), icon = "info", action = "open",
|
||||||
|
data = { original = text, from = from or "auto", to = to or settings.language } } }
|
||||||
|
end
|
||||||
|
local understood, text, to, from = pcall(parse, rest)
|
||||||
|
if not understood then return failed(text, rest) end
|
||||||
local label
|
local label
|
||||||
if rest == "" then
|
if rest == "" then
|
||||||
text, label = toolshed.selection(), toolshed.t("selected")
|
text, label = toolshed.selection(), toolshed.t("selected")
|
||||||
if not text or text:match("^%s*$") then return {} end
|
if not text or text:match("^%s*$") then return {} end
|
||||||
end
|
end
|
||||||
local ok, result = pcall(translate, text, settings, to, from, 4)
|
local ok, result = pcall(translate, text, settings, to, from, 4)
|
||||||
if not ok then
|
if not ok then return failed(result, text, from, to) end
|
||||||
return { { title = toolshed.t("failed"), subtitle = tostring(result), icon = "info", action = "open",
|
|
||||||
data = { original = text, from = from, to = to or settings.language } } }
|
|
||||||
end
|
|
||||||
return { {
|
return { {
|
||||||
title = one_line(result.text),
|
title = one_line(result.text),
|
||||||
subtitle = name(result.from) .. " → " .. name(result.to),
|
subtitle = name(result.from) .. " → " .. name(result.to),
|
||||||
|
|||||||
@@ -51,3 +51,6 @@ other = "Select some text first"
|
|||||||
|
|
||||||
[too-long]
|
[too-long]
|
||||||
other = "That's more than {{.count}} characters: open Google Translate for it"
|
other = "That's more than {{.count}} characters: open Google Translate for it"
|
||||||
|
|
||||||
|
[no-language]
|
||||||
|
other = "There's no language called “{{.name}}” here: try its name or Google's code, like sv"
|
||||||
@@ -51,3 +51,6 @@ other = "Merk litt tekst først"
|
|||||||
|
|
||||||
[too-long]
|
[too-long]
|
||||||
other = "Det er mer enn {{.count}} tegn: åpne Google Translate for det"
|
other = "Det er mer enn {{.count}} tegn: åpne Google Translate for det"
|
||||||
|
|
||||||
|
[no-language]
|
||||||
|
other = "Her finnes ikke noe språk som heter «{{.name}}»: prøv navnet eller Googles kode, som sv"
|
||||||
Reference in new issue
Block a user