2 LuCI - Internationalisation
5 A very minimalistic but yet effective internationalisation module
11 Copyright 2008 Steven Barth <steven@midlink.org>
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
17 http://www.apache.org/licenses/LICENSE-2.0
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.
27 --- LuCI translation library.
28 module("luci.i18n", package.seeall)
31 local tparser = require "luci.template.parser"
34 i18ndir = luci.util.libpath() .. "/i18n/"
36 context = luci.util.threadlocal()
39 --- Clear the translation table.
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)
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)
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)
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
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(...)
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
93 return tostring(translate(key))
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(...)