9a11a9dc8ff6744bdf89a63cfe81bee026025c77
[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   Translation key
97 -- @param def   Default translation
98 -- @return              Translated string
99 function translate(key, def)
100         return (table[context.lang] and table[context.lang][key])
101                 or (table[context.parent] and table[context.parent][key])
102                 or (table[default] and table[default][key])
103                 or def
104 end
105
106 --- Return the translated value for a specific translation key and use it as sprintf pattern.
107 -- @param key           Translation key
108 -- @param default       Default translation
109 -- @param ...           Format parameters
110 -- @return                      Translated and formatted string
111 function translatef(key, default, ...)
112         return tostring(translate(key, default)):format(...)
113 end
114
115 --- Return the translated value for a specific translation key
116 -- and ensure that the returned value is a Lua string value.
117 -- This is the same as calling <code>tostring(translate(...))</code>
118 -- @param key           Translation key
119 -- @param default       Default translation
120 -- @return                      Translated string
121 function string(key, default)
122         return tostring(translate(key, default))
123 end
124
125 --- Return the translated value for a specific translation key and use it as sprintf pattern.
126 -- Ensure that the returned value is a Lua string value.
127 -- This is the same as calling <code>tostring(translatef(...))</code>
128 -- @param key           Translation key
129 -- @param default       Default translation
130 -- @param ...           Format parameters
131 -- @return                      Translated and formatted string
132 function stringf(key, default, ...)
133         return tostring(translate(key, default)):format(...)
134 end