c362d3e5f06d2458a7aac3388ae88df103f5d22b
[project/luci.git] / src / ffluci / i18n.lua
1 --[[
2 FFLuCI - 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("ffluci.i18n", package.seeall)
28
29 require("ffluci.fs")
30 require("ffluci.util")
31 require("ffluci.config")
32
33 table = {}
34 i18ndir = ffluci.fs.dirname(ffluci.util.__file__()) .. "/i18n/"
35
36 -- Clears the translation table
37 function clear()
38 table = {}
39 end
40
41 -- Loads a translation and copies its data into the global translation table
42 function load(file)
43 local f = loadfile(i18ndir .. file)
44 if f then
45 setfenv(f, table)
46 f()
47 return true
48 else
49 return false
50 end
51 end
52
53 -- Same as load but autocompletes the filename with .LANG from config.lang
54 function loadc(file)
55 return load(file .. "." .. ffluci.config.main.lang)
56 end
57
58 -- Returns the i18n-value defined by "key" or if there is no such: "default"
59 function translate(key, default)
60 return table[key] or default
61 end