* Performance optimizations
[project/luci.git] / libs / web / luasrc / i18n.lua
1 --[[
2 LuCI - Internationalisation
3
4 Description:
5 A very minimalistic but yet effective internationalisation module
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
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
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
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.
24
25 ]]--
26
27 module("luci.i18n", package.seeall)
28 require("luci.sys")
29
30 table = {}
31 i18ndir = luci.sys.libpath() .. "/i18n/"
32 loaded = {}
33
34 -- Clears the translation table
35 function clear()
36 table = {}
37 end
38
39 -- Loads a translation and copies its data into the global translation table
40 function load(file, force)
41 if force or not loaded[file] then
42 local f = loadfile(i18ndir..file..".lua") or loadfile(i18ndir..file)
43 if f then
44 setfenv(f, table)
45 f()
46 loaded[file] = true
47 return true
48 else
49 return false
50 end
51 else
52 return true
53 end
54 end
55
56 -- Same as load but autocompletes the filename with .LANG from config.lang
57 function loadc(file, force)
58 return load(file .. "." .. require("luci.config").main.lang, force)
59 end
60
61 -- Returns the i18n-value defined by "key" or if there is no such: "default"
62 function translate(key, default)
63 return table[key] or default
64 end
65
66 -- Translate shourtcut with sprintf/string.format inclusion
67 function translatef(key, default, ...)
68 return translate(key, default):format(...)
69 end