libs/web: Small improvements, added inline documentation
[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 --- LuCI translation library.
28 module("luci.i18n", package.seeall)
29 require("luci.sys")
30
31 table = {}
32 i18ndir = luci.sys.libpath() .. "/i18n/"
33 loaded = {}
34 context = luci.util.threadlocal()
35 default = "en"
36
37 --- Clear the translation table.
38 function clear()
39 table = {}
40 end
41
42 --- Load a translation and copy its data into the translation table.
43 -- @param file Language file
44 -- @param lang Two-letter language code
45 -- @param force Force reload even if already loaded (optional)
46 -- @return Success status
47 function load(file, lang, force)
48 lang = lang or ""
49 if force or not loaded[lang] or not loaded[lang][file] then
50 local f = loadfile(i18ndir .. file .. "." .. lang .. ".lua")
51 if f then
52 table[lang] = table[lang] or {}
53 setfenv(f, table[lang])
54 f()
55 loaded[lang] = loaded[lang] or {}
56 loaded[lang][file] = true
57 return true
58 else
59 return false
60 end
61 else
62 return true
63 end
64 end
65
66 --- Load a translation file using the default translation language.
67 -- Alternatively load the translation of the fallback language.
68 -- @param file Language file
69 -- @param force Force reload even if already loaded (optional)
70 function loadc(file, force)
71 load(file, default, force)
72 return load(file, context.lang, force)
73 end
74
75 --- Set the context default translation language.
76 -- @param lang Two-letter language code
77 function setlanguage(lang)
78 context.lang = lang
79 end
80
81 --- Return the translated value for a specific translation key.
82 -- @param key Translation key
83 -- @param def Default translation
84 -- @return Translated string
85 function translate(key, def)
86 return (table[context.lang] and table[context.lang][key])
87 or (table[default] and table[default][key])
88 or def
89 end
90
91 --- Return the translated value for a specific translation key and use it as sprintf pattern.
92 -- @param key Translation key
93 -- @param default Default translation
94 -- @param ... Format parameters
95 -- @return Translated and formatted string
96 function translatef(key, default, ...)
97 return translate(key, default):format(...)
98 end