summaryrefslogtreecommitdiffhomepage
path: root/libs/web/luasrc/i18n.lua
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2008-06-14 14:12:12 +0000
committerSteven Barth <steven@midlink.org>2008-06-14 14:12:12 +0000
commit855b7582d3576f45693e3a48fdb253c813cf4dce (patch)
treeb912f63dc43f3b696385083542c801dba8c53976 /libs/web/luasrc/i18n.lua
parent50fd29841540bb8b1735291b72853454679e9e62 (diff)
* Rewrote Luci to be coroutine-safe allowing the use of non-forking webservers
* Setting base version to 0.7
Diffstat (limited to 'libs/web/luasrc/i18n.lua')
-rw-r--r--libs/web/luasrc/i18n.lua29
1 files changed, 21 insertions, 8 deletions
diff --git a/libs/web/luasrc/i18n.lua b/libs/web/luasrc/i18n.lua
index 3ed1ce017..35ad0965d 100644
--- a/libs/web/luasrc/i18n.lua
+++ b/libs/web/luasrc/i18n.lua
@@ -30,6 +30,8 @@ require("luci.sys")
table = {}
i18ndir = luci.sys.libpath() .. "/i18n/"
loaded = {}
+context = luci.util.threadlocal()
+default = "en"
-- Clears the translation table
function clear()
@@ -37,13 +39,17 @@ function clear()
end
-- Loads a translation and copies its data into the global translation table
-function load(file, force)
- if force or not loaded[file] then
- local f = loadfile(i18ndir..file..".lua") or loadfile(i18ndir..file)
+function load(file, lang, force)
+ lang = lang or ""
+ if force or not loaded[lang] or not loaded[lang][file] then
+ local f = loadfile(i18ndir .. file .. "." .. lang .. ".lua")
+ or loadfile(i18ndir .. file .. "." .. lang)
if f then
- setfenv(f, table)
+ table[lang] = table[lang] or {}
+ setfenv(f, table[lang])
f()
- loaded[file] = true
+ loaded[lang] = loaded[lang] or {}
+ loaded[lang][file] = true
return true
else
return false
@@ -55,13 +61,20 @@ end
-- Same as load but autocompletes the filename with .LANG from config.lang
function loadc(file, force)
- load(file .. ".en", force)
- return load(file .. "." .. require("luci.config").main.lang, force)
+ load(file, default, force)
+ return load(file, context.lang, force)
+end
+
+-- Sets the context language
+function setlanguage(lang)
+ context.lang = lang
end
-- Returns the i18n-value defined by "key" or if there is no such: "default"
function translate(key, default)
- return table[key] or default
+ return (table[context.lang] and table[context.lang][key])
+ or (table[default] and table[default][key])
+ or default
end
-- Translate shourtcut with sprintf/string.format inclusion