intllib.lua 1.1 KB

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