summaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua90
1 files changed, 76 insertions, 14 deletions
diff --git a/init.lua b/init.lua
index 79d4c31..ee6d1a9 100644
--- a/init.lua
+++ b/init.lua
@@ -21,6 +21,22 @@ if not (LANG and (LANG ~= "")) then LANG = "en" end
local INS_CHAR = intllib.INSERTION_CHAR
local insertion_pattern = "("..INS_CHAR.."?)"..INS_CHAR.."(%(?)(%d+)(%)?)"
+local function do_replacements(str, ...)
+ local args = {...}
+ -- Outer parens discard extra return values
+ return (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))
+end
+
local function make_getter(msgstrs)
return function(s, ...)
local str
@@ -33,24 +49,12 @@ local function make_getter(msgstrs)
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
+ return do_replacements(str, ...)
end
end
-function intllib.Getter(modname)
+local function Getter(modname)
modname = modname or minetest.get_current_modname()
if not intllib.getters[modname] then
local msgstr = intllib.get_strings(modname)
@@ -60,6 +64,64 @@ function intllib.Getter(modname)
end
+function intllib.Getter(modname)
+ minetest.log("deprecated", "intllib.Getter is deprecated."
+ .."Please use intllib.make_gettext_pair instead.")
+ return Getter(modname)
+end
+
+
+local gettext = dofile(minetest.get_modpath("intllib").."/gettext.lua")
+
+
+local function catgettext(catalogs, msgid)
+ for _, cat in ipairs(catalogs) do
+ local msgstr = cat and cat[msgid]
+ if msgstr then
+ return msgstr[0]
+ end
+ end
+end
+
+local function catngettext(catalogs, msgid, msgid_plural, n)
+ n = math.floor(n)
+ for i, cat in ipairs(catalogs) do
+ print(i, dump(cat))
+ local msgstr = cat and cat[msgid]
+ if msgstr then
+ local index = cat.plural_index(n)
+ print("catngettext:", index, msgstr[index])
+ return msgstr[index]
+ end
+ end
+ return n==1 and msgid or msgid_plural
+end
+
+
+local gettext_getters = { }
+function intllib.make_gettext_pair(modname)
+ modname = modname or minetest.get_current_modname()
+ if gettext_getters[modname] then
+ return unpack(gettext_getters[modname])
+ end
+ local localedir = minetest.get_modpath(modname).."/locale"
+ local catalogs = gettext.load_catalogs(localedir)
+ local getter = Getter(modname)
+ local function gettext(msgid, ...)
+ local msgstr = (catgettext(catalogs, msgid)
+ or getter(msgid))
+ return do_replacements(msgstr, ...)
+ end
+ local function ngettext(msgid, msgid_plural, n, ...)
+ local msgstr = (catngettext(catalogs, msgid, msgid_plural, n)
+ or getter(msgid))
+ return do_replacements(msgstr, ...)
+ end
+ gettext_getters[modname] = { gettext, ngettext }
+ return gettext, ngettext
+end
+
+
local function get_locales(code)
local ll, cc = code:match("^(..)_(..)")
if ll then