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 /lib/intllib.lua | |
parent | 4e067ec21906e9a27ec704dd5f34297b2592d6de (diff) |
Add support for gettext message catalogs.
Diffstat (limited to 'lib/intllib.lua')
-rw-r--r-- | lib/intllib.lua | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/intllib.lua b/lib/intllib.lua new file mode 100644 index 0000000..6669d72 --- /dev/null +++ b/lib/intllib.lua @@ -0,0 +1,45 @@ + +-- Fallback functions for when `intllib` is not installed. +-- Code released under Unlicense <http://unlicense.org>. + +-- Get the latest version of this file at: +-- https://raw.githubusercontent.com/minetest-mods/intllib/master/lib/intllib.lua + +local function format(str, ...) + local args = { ... } + local function repl(escape, open, num, close) + if escape == "" then + local replacement = tostring(args[tonumber(num)]) + if open == "" then + replacement = replacement..close + end + return replacement + else + return "@"..open..num..close + end + end + return (str:gsub("(@?)@(%(?)(%d+)(%)?)", repl)) +end + +local gettext, ngettext +if minetest.get_modpath("intllib") then + if intllib.make_gettext_pair then + -- New method using gettext. + gettext, ngettext = intllib.make_gettext_pair() + else + -- Old method using text files. + gettext = intllib.Getter() + end +end + +-- Fill in missing functions. + +gettext = gettext or function(msgid, ...) + return format(msgid, ...) +end + +ngettext = ngettext or function(msgid, msgid_plural, n, ...) + return format(n==1 and msgid or msgid_plural, ...) +end + +return gettext, ngettext |