mirror of
https://github.com/minetest-mods/intllib.git
synced 2025-07-04 09:00:21 +02:00
Add numbered insertions
This commit is contained in:
58
init.lua
58
init.lua
@ -1,4 +1,3 @@
|
||||
|
||||
-- Support the old multi-load method
|
||||
intllib = intllib or {}
|
||||
|
||||
@ -6,7 +5,6 @@ local MP = minetest.get_modpath("intllib")
|
||||
|
||||
dofile(MP.."/lib.lua")
|
||||
|
||||
local strings = {}
|
||||
|
||||
local LANG = minetest.setting_get("language")
|
||||
if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end
|
||||
@ -18,37 +16,59 @@ intllib.getters = intllib.getters or {}
|
||||
|
||||
intllib.strings = {}
|
||||
|
||||
|
||||
local function noop_getter(s)
|
||||
return s
|
||||
end
|
||||
|
||||
|
||||
local INS_CHAR = intllib.INSERTION_CHAR
|
||||
local insertion_pattern = "("..INS_CHAR.."?)"..INS_CHAR.."(%(?)(%d+)(%)?)"
|
||||
|
||||
local function make_getter(strs)
|
||||
return function(s, ...)
|
||||
local str = strs[s]
|
||||
if not str or str == "" then
|
||||
return s
|
||||
end
|
||||
if select("#", ...) == 0 then
|
||||
return str
|
||||
end
|
||||
local args = {...}
|
||||
local 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 modpath = minetest.get_modpath(modname)
|
||||
if modpath then
|
||||
local filename = modpath.."/locale/"..LANG..".txt"
|
||||
local msgstr = intllib.load_strings(filename)
|
||||
intllib.strings[modname] = msgstr or false
|
||||
if msgstr then
|
||||
intllib.getters[modname] = function (s)
|
||||
if msgstr[s] and msgstr[s] ~= "" then
|
||||
return msgstr[s]
|
||||
end
|
||||
return s
|
||||
end
|
||||
else
|
||||
intllib.getters[modname] = noop_getter
|
||||
end
|
||||
if not intllib.getters[modname] then
|
||||
local msgstr = intllib.get_strings(modname)
|
||||
if msgstr then
|
||||
intllib.getters[modname] = make_getter(msgstr)
|
||||
else
|
||||
intllib.getters[modname] = noop_getter
|
||||
end
|
||||
end
|
||||
return intllib.getters[modname]
|
||||
end
|
||||
|
||||
|
||||
function intllib.get_strings(modname)
|
||||
modname = modname or minetest.get_current_modname()
|
||||
local msgstr = intllib.strings[modname]
|
||||
if msgstr == nil then
|
||||
if not msgstr then
|
||||
local modpath = minetest.get_modpath(modname)
|
||||
msgstr = intllib.load_strings(modpath.."/locale/"..LANG..".txt")
|
||||
intllib.strings[modname] = msgstr
|
||||
|
Reference in New Issue
Block a user