diff options
author | ShadowNinja <noreply@gmail.com> | 2013-10-29 11:19:35 -0400 |
---|---|---|
committer | ShadowNinja <noreply@gmail.com> | 2013-10-29 11:19:35 -0400 |
commit | af31d71d1a9ffd6d1373b2714bcf025c63afaaee (patch) | |
tree | e627ba33a5673a1a298a592b6e201fc6582cfbbc /init.lua | |
parent | 5de9bcea76f851e64cfb1307bc3d321642cb5be2 (diff) |
Use optional dependencies to only load once and re-add support for escapes
Diffstat (limited to 'init.lua')
-rw-r--r-- | init.lua | 70 |
1 files changed, 70 insertions, 0 deletions
@@ -0,0 +1,70 @@ + +-- 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 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 + + +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 + +-- Support the old multi-load method +intllib.getters = intllib.getters or {} + +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 + end + end + return intllib.getters[modname] +end + |