Intllib: add `core.get_translator_auto()` wrapper

master
luk3yx 2021-12-28 18:46:29 +13:00
parent 4cfc5a506f
commit 4f45afdba5
1 changed files with 16 additions and 2 deletions

View File

@ -158,7 +158,7 @@ function intllib.make_gettext_pair(modname)
if gettext_getters[modname] then
return unpack(gettext_getters[modname])
end
local modpath = core.get_modpath(modname) and core.get_modpath(modname)
local modpath = core.get_modpath(modname)
local localedir = modpath and modpath.."/locale"
local catalogs = localedir and gettext.load_catalogs(localedir) or {}
local getter = Getter(modname)
@ -192,7 +192,7 @@ function intllib.get_strings(modname, langcode)
modname = modname or core.get_current_modname()
local msgstr = intllib.strings[modname]
if not msgstr then
local modpath = core.get_modpath(modname) and core.get_modpath(modname)
local modpath = core.get_modpath(modname)
msgstr = { }
if modpath then
for _, l in ipairs(get_locales(langcode)) do
@ -207,3 +207,17 @@ function intllib.get_strings(modname, langcode)
end
return msgstr
end
function core.get_translator_auto(langs)
if type(langs) == "table" then
for _, l in ipairs(langs) do
if LANG == l then
-- Surrounded in brackets so there's only one return value
return (intllib.make_gettext_pair())
end
end
end
return core.get_translator(core.get_current_modname())
end