ff917c6f38bb6ead44a77364bbab59b518b28241
[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                 end
67         end
68 end
69
70 --- Return the translated value for a specific translation key.
71 -- @param key   Default translation text
72 -- @return              Translated string
73 function translate(key)
74         return tparser.translate(key) or key
75 end
76
77 --- Return the translated value for a specific translation key and use it as sprintf pattern.
78 -- @param key           Default translation text
79 -- @param ...           Format parameters
80 -- @return                      Translated and formatted string
81 function translatef(key, ...)
82         return tostring(translate(key)):format(...)
83 end
84
85 --- Return the translated value for a specific translation key
86 -- and ensure that the returned value is a Lua string value.
87 -- This is the same as calling <code>tostring(translate(...))</code>
88 -- @param key           Default translation text
89 -- @return                      Translated string
90 function string(key)
91         return tostring(translate(key))
92 end
93
94 --- Return the translated value for a specific translation key and use it as sprintf pattern.
95 -- Ensure that the returned value is a Lua string value.
96 -- This is the same as calling <code>tostring(translatef(...))</code>
97 -- @param key           Default translation text
98 -- @param ...           Format parameters
99 -- @return                      Translated and formatted string
100 function stringf(key, ...)
101         return tostring(translate(key)):format(...)
102 end