diff --git a/bin/lua/wipf/lang/en.lua b/bin/lua/wipf/lang/en.lua new file mode 100644 index 00000000..ebcf5f0a --- /dev/null +++ b/bin/lua/wipf/lang/en.lua @@ -0,0 +1,4 @@ +-- WIPF Localization: English + +err_conf_iprange_inc = "Bad Include IP address: line %d" +err_conf_iprange_exc = "Bad Exclude IP address: line %d" diff --git a/bin/lua/wipf/util/conf.lua b/bin/lua/wipf/util/conf.lua index 2e318230..2a4c6448 100644 --- a/bin/lua/wipf/util/conf.lua +++ b/bin/lua/wipf/util/conf.lua @@ -1,6 +1,7 @@ -- WIPF Configuration Utilities local wipf = require"wipflua" +local i18n = require"wipf/util/i18n" local util_ip = require"wipf/util/ip" @@ -73,9 +74,18 @@ local conf_meta = { local iprange_from_inc, iprange_to_inc = util_ip.ip4range_to_numbers(self.ip_include) + if not iprange_from_inc then + return nil, i18n.tr_fmt('err_conf_iprange_inc', iprange_to_inc) + end local iprange_from_exc, iprange_to_exc = util_ip.ip4range_to_numbers(self.ip_exclude) + if not iprange_from_exc then + return nil, i18n.tr_fmt('err_conf_iprange_exc', iprange_to_exc) + end + + --local group_names, app_dospaths = + -- util_ip.ip4range_to_numbers(self.ip_exclude) return true end, diff --git a/bin/lua/wipf/util/fs.lua b/bin/lua/wipf/util/fs.lua index 7bd4fc90..9f73bd8c 100644 --- a/bin/lua/wipf/util/fs.lua +++ b/bin/lua/wipf/util/fs.lua @@ -66,9 +66,22 @@ local function path_to_dospath(path) return drive_to_dosname(drive) .. sub_path end +-- Load file, run it in sandbox and return it's globals in a table +function sandbox(path) + local chunk, err_msg = loadfile(path) + if not chunk then + return nil, err_msg + end + local env = setmetatable({}, nil) + setfenv(chunk, env) + chunk() + return env +end + return { pid_dospath = pid_dospath, dospath_to_path = dospath_to_path, path_to_dospath = path_to_dospath, + sandbox = sandbox, } diff --git a/bin/lua/wipf/util/i18n.lua b/bin/lua/wipf/util/i18n.lua new file mode 100644 index 00000000..72f0c5e0 --- /dev/null +++ b/bin/lua/wipf/util/i18n.lua @@ -0,0 +1,43 @@ +-- WIPF Internationalization Utilities + +local util_fs = require"wipf/util/fs" + + +local current_lang, lang_strings + + +local function set_current_lang(lang) + current_lang = lang + lang_strings = assert(util_fs.sandbox("lua/wipf/lang/" .. lang .. ".lua")) +end + +local function get_current_lang() + return current_lang +end + +-- Get translation text for l10n identifier +local function tr(id) + return lang_strings[id] or id +end + +-- Get formatted translation text for l10n identifier +local function tr_fmt(id, ...) + local s = lang_strings[id] + if not s then + return id + end + + return string.format(s, ...) +end + + +-- Set default language English +set_current_lang("en") + + +return { + set_current_lang = set_current_lang, + get_current_lang = get_current_lang, + tr = tr, + tr_fmt = tr_fmt, +} diff --git a/test/wipf_test.lua b/test/wipf_test.lua index 28844e38..8c58af01 100644 --- a/test/wipf_test.lua +++ b/test/wipf_test.lua @@ -4,6 +4,7 @@ local sys = require"sys" local sock = require"sys.sock" local wipf = require"wipflua" +local i18n = require"wipf/util/i18n" local util_conf = require"wipf/util/conf" local util_fs = require"wipf/util/fs" local util_ip = require"wipf/util/ip" @@ -111,3 +112,12 @@ do end +print"-- I18n" +do + local id = 'err_conf_iprange_inc' + local text = i18n.tr_fmt(id, 3) + assert(text ~= id) + print("OK") +end + +