1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 --- LuCI translation library.
5 module("luci.i18n", package.seeall)
8 local tparser = require "luci.template.parser"
11 i18ndir = luci.util.libpath() .. "/i18n/"
13 context = luci.util.threadlocal()
16 --- Clear the translation table.
20 --- Load a translation and copy its data into the translation table.
21 -- @param file Language file
22 -- @param lang Two-letter language code
23 -- @param force Force reload even if already loaded (optional)
24 -- @return Success status
25 function load(file, lang, force)
28 --- Load a translation file using the default translation language.
29 -- Alternatively load the translation of the fallback language.
30 -- @param file Language file
31 -- @param force Force reload even if already loaded (optional)
32 function loadc(file, force)
35 --- Set the context default translation language.
36 -- @param lang Two-letter language code
37 function setlanguage(lang)
38 context.lang = lang:gsub("_", "-")
39 context.parent = (context.lang:match("^([a-z][a-z])_"))
40 if not tparser.load_catalog(context.lang, i18ndir) then
41 if context.parent then
42 tparser.load_catalog(context.parent, i18ndir)
49 --- Return the translated value for a specific translation key.
50 -- @param key Default translation text
51 -- @return Translated string
52 function translate(key)
53 return tparser.translate(key) or key
56 --- Return the translated value for a specific translation key and use it as sprintf pattern.
57 -- @param key Default translation text
58 -- @param ... Format parameters
59 -- @return Translated and formatted string
60 function translatef(key, ...)
61 return tostring(translate(key)):format(...)
64 --- Return the translated value for a specific translation key
65 -- and ensure that the returned value is a Lua string value.
66 -- This is the same as calling <code>tostring(translate(...))</code>
67 -- @param key Default translation text
68 -- @return Translated string
70 return tostring(translate(key))
73 --- Return the translated value for a specific translation key and use it as sprintf pattern.
74 -- Ensure that the returned value is a Lua string value.
75 -- This is the same as calling <code>tostring(translatef(...))</code>
76 -- @param key Default translation text
77 -- @param ... Format parameters
78 -- @return Translated and formatted string
79 function stringf(key, ...)
80 return tostring(translate(key)):format(...)