diff options
author | ShadowNinja <shadowninja@minetest.net> | 2015-02-01 18:10:09 -0500 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2015-02-01 18:10:09 -0500 |
commit | 4404c4071cddfd7c30b2420be54d184067797482 (patch) | |
tree | d555b2fe6773e8216d811ef95833f18e3cc2d3d4 | |
parent | c4335aa3dca1d568167adcf13ec1285b76f25a9a (diff) |
Add numbered insertions
-rw-r--r-- | README.txt | 7 | ||||
-rw-r--r-- | init.lua | 56 | ||||
-rw-r--r-- | lib.lua | 30 | ||||
-rwxr-xr-x | tools/findtext.lua | 2 |
4 files changed, 70 insertions, 25 deletions
@@ -15,7 +15,14 @@ it at the beginning of your source file(s): if minetest.get_modpath("intllib") then S = intllib.Getter() else + -- If you don't use insertions (@1, @2, etc) you can use this: 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 You will also need to optionally depend on intllib, to do so add "intllib?" to @@ -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 -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 + +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 - intllib.getters[modname] = noop_getter + 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) + 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 @@ -1,19 +1,37 @@ intllib = intllib or {} +local INS_CHAR = "@" +intllib.INSERTION_CHAR = INS_CHAR + local escapes = { ["\\"] = "\\", ["n"] = "\n", + [INS_CHAR] = INS_CHAR..INS_CHAR, } -local function unescape(s) - return s:gsub("([\\]?)\\(.)", function(slash, what) - if slash and (slash ~= "") then - return "\\"..what +local function unescape(str) + local parts = {} + local n = 1 + 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 - return escapes[what] or what + add(str:sub(start)) + break end - end) + local c = str:sub(pos + 1, pos + 1) + add(escapes[c] or c) + start = pos + 2 + end + return table.concat(parts) end local function find_eq(s) diff --git a/tools/findtext.lua b/tools/findtext.lua index 63137e0..b6360cb 100755 --- a/tools/findtext.lua +++ b/tools/findtext.lua @@ -109,7 +109,7 @@ for _, file in ipairs(inputs) do local infile, e = io.open(file, "r") if infile then for line in infile:lines() do - for s in line:gmatch('S%("([^"]*)"%)') do + for s in line:gmatch('S%("([^"]*)"') do table.insert(messages, s) end end |