附协议分析笔记,沿用统一的 update.lua 与打包脚本。 Co-authored-by: Copilot <[email protected]>
38 lines
1.2 KiB
Lua
38 lines
1.2 KiB
Lua
local M = {}
|
|
local catalog
|
|
|
|
local function currentLang()
|
|
local ok, GetText = pcall(require, "gettext")
|
|
local lang = ok and type(GetText) == "table" and GetText.current_lang or "zh_CN"
|
|
return (tostring(lang):gsub("%.[Uu][Tt][Ff]%-?8$", ""))
|
|
end
|
|
|
|
function M.candidates(lang)
|
|
if lang == "" or lang == "C" or lang:match("^en") then return { "en" } end
|
|
if lang:match("^zh_TW") or lang:match("^zh_HK") then return { "zh_TW" } end
|
|
if lang:match("^zh") then return {} end
|
|
local out = { lang }
|
|
local base = lang:match("^([^_]+)")
|
|
if base and base ~= lang then out[#out + 1] = base end
|
|
out[#out + 1] = "en"
|
|
return out
|
|
end
|
|
|
|
function M.load(lang)
|
|
catalog = {}
|
|
for _, name in ipairs(M.candidates(lang or currentLang())) do
|
|
local ok, t = pcall(require, "moonsource_bilimanga.l10n." .. name)
|
|
if ok and type(t) == "table" then catalog = t; break end
|
|
end
|
|
return catalog
|
|
end
|
|
|
|
function M.translate(msg, ...)
|
|
if not catalog then M.load() end
|
|
local s = catalog[msg] or msg
|
|
if select("#", ...) > 0 then return s:format(...) end
|
|
return s
|
|
end
|
|
|
|
return setmetatable(M, { __call = function(_, ...) return M.translate(...) end })
|