diff options
-rw-r--r-- | script/language.lua | 41 | ||||
-rw-r--r-- | script/provider/client.lua | 2 |
2 files changed, 21 insertions, 22 deletions
diff --git a/script/language.lua b/script/language.lua index 6077e4c6..f23d09ab 100644 --- a/script/language.lua +++ b/script/language.lua @@ -12,16 +12,15 @@ local function supportLanguage() return list end -local function osLanguage() - return LANG:lower() -end - local function getLanguage(id) local support = supportLanguage() -- 检查是否支持语言 if support[id] then return id end + if not id then + return 'en-us' + end -- 根据语言的前2个字母来找近似语言 for _, lang in ipairs(support) do if lang:sub(1, 2) == id:sub(1, 2) then @@ -29,7 +28,7 @@ local function getLanguage(id) end end -- 使用英文 - return 'enUS' + return 'en-us' end local function loadFileByLanguage(name, language) @@ -87,8 +86,8 @@ local function formatAsTable(str, ...) end local function loadLang(name, language) - local tbl = loadFileByLanguage(name, 'en-US') - if language ~= 'en-US' then + 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 @@ -123,18 +122,16 @@ local function loadLang(name, language) }) 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 setmetatable({ id = language }, { - __index = function (self, name) - local tbl = loadLang(name, language) - self[name] = tbl - return tbl - end, - }) -end - -return init() +return setmetatable({}, { + __index = function (self, name) + local tbl = loadLang(name, self.id) + self[name] = tbl + return tbl + end, + __call = function (self, id) + local language = getLanguage(id) + log.info(('VSC language: %s'):format(id)) + log.info(('LS language: %s'):format(language)) + self.id = language + end, +}) diff --git a/script/provider/client.lua b/script/provider/client.lua index c1b16f0f..b9752b66 100644 --- a/script/provider/client.lua +++ b/script/provider/client.lua @@ -1,5 +1,6 @@ local nonil = require 'without-check-nil' local util = require 'utility' +local lang = require 'language' local m = {} @@ -13,6 +14,7 @@ end function m.init(t) log.debug('Client init', util.dump(t)) m.info = t + lang(t.locale) end return m |