1
0
mirror of https://github.com/minetest-mods/intllib.git synced 2024-11-13 05:50:29 +01:00
intllib/init.lua
2015-02-16 23:24:10 -05:00

73 lines
1.7 KiB
Lua

-- Support the old multi-load method
intllib = rawget(_G, "intllib") or {}
local MP = minetest.get_modpath("intllib")
dofile(MP.."/lib.lua")
local LANG = minetest.setting_get("language")
if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end
if not (LANG and (LANG ~= "")) then LANG = "en" end
LANG = LANG:sub(1, 2)
-- Support the old multi-load method
intllib.getters = intllib.getters or {}
intllib.strings = {}
local INS_CHAR = intllib.INSERTION_CHAR
local insertion_pattern = "("..INS_CHAR.."?)"..INS_CHAR.."(%(?)(%d+)(%)?)"
local function make_getter(msgstrs)
return function(s, ...)
local str
if msgstrs then
str = msgstrs[s]
end
if not str or str == "" then
str = s
end
if select("#", ...) == 0 then
return str
end
local args = {...}
str = str:gsub(insertion_pattern, function(escape, open, num, close)
if escape == "" then
local replacement = tostring(args[tonumber(num)])
if open == "" then
replacement = replacement..close
end
return replacement
else
return INS_CHAR..open..num..close
end
end)
return str
end
end
function intllib.Getter(modname)
modname = modname or minetest.get_current_modname()
if not intllib.getters[modname] then
local msgstr = intllib.get_strings(modname)
intllib.getters[modname] = make_getter(msgstr)
end
return intllib.getters[modname]
end
function intllib.get_strings(modname)
modname = modname or minetest.get_current_modname()
local msgstr = intllib.strings[modname]
if not msgstr then
local modpath = minetest.get_modpath(modname)
msgstr = intllib.load_strings(modpath.."/locale/"..LANG..".txt")
intllib.strings[modname] = msgstr
end
return msgstr or nil
end