816d90310af692298b830b6cbbd8be47ebc1eaae
[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.util")
30 require("lmo")
31
32 table = {}
33 i18ndir = luci.util.libpath() .. "/i18n/"
34 loaded = {}
35 context = luci.util.threadlocal()
36 default = "en"
37
38 --- Clear the translation table.
39 function clear()
40 table = {}
41 end
42
43 --- Load a translation and copy its data into the translation table.
44 -- @param file Language file
45 -- @param lang Two-letter language code
46 -- @param force Force reload even if already loaded (optional)
47 -- @return Success status
48 function load(file, lang, force)
49 lang = lang and lang:gsub("_", "-") or ""
50 if force or not loaded[lang] or not loaded[lang][file] then
51 local f = lmo.open(i18ndir .. file .. "." .. lang .. ".lmo")
52 if f then
53 if not table[lang] then
54 table[lang] = { f }
55 setmetatable(table[lang], {
56 __index = function(tbl, key)
57 for i = 1, #tbl do
58 local s = rawget(tbl, i):lookup(key)
59 if s then return s end
60 end
61 end
62 })
63 else
64 table[lang][#table[lang]+1] = f
65 end
66
67 loaded[lang] = loaded[lang] or {}
68 loaded[lang][file] = true
69 return true
70 else
71 return false
72 end
73 else
74 return true
75 end
76 end
77
78 --- Load a translation file using the default translation language.
79 -- Alternatively load the translation of the fallback language.
80 -- @param file Language file
81 -- @param force Force reload even if already loaded (optional)
82 function loadc(file, force)
83 load(file, default, force)
84 if context.parent then load(file, context.parent, force) end
85 return load(file, context.lang, force)
86 end
87
88 --- Set the context default translation language.
89 -- @param lang Two-letter language code
90 function setlanguage(lang)
91 context.lang = lang:gsub("_", "-")
92 context.parent = (context.lang:match("^([a-z][a-z])_"))
93 end
94
95 --- Return the translated value for a specific translation key.
96 -- @param key Default translation text
97 -- @return Translated string
98 function translate(key)
99 return (table[context.lang] and table[context.lang][key])
100 or (table[context.parent] and table[context.parent][key])
101 or (table[default] and table[default][key])
102 or key
103 end
104
105 --- Return the translated value for a specific translation key and use it as sprintf pattern.
106 -- @param key Default translation text
107 -- @param ... Format parameters
108 -- @return Translated and formatted string
109 function translatef(key, ...)
110 return tostring(translate(key)):format(...)
111 end
112
113 --- Return the translated value for a specific translation key
114 -- and ensure that the returned value is a Lua string value.
115 -- This is the same as calling <code>tostring(translate(...))</code>
116 -- @param key Default translation text
117 -- @return Translated string
118 function string(key)
119 return tostring(translate(key))
120 end
121
122 --- Return the translated value for a specific translation key and use it as sprintf pattern.
123 -- Ensure that the returned value is a Lua string value.
124 -- This is the same as calling <code>tostring(translatef(...))</code>
125 -- @param key Default translation text
126 -- @param ... Format parameters
127 -- @return Translated and formatted string
128 function stringf(key, ...)
129 return tostring(translate(key)):format(...)
130 end