diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/locale/en-US/script.lni | 0 | ||||
-rw-r--r-- | server/src/language.lua | 69 | ||||
-rw-r--r-- | server/src/matcher/diagnostics.lua | 2 | ||||
-rw-r--r-- | server/src/matcher/library.lua | 7 |
4 files changed, 74 insertions, 4 deletions
diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/server/locale/en-US/script.lni diff --git a/server/src/language.lua b/server/src/language.lua index 307b3b62..65768c8d 100644 --- a/server/src/language.lua +++ b/server/src/language.lua @@ -1,4 +1,5 @@ local fs = require 'bee.filesystem' +local lni = require 'lni' local function supportLanguage() local list = {} @@ -30,12 +31,78 @@ local function getLanguage(id) return 'enUS' end +local function loadFileByLanguage(name, language) + local path = ROOT / 'locale' / language / (name .. '.lni') + local buf = io.load(path) + if not buf then + return {} + end + local suc, tbl = xpcall(lni.classics, log.error, buf, path:string()) + if not suc then + return {} + end + return tbl +end + +local function loadLang(name, language) + local tbl = loadFileByLanguage(name, 'en-US') + if language ~= 'en-US' then + local other = loadFileByLanguage(name, language) + for k, v in pairs(other) do + tbl[k] = v + end + end + return setmetatable(tbl, { + __index = function (self, key) + self[key] = key + return key + end, + __call = function (self, key, ...) + local str = self[key] + local index = 0 + str:gsub('%{(.-)%}', function (pat) + local id, fmt + local pos = pat:find(':', 1, true) + if pos then + id = pat:sub(1, pos-1) + fmg = pat:sub(pos+1) + else + id = pat + fmt = 's' + end + id = tonumber(id) + if not id then + index = index + 1 + id = index + end + local v = select(id, ...) + local suc, res = pcall(string.format, '%'..fmt, v) + if suc then + return res + else + -- 这里不能使用翻译,以免死循环 + log.warn(('[%s][%s-%s]{%s} formated error.'):format( + language, name, key, pat + )) + return nil + end + end) + end, + }) +end + local function init() local id = osLanguage() local language = getLanguage(id) log.info(('VSC language: %s'):format(id)) log.info(('LS language: %s'):format(language)) - return language + return setmetatable({ id = language }, { + __index = function (self, name) + local tbl = loadLang(name, language) + self[name] = tbl + return tbl + end, + }) end return init() diff --git a/server/src/matcher/diagnostics.lua b/server/src/matcher/diagnostics.lua index 5e51f9ac..ba537694 100644 --- a/server/src/matcher/diagnostics.lua +++ b/server/src/matcher/diagnostics.lua @@ -1,3 +1,5 @@ +local lang = require 'language' + local function searchUnusedLocals(results, callback) for _, var in ipairs(results.locals) do if var.key == 'self' diff --git a/server/src/matcher/library.lua b/server/src/matcher/library.lua index 5b150513..68ec0fca 100644 --- a/server/src/matcher/library.lua +++ b/server/src/matcher/library.lua @@ -137,7 +137,8 @@ local function fix(libs) end local function init() - local language = require 'language' + local lang = require 'language' + local id = lang.id local alllibs = { global = table.container(), library = table.container(), @@ -155,8 +156,8 @@ local function init() local locale = loadLocale('en-US', relative) mergeLocale(libs, locale) - if language ~= 'en-US' then - locale = loadLocale(language, relative) + if id ~= 'en-US' then + locale = loadLocale(id, relative) mergeLocale(libs, locale) end mergeLibs(alllibs, libs) |