Merge pull request #1818 from dibdot/lxc_fix
[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 module("luci.i18n", package.seeall)
5 require("luci.util")
6
7 local tparser = require "luci.template.parser"
8
9 table   = {}
10 i18ndir = luci.util.libpath() .. "/i18n/"
11 loaded  = {}
12 context = luci.util.threadlocal()
13 default = "en"
14
15 function clear()
16 end
17
18 function load(file, lang, force)
19 end
20
21 -- Alternatively load the translation of the fallback language.
22 function loadc(file, force)
23 end
24
25 function setlanguage(lang)
26         context.lang   = lang:gsub("_", "-")
27         context.parent = (context.lang:match("^([a-z][a-z])_"))
28         if not tparser.load_catalog(context.lang, i18ndir) then
29                 if context.parent then
30                         tparser.load_catalog(context.parent, i18ndir)
31                         return context.parent
32                 end
33         end
34         return context.lang
35 end
36
37 function translate(key)
38         return tparser.translate(key) or key
39 end
40
41 function translatef(key, ...)
42         return tostring(translate(key)):format(...)
43 end
44
45 -- and ensure that the returned value is a Lua string value.
46 -- This is the same as calling <code>tostring(translate(...))</code>
47 function string(key)
48         return tostring(translate(key))
49 end
50
51 -- Ensure that the returned value is a Lua string value.
52 -- This is the same as calling <code>tostring(translatef(...))</code>
53 function stringf(key, ...)
54         return tostring(translate(key)):format(...)
55 end