Add numbered insertions

This commit is contained in:
ShadowNinja 2015-02-01 18:10:09 -05:00
parent c4335aa3dc
commit 4404c4071c
4 changed files with 71 additions and 26 deletions

View File

@ -15,7 +15,14 @@ it at the beginning of your source file(s):
if minetest.get_modpath("intllib") then if minetest.get_modpath("intllib") then
S = intllib.Getter() S = intllib.Getter()
else else
-- If you don't use insertions (@1, @2, etc) you can use this:
S = function(s) return s end S = function(s) return s end
-- If you use insertions, but not insertion escapes this will work:
S = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
-- Use this if you require full functionality
S = function(s,a,...)if a==nil then return s end a={a,...}return s:gsub("(@?)@(%(?)(%d+)(%)?)",function(e,o,n,c)if e==""then return a[tonumber(n)]..(o==""and c or"")else return"@"..o..n..c end end) end
end end
You will also need to optionally depend on intllib, to do so add "intllib?" to You will also need to optionally depend on intllib, to do so add "intllib?" to

View File

@ -1,4 +1,3 @@
-- Support the old multi-load method -- Support the old multi-load method
intllib = intllib or {} intllib = intllib or {}
@ -6,7 +5,6 @@ local MP = minetest.get_modpath("intllib")
dofile(MP.."/lib.lua") dofile(MP.."/lib.lua")
local strings = {}
local LANG = minetest.setting_get("language") local LANG = minetest.setting_get("language")
if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end
@ -18,37 +16,59 @@ intllib.getters = intllib.getters or {}
intllib.strings = {} intllib.strings = {}
local function noop_getter(s) local function noop_getter(s)
return s return s
end 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) function intllib.Getter(modname)
modname = modname or minetest.get_current_modname() modname = modname or minetest.get_current_modname()
if not intllib.getters[modname] then if not intllib.getters[modname] then
local modpath = minetest.get_modpath(modname) local msgstr = intllib.get_strings(modname)
if modpath then if msgstr then
local filename = modpath.."/locale/"..LANG..".txt" intllib.getters[modname] = make_getter(msgstr)
local msgstr = intllib.load_strings(filename) else
intllib.strings[modname] = msgstr or false intllib.getters[modname] = noop_getter
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
end end
end end
return intllib.getters[modname] return intllib.getters[modname]
end end
function intllib.get_strings(modname) function intllib.get_strings(modname)
modname = modname or minetest.get_current_modname() modname = modname or minetest.get_current_modname()
local msgstr = intllib.strings[modname] local msgstr = intllib.strings[modname]
if msgstr == nil then if not msgstr then
local modpath = minetest.get_modpath(modname) local modpath = minetest.get_modpath(modname)
msgstr = intllib.load_strings(modpath.."/locale/"..LANG..".txt") msgstr = intllib.load_strings(modpath.."/locale/"..LANG..".txt")
intllib.strings[modname] = msgstr intllib.strings[modname] = msgstr

30
lib.lua
View File

@ -1,19 +1,37 @@
intllib = intllib or {} intllib = intllib or {}
local INS_CHAR = "@"
intllib.INSERTION_CHAR = INS_CHAR
local escapes = { local escapes = {
["\\"] = "\\", ["\\"] = "\\",
["n"] = "\n", ["n"] = "\n",
[INS_CHAR] = INS_CHAR..INS_CHAR,
} }
local function unescape(s) local function unescape(str)
return s:gsub("([\\]?)\\(.)", function(slash, what) local parts = {}
if slash and (slash ~= "") then local n = 1
return "\\"..what local function add(s)
parts[n] = s
n = n + 1
end
local start = 1
while true do
local pos = str:find("\\", start, true)
if pos then
add(str:sub(start, pos - 1))
else else
return escapes[what] or what add(str:sub(start))
break
end end
end) local c = str:sub(pos + 1, pos + 1)
add(escapes[c] or c)
start = pos + 2
end
return table.concat(parts)
end end
local function find_eq(s) local function find_eq(s)

View File

@ -109,7 +109,7 @@ for _, file in ipairs(inputs) do
local infile, e = io.open(file, "r") local infile, e = io.open(file, "r")
if infile then if infile then
for line in infile:lines() do for line in infile:lines() do
for s in line:gmatch('S%("([^"]*)"%)') do for s in line:gmatch('S%("([^"]*)"') do
table.insert(messages, s) table.insert(messages, s)
end end
end end