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)
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
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(...)
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
91 return tostring(translate(key))
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(...)