]> git.lizzy.rs Git - signs_lib.git/blob - intllib.lua
Locked Wood Signs
[signs_lib.git] / intllib.lua
1
2 -- Fallback functions for when `intllib` is not installed.
3 -- Code released under Unlicense <http://unlicense.org>.
4
5 -- Get the latest version of this file at:
6 --   https://raw.githubusercontent.com/minetest-mods/intllib/master/lib/intllib.lua
7
8 local function format(str, ...)
9         local args = { ... }
10         local function repl(escape, open, num, close)
11                 if escape == "" then
12                         local replacement = tostring(args[tonumber(num)])
13                         if open == "" then
14                                 replacement = replacement..close
15                         end
16                         return replacement
17                 else
18                         return "@"..open..num..close
19                 end
20         end
21         return (str:gsub("(@?)@(%(?)(%d+)(%)?)", repl))
22 end
23
24 local gettext, ngettext
25 if minetest.get_modpath("intllib") then
26         if intllib.make_gettext_pair then
27                 -- New method using gettext.
28                 gettext, ngettext = intllib.make_gettext_pair()
29         else
30                 -- Old method using text files.
31                 gettext = intllib.Getter()
32         end
33 end
34
35 -- Fill in missing functions.
36
37 gettext = gettext or function(msgid, ...)
38         return format(msgid, ...)
39 end
40
41 ngettext = ngettext or function(msgid, msgid_plural, n, ...)
42         return format(n==1 and msgid or msgid_plural, ...)
43 end
44
45 return gettext, ngettext