2008-03-02 21:52:58 +00:00
|
|
|
--[[
|
2008-05-25 17:00:30 +00:00
|
|
|
LuCI - Internationalisation
|
2008-03-02 21:52:58 +00:00
|
|
|
|
|
|
|
Description:
|
|
|
|
A very minimalistic but yet effective internationalisation module
|
|
|
|
|
|
|
|
FileId:
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
License:
|
|
|
|
Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2008-05-25 17:00:30 +00:00
|
|
|
module("luci.i18n", package.seeall)
|
|
|
|
require("luci.sys")
|
2008-03-02 21:52:58 +00:00
|
|
|
|
|
|
|
table = {}
|
2008-05-25 17:00:30 +00:00
|
|
|
i18ndir = luci.sys.libpath() .. "/i18n/"
|
2008-05-31 13:57:30 +00:00
|
|
|
loaded = {}
|
2008-06-14 14:12:12 +00:00
|
|
|
context = luci.util.threadlocal()
|
|
|
|
default = "en"
|
2008-03-02 21:52:58 +00:00
|
|
|
|
|
|
|
-- Clears the translation table
|
|
|
|
function clear()
|
|
|
|
table = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Loads a translation and copies its data into the global translation table
|
2008-06-14 14:12:12 +00:00
|
|
|
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")
|
2008-05-31 13:57:30 +00:00
|
|
|
if f then
|
2008-06-14 14:12:12 +00:00
|
|
|
table[lang] = table[lang] or {}
|
|
|
|
setfenv(f, table[lang])
|
2008-05-31 13:57:30 +00:00
|
|
|
f()
|
2008-06-14 14:12:12 +00:00
|
|
|
loaded[lang] = loaded[lang] or {}
|
|
|
|
loaded[lang][file] = true
|
2008-05-31 13:57:30 +00:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2008-03-02 21:52:58 +00:00
|
|
|
else
|
2008-05-31 13:57:30 +00:00
|
|
|
return true
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Same as load but autocompletes the filename with .LANG from config.lang
|
2008-06-01 12:12:18 +00:00
|
|
|
function loadc(file, force)
|
2008-06-14 14:12:12 +00:00
|
|
|
load(file, default, force)
|
|
|
|
return load(file, context.lang, force)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sets the context language
|
|
|
|
function setlanguage(lang)
|
|
|
|
context.lang = lang
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns the i18n-value defined by "key" or if there is no such: "default"
|
2008-06-28 16:03:54 +00:00
|
|
|
function translate(key, def)
|
2008-06-14 14:12:12 +00:00
|
|
|
return (table[context.lang] and table[context.lang][key])
|
|
|
|
or (table[default] and table[default][key])
|
2008-06-28 16:03:54 +00:00
|
|
|
or def
|
2008-05-25 15:55:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Translate shourtcut with sprintf/string.format inclusion
|
|
|
|
function translatef(key, default, ...)
|
|
|
|
return translate(key, default):format(...)
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|