Import intllib 0.1.0
[minetest_tutorial_subgame.git] / mods / intllib / init.lua
blob62a8f51389cb22b5aaeeec4f1551e113b66fd99e
2 -- Support the old multi-load method
3 intllib = intllib or {}
5 local MP = minetest.get_modpath("intllib")
7 dofile(MP.."/lib.lua")
9 local strings = {}
11 local LANG = minetest.setting_get("language")
12 if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end
13 if not (LANG and (LANG ~= "")) then LANG = "en" end
14 LANG = LANG:sub(1, 2)
16 -- Support the old multi-load method
17 intllib.getters = intllib.getters or {}
19 intllib.strings = {}
21 local function noop_getter(s)
22 return s
23 end
25 function intllib.Getter(modname)
26 modname = modname or minetest.get_current_modname()
27 if not intllib.getters[modname] then
28 local modpath = minetest.get_modpath(modname)
29 if modpath then
30 local filename = modpath.."/locale/"..LANG..".txt"
31 local msgstr = intllib.load_strings(filename)
32 intllib.strings[modname] = msgstr or false
33 if msgstr then
34 intllib.getters[modname] = function (s)
35 if msgstr[s] and msgstr[s] ~= "" then
36 return msgstr[s]
37 end
38 return s
39 end
40 else
41 intllib.getters[modname] = noop_getter
42 end
43 end
44 end
45 return intllib.getters[modname]
46 end
48 function intllib.get_strings(modname)
49 modname = modname or minetest.get_current_modname()
50 local msgstr = intllib.strings[modname]
51 if msgstr == nil then
52 local modpath = minetest.get_modpath(modname)
53 msgstr = intllib.load_strings(modpath.."/locale/"..LANG..".txt")
54 intllib.strings[modname] = msgstr
55 end
56 return msgstr or nil
57 end