luci-0.11: merge outstanding trunk changes
[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
31 local tparser = require "luci.template.parser"
32
33 table = {}
34 i18ndir = luci.util.libpath() .. "/i18n/"
35 loaded = {}
36 context = luci.util.threadlocal()
37 default = "en"
38
39 --- Clear the translation table.
40 function clear()
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 end
50
51 --- Load a translation file using the default translation language.
52 -- Alternatively load the translation of the fallback language.
53 -- @param file Language file
54 -- @param force Force reload even if already loaded (optional)
55 function loadc(file, force)
56 end
57
58 --- Set the context default translation language.
59 -- @param lang Two-letter language code
60 function setlanguage(lang)
61 context.lang = lang:gsub("_", "-")
62 context.parent = (context.lang:match("^([a-z][a-z])_"))
63 if not tparser.load_catalog(context.lang, i18ndir) then
64 if context.parent then
65 tparser.load_catalog(context.parent, i18ndir)
66 return context.parent
67 end
68 end
69 return context.lang
70 end
71
72 --- Return the translated value for a specific translation key.
73 -- @param key Default translation text
74 -- @return Translated string
75 function translate(key)
76 return tparser.translate(key) or key
77 end
78
79 --- Return the translated value for a specific translation key and use it as sprintf pattern.
80 -- @param key Default translation text
81 -- @param ... Format parameters
82 -- @return Translated and formatted string
83 function translatef(key, ...)
84 return tostring(translate(key)):format(...)
85 end
86
87 --- Return the translated value for a specific translation key
88 -- and ensure that the returned value is a Lua string value.
89 -- This is the same as calling <code>tostring(translate(...))</code>
90 -- @param key Default translation text
91 -- @return Translated string
92 function string(key)
93 return tostring(translate(key))
94 end
95
96 --- Return the translated value for a specific translation key and use it as sprintf pattern.
97 -- Ensure that the returned value is a Lua string value.
98 -- This is the same as calling <code>tostring(translatef(...))</code>
99 -- @param key Default translation text
100 -- @param ... Format parameters
101 -- @return Translated and formatted string
102 function stringf(key, ...)
103 return tostring(translate(key)):format(...)
104 end