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

76 lines
1.5 KiB
Lua
Raw Normal View History

local Trans = require('Trans')
local util = Trans.util
2023-03-09 19:42:41 +08:00
local backends = {
'offline',
'baidu',
}
2023-03-09 19:42:41 +08:00
local function new_data(opts)
2023-03-09 19:42:41 +08:00
opts = opts or {}
local method = opts.method or ({
n = 'normal',
v = 'visual',
})(vim.api.nvim_get_mode().mode)
local str = util.get_str(method)
2023-03-09 19:42:41 +08:00
if str == '' then return end
local strategy = Trans.conf.strategy[method] or Trans.conf.strategy
2023-03-09 19:42:41 +08:00
local data = {
str = str,
method = method,
frontend = strategy.frontend,
2023-03-09 19:42:41 +08:00
}
local backend = strategy.backend
if type(backend) == 'string' then
backend = backend == '*' and backends or { backend }
end
data.backend = backend
2023-03-09 19:42:41 +08:00
if util.is_English(str) then
data.from = 'en'
data.to = 'zh'
else
data.from = 'zh'
data.to = 'en'
end
return data
end
2023-03-09 19:42:41 +08:00
local function set_result(data)
require('Trans.backend').baidu.query(data)
local thread = coroutine.running()
local resume = function()
coroutine.resume(thread)
end
local time = 0
while data.result == nil do
vim.defer_fn(resume, 400)
time = time + 1
print('waiting' .. ('.'):rep(time))
coroutine.yield()
end
vim.pretty_print(data)
end
local function render_window()
end
local function process(opts)
Trans.translate = coroutine.wrap(process)
local data = new_data(opts)
if not data then return end
set_result(data)
if data.result == false then return end
2023-03-09 19:42:41 +08:00
render_window()
2023-03-09 19:42:41 +08:00
end
return coroutine.wrap(process)