2023-03-24 00:56:36 +08:00
|
|
|
local Trans = require 'Trans'
|
2023-03-09 19:42:41 +08:00
|
|
|
|
2023-05-06 11:38:37 +08:00
|
|
|
local function process(opts)
|
2023-03-09 19:42:41 +08:00
|
|
|
opts = opts or {}
|
2023-03-22 23:09:09 +08:00
|
|
|
opts.mode = opts.mode or vim.fn.mode()
|
2023-05-06 11:38:37 +08:00
|
|
|
local str = Trans.util.get_str(opts.mode)
|
|
|
|
opts.str = str
|
2023-03-18 18:20:01 +08:00
|
|
|
|
2023-09-08 22:44:23 +08:00
|
|
|
|
|
|
|
if not str or str == '' then
|
|
|
|
Trans.debug 'No string to translate'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if opts.from == nil and opts.to == nil then
|
|
|
|
-- INFO : Default support [zh -> en] or [en -> zh]
|
|
|
|
if Trans.util.is_english(str) then
|
|
|
|
opts.from = 'en'
|
|
|
|
opts.to = 'zh'
|
|
|
|
else
|
|
|
|
opts.from = 'zh'
|
|
|
|
opts.to = 'en'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert(opts.from and opts.to, 'opts.from and opts.to must be set at the same time')
|
|
|
|
|
|
|
|
opts.is_word = opts.is_word or Trans.util.is_word(str)
|
2023-03-12 21:33:00 +08:00
|
|
|
|
2023-03-23 17:31:02 +08:00
|
|
|
|
2023-03-12 21:33:00 +08:00
|
|
|
-- Find in cache
|
|
|
|
if Trans.cache[str] then
|
2023-03-12 22:12:33 +08:00
|
|
|
local data = Trans.cache[str]
|
2023-09-08 22:44:23 +08:00
|
|
|
return data.frontend:process(data)
|
2023-03-12 21:33:00 +08:00
|
|
|
end
|
2023-03-11 00:24:48 +08:00
|
|
|
|
2023-09-08 22:44:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Create new data
|
2023-03-13 19:50:28 +08:00
|
|
|
local data = Trans.data.new(opts)
|
2023-05-06 11:38:37 +08:00
|
|
|
if Trans.strategy[data.frontend.opts.query](data) then
|
2023-03-16 11:49:26 +08:00
|
|
|
Trans.cache[data.str] = data
|
2023-03-16 16:07:44 +08:00
|
|
|
data.frontend:process(data)
|
|
|
|
else
|
|
|
|
data.frontend:fallback()
|
|
|
|
end
|
2023-03-09 19:42:41 +08:00
|
|
|
end
|
|
|
|
|
2023-05-06 11:38:37 +08:00
|
|
|
|
2023-09-08 22:44:23 +08:00
|
|
|
---@class TransDataOption
|
|
|
|
---@field mode string?
|
|
|
|
---@field frontend string?
|
|
|
|
---@field from string? @Source language type
|
|
|
|
---@field to string? @Target language type
|
|
|
|
---@field is_word? boolean @Is the str a word
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- NOTE : Use coroutine to stop and resume the process (for animation)
|
|
|
|
|
|
|
|
---@class Trans
|
|
|
|
---@field translate fun(opts: TransDataOption?) Translate string core function
|
|
|
|
return function(...) coroutine.wrap(process)(...) end
|