init.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. -- Old multi-load method compatibility
  2. if rawget(_G, "intllib") then return end
  3. intllib = {
  4. getters = {},
  5. strings = {},
  6. }
  7. local MP = minetest.get_modpath("intllib")
  8. dofile(MP.."/lib.lua")
  9. local LANG = minetest.settings:get("language")
  10. if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end
  11. if not (LANG and (LANG ~= "")) then LANG = "en" end
  12. local INS_CHAR = intllib.INSERTION_CHAR
  13. local insertion_pattern = "("..INS_CHAR.."?)"..INS_CHAR.."(%(?)(%d+)(%)?)"
  14. local function do_replacements(str, ...)
  15. local args = {...}
  16. -- Outer parens discard extra return values
  17. return (str:gsub(insertion_pattern, function(escape, open, num, close)
  18. if escape == "" then
  19. local replacement = tostring(args[tonumber(num)])
  20. if open == "" then
  21. replacement = replacement..close
  22. end
  23. return replacement
  24. else
  25. return INS_CHAR..open..num..close
  26. end
  27. end))
  28. end
  29. local function make_getter(msgstrs)
  30. return function(s, ...)
  31. local str
  32. if msgstrs then
  33. str = msgstrs[s]
  34. end
  35. if not str or str == "" then
  36. str = s
  37. end
  38. if select("#", ...) == 0 then
  39. return str
  40. end
  41. return do_replacements(str, ...)
  42. end
  43. end
  44. local function Getter(modname)
  45. modname = modname or minetest.get_current_modname()
  46. if not intllib.getters[modname] then
  47. local msgstr = intllib.get_strings(modname)
  48. intllib.getters[modname] = make_getter(msgstr)
  49. end
  50. return intllib.getters[modname]
  51. end
  52. function intllib.Getter(modname)
  53. local info = debug and debug.getinfo and debug.getinfo(2)
  54. local loc = info and info.short_src..":"..info.currentline
  55. minetest.log("deprecated", "intllib.Getter is deprecated."
  56. .." Please use intllib.make_gettext_pair instead."
  57. ..(info and " (called from "..loc..")" or ""))
  58. return Getter(modname)
  59. end
  60. local strfind, strsub = string.find, string.sub
  61. local langs
  62. local function split(str, sep)
  63. local pos, endp = 1, #str+1
  64. return function()
  65. if (not pos) or pos > endp then return end
  66. local s, e = strfind(str, sep, pos, true)
  67. local part = strsub(str, pos, s and s-1)
  68. pos = e and e + 1
  69. return part
  70. end
  71. end
  72. function intllib.get_detected_languages()
  73. if langs then return langs end
  74. langs = { }
  75. local function addlang(l)
  76. local sep
  77. langs[#langs+1] = l
  78. sep = strfind(l, ".", 1, true)
  79. if sep then
  80. l = strsub(l, 1, sep-1)
  81. langs[#langs+1] = l
  82. end
  83. sep = strfind(l, "_", 1, true)
  84. if sep then
  85. langs[#langs+1] = strsub(l, 1, sep-1)
  86. end
  87. end
  88. local v
  89. v = minetest.settings:get("language")
  90. if v and v~="" then
  91. addlang(v)
  92. end
  93. v = os.getenv("LANGUAGE")
  94. if v then
  95. for item in split(v, ":") do
  96. langs[#langs+1] = item
  97. end
  98. end
  99. v = os.getenv("LANG")
  100. if v then
  101. addlang(v)
  102. end
  103. langs[#langs+1] = "en"
  104. return langs
  105. end
  106. local gettext = dofile(minetest.get_modpath("intllib").."/gettext.lua")
  107. local function catgettext(catalogs, msgid)
  108. for _, cat in ipairs(catalogs) do
  109. local msgstr = cat and cat[msgid]
  110. if msgstr and msgstr~="" then
  111. local msg = msgstr[0]
  112. return msg~="" and msg or nil
  113. end
  114. end
  115. end
  116. local function catngettext(catalogs, msgid, msgid_plural, n)
  117. n = math.floor(n)
  118. for _, cat in ipairs(catalogs) do
  119. local msgstr = cat and cat[msgid]
  120. if msgstr then
  121. local index = cat.plural_index(n)
  122. local msg = msgstr[index]
  123. return msg~="" and msg or nil
  124. end
  125. end
  126. return n==1 and msgid or msgid_plural
  127. end
  128. local gettext_getters = { }
  129. function intllib.make_gettext_pair(modname)
  130. modname = modname or minetest.get_current_modname()
  131. if gettext_getters[modname] then
  132. return unpack(gettext_getters[modname])
  133. end
  134. local localedir = minetest.get_modpath(modname).."/locale"
  135. local catalogs = gettext.load_catalogs(localedir)
  136. local getter = Getter(modname)
  137. local function gettext_func(msgid, ...)
  138. local msgstr = (catgettext(catalogs, msgid)
  139. or getter(msgid))
  140. return do_replacements(msgstr, ...)
  141. end
  142. local function ngettext_func(msgid, msgid_plural, n, ...)
  143. local msgstr = (catngettext(catalogs, msgid, msgid_plural, n)
  144. or getter(msgid))
  145. return do_replacements(msgstr, ...)
  146. end
  147. gettext_getters[modname] = { gettext_func, ngettext_func }
  148. return gettext_func, ngettext_func
  149. end
  150. local function get_locales(code)
  151. local ll, cc = code:match("^(..)_(..)")
  152. if ll then
  153. return { ll.."_"..cc, ll, ll~="en" and "en" or nil }
  154. else
  155. return { code, code~="en" and "en" or nil }
  156. end
  157. end
  158. function intllib.get_strings(modname, langcode)
  159. langcode = langcode or LANG
  160. modname = modname or minetest.get_current_modname()
  161. local msgstr = intllib.strings[modname]
  162. if not msgstr then
  163. local modpath = minetest.get_modpath(modname)
  164. msgstr = { }
  165. for _, l in ipairs(get_locales(langcode)) do
  166. local t = intllib.load_strings(modpath.."/locale/"..l..".txt") or { }
  167. for k, v in pairs(t) do
  168. msgstr[k] = msgstr[k] or v
  169. end
  170. end
  171. intllib.strings[modname] = msgstr
  172. end
  173. return msgstr
  174. end