2 LuCI - Internationalisation
5 A very minimalistic but yet effective internationalisation module
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
27 module("luci.i18n", package.seeall)
31 i18ndir = luci.sys.libpath() .. "/i18n/"
33 context = luci.util.threadlocal()
36 -- Clears the translation table
41 -- Loads a translation and copies its data into the global translation table
42 function load(file, lang, force)
44 if force or not loaded[lang] or not loaded[lang][file] then
45 local f = loadfile(i18ndir .. file .. "." .. lang .. ".lua")
46 or loadfile(i18ndir .. file .. "." .. lang)
48 table[lang] = table[lang] or {}
49 setfenv(f, table[lang])
51 loaded[lang] = loaded[lang] or {}
52 loaded[lang][file] = true
62 -- Same as load but autocompletes the filename with .LANG from config.lang
63 function loadc(file, force)
64 load(file, default, force)
65 return load(file, context.lang, force)
68 -- Sets the context language
69 function setlanguage(lang)
73 -- Returns the i18n-value defined by "key" or if there is no such: "default"
74 function translate(key, def)
75 return (table[context.lang] and table[context.lang][key])
76 or (table[default] and table[default][key])
80 -- Translate shourtcut with sprintf/string.format inclusion
81 function translatef(key, default, ...)
82 return translate(key, default):format(...)