Add i18n support.

This commit is contained in:
Nodir Temirkhodjaev 2015-01-21 14:59:17 +05:00
parent 0ea1977708
commit 98a9533608
5 changed files with 80 additions and 0 deletions

4
bin/lua/wipf/lang/en.lua Normal file
View File

@ -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"

View File

@ -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,

View File

@ -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,
}

View File

@ -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,
}

View File

@ -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