luci-base: optimize luci.tools.webadmin.iface_get_network()
[project/luci.git] / modules / luci-base / luasrc / i18n.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 --- LuCI translation library.
5 module("luci.i18n", package.seeall)
6 require("luci.util")
7
8 local tparser = require "luci.template.parser"
9
10 table   = {}
11 i18ndir = luci.util.libpath() .. "/i18n/"
12 loaded  = {}
13 context = luci.util.threadlocal()
14 default = "en"
15
16 --- Clear the translation table.
17 function clear()
18 end
19
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)
26 end
27
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)
33 end
34
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)
43                         return context.parent
44                 end
45         end
46         return context.lang
47 end
48
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
54 end
55
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(...)
62 end
63
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
69 function string(key)
70         return tostring(translate(key))
71 end
72
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(...)
81 end