1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.i18n", package.seeall)
7 local tparser = require "luci.template.parser"
10 i18ndir = luci.util.libpath() .. "/i18n/"
12 context = luci.util.threadlocal()
18 function load(file, lang, force)
21 -- Alternatively load the translation of the fallback language.
22 function loadc(file, force)
25 function setlanguage(lang)
26 local code, subcode = lang:match("^([A-Za-z][A-Za-z])[%-_]([A-Za-z][A-Za-z])$")
27 if not (code and subcode) then
28 subcode = lang:match("^([A-Za-z][A-Za-z])$")
34 context.parent = code and code:lower()
35 context.lang = context.parent and context.parent.."-"..subcode:lower() or subcode:lower()
37 if tparser.load_catalog(context.lang, i18ndir) and
38 tparser.change_catalog(context.lang)
42 elseif context.parent then
43 if tparser.load_catalog(context.parent, i18ndir) and
44 tparser.change_catalog(context.parent)
53 function translate(key)
54 return tparser.translate(key) or key
57 function translatef(key, ...)
58 return tostring(translate(key)):format(...)
61 -- and ensure that the returned value is a Lua string value.
62 -- This is the same as calling <code>tostring(translate(...))</code>
64 return tostring(translate(key))
67 -- Ensure that the returned value is a Lua string value.
68 -- This is the same as calling <code>tostring(translatef(...))</code>
69 function stringf(key, ...)
70 return tostring(translate(key)):format(...)
75 tparser.get_translations(function(k, v) rv[k] = v end)