diff options
author | Diego MartÃnez <kaeza@users.noreply.github.com> | 2017-01-21 01:04:03 -0300 |
---|---|---|
committer | Diego MartÃnez <kaeza@users.noreply.github.com> | 2017-01-24 00:24:57 -0300 |
commit | b2551f6a2209b8a11b42834cb0d63f5c03a2b95f (patch) | |
tree | 93e1ac391a3146431b273c94805f543b63b64fb1 /init.lua | |
parent | 4e067ec21906e9a27ec704dd5f34297b2592d6de (diff) |
Add support for gettext message catalogs.
Diffstat (limited to 'init.lua')
-rw-r--r-- | init.lua | 90 |
1 files changed, 76 insertions, 14 deletions
@@ -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 |