diff options
author | kaeza <kaeza@users.sf.net> | 2013-10-29 17:37:36 -0200 |
---|---|---|
committer | kaeza <kaeza@users.sf.net> | 2013-10-29 17:37:36 -0200 |
commit | abb257aae33b7c794eb7fcb604f2464684aed176 (patch) | |
tree | 21560d7a441522c8a1283cdfc6f2feb4b8c23719 /init.lua | |
parent | 2866bb4ec68a63162fda76b07b7aa254e98dbfb2 (diff) |
Some refactoring.
Diffstat (limited to 'init.lua')
-rw-r--r-- | init.lua | 73 |
1 files changed, 22 insertions, 51 deletions
@@ -2,67 +2,38 @@ -- Support the old multi-load method intllib = intllib or {} -local strings = {} - -local LANG = minetest.setting_get("language") or os.getenv("LANG") or "en" -LANG = LANG:sub(1, 2) +local MP = minetest.get_modpath("intllib") -local escapes = { - ["\\"] = "\\", - ["n"] = "\n", - ["="] = "=", -} - -local function unescape(s) - return s:gsub("([\\]?)\\(.)", function(slash, what) - if slash and (slash ~= "") then - return "\\"..what - else - return escapes[what] or what - end - end) -end - - -local function find_eq(s) - for slashes, pos in s:gmatch("([\\]*)=()") do - if (slashes:len() % 2) == 0 then - return pos - 1 - end - end -end +dofile(MP.."/lib.lua") +local strings = {} -function load_strings(modname) - local modpath = minetest.get_modpath(modname) - local file, err = io.open(modpath.."/locale/"..LANG..".txt", "r") - if not file then - return nil - end - local strings = {} - for line in file:lines() do - line = line:trim() - if line ~= "" and line:sub(1, 1) ~= "#" then - local pos = find_eq(line) - if pos then - local msgid = unescape(line:sub(1, pos - 1):trim()) - strings[msgid] = unescape(line:sub(pos + 1):trim()) - end - end - end - file:close() - return strings -end +local LANG = minetest.setting_get("language") +if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end +if not (LANG and (LANG ~= "")) then LANG = "en" end +LANG = LANG:sub(1, 2) -- Support the old multi-load method intllib.getters = intllib.getters or {} +local function noop_getter(s) + return s +end + function intllib.Getter(modname) modname = modname or minetest.get_current_modname() if not intllib.getters[modname] then - local msgstr = load_strings(modname) or {} - intllib.getters[modname] = function (s) - return msgstr[s] or s + local modpath = minetest.get_modpath(modname) + if modpath then + local filename = modpath.."/locale/"..LANG..".txt" + local msgstr = load_strings(filename) + if msgstr then + intllib.getters[modname] = function (s) + return msgstr[s] or s + end + else + intllib.getters[modname] = noop_getter + end end end return intllib.getters[modname] |