Trans.nvim/lua/Trans/core/translate.lua

128 lines
3.5 KiB
Lua
Raw Normal View History

local Trans = require('Trans')
local util = Trans.util
2023-03-09 19:42:41 +08:00
2023-03-12 21:33:00 +08:00
local function init_opts(opts)
2023-03-09 19:42:41 +08:00
opts = opts or {}
2023-03-12 21:33:00 +08:00
opts.mode = opts.mode or ({
n = 'normal',
v = 'visual',
2023-03-11 11:32:13 +08:00
})[vim.api.nvim_get_mode().mode]
2023-03-12 21:33:00 +08:00
opts.str = util.get_str(opts.mode)
return opts
end
local function do_query(data, backend)
-- TODO : template method for online query
local name = backend.name
local uri = backend.uti
local method = backend.method
local formatter = backend.formatter
local query = backend.get_query(data)
local header
if backend.header then
if type(backend.header) == "function" then
header = backend.header(data)
else
header = backend.header
end
end
local function handle(output)
local status, body = pcall(vim.json.decode, output.body)
-- -- vim.print(body)
if not status or not body or body.errorCode ~= "0" then
if not Trans.conf.debug then backend.debug(body) end
data.result[name] = false
data[#data + 1] = output
return
end
-- check_untracked_field(body)
-- if not body.isWord then
-- data.result.youdao = {
-- title = body.query,
-- [data.from == 'en' and 'translation' or 'definition'] = body.translation,
-- }
-- return
-- end
-- local tmp = {
-- title = {
-- word = body.query,
-- phonetic = body.basic.phonetic,
-- },
-- web = body.web,
-- explains = body.basic.explains,
-- -- phrases = body.phrases,
-- -- synonyms = body.synonyms,
-- -- sentenceSample = body.sentenceSample,
-- [data.from == 'en' and 'translation' or 'definition'] = body.translation,
-- }
data.result[name] = formatter and formatter(output) or output
end
Trans.curl[method](uri, {
query = query,
callback = handle,
header = header,
})
-- Hook ?
end
---@type table<string, fun(data: TransData): true | nil>
2023-03-15 20:57:28 +08:00
local strategy = {
fallback = function(data)
2023-03-15 20:57:28 +08:00
local result = data.result
Trans.backend.offline.query(data)
if result.offline then return true end
local update = data.frontend:wait()
2023-03-15 20:57:28 +08:00
for _, backend in ipairs(data.backends) do
---@cast backend TransBackend
backend.query(data)
local name = backend.name
while result[name] == nil do
if not update() then return end
2023-03-15 20:57:28 +08:00
end
if result[name] then return true end
end
2023-03-16 00:03:13 +08:00
end,
--- TODO :More Strategys
2023-03-15 20:57:28 +08:00
}
2023-03-11 11:32:13 +08:00
2023-03-12 21:33:00 +08:00
2023-03-11 11:32:13 +08:00
-- HACK : Core process logic
local function process(opts)
2023-03-12 21:33:00 +08:00
opts = init_opts(opts)
local str = opts.str
if not str or str == '' then return end
-- Find in cache
if Trans.cache[str] then
local data = Trans.cache[str]
data.frontend:process(data)
return
2023-03-12 21:33:00 +08:00
end
2023-03-13 19:50:28 +08:00
local data = Trans.data.new(opts)
if strategy[Trans.conf.query](data) then
Trans.cache[data.str] = data
data.frontend:process(data)
else
data.frontend:fallback()
end
2023-03-09 19:42:41 +08:00
end
2023-03-14 18:17:07 +08:00
---@class Trans
2023-03-15 11:34:50 +08:00
---@field translate fun(opts: { frontend: string?, mode: string?}?) Translate string core function
2023-03-14 18:17:07 +08:00
return function(opts)
coroutine.wrap(process)(opts)
end