luci-base: expose system translations to JavaScript
Add a new /admin/translations/ endpoint which exposes the loaded system translations as JavaScript file. Once referenced by <script>, the endpoint will create a `window.TR` object containing the entire translation string table for use on the client side. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
parent
8ff68c772b
commit
c916b5ed87
1 changed files with 24 additions and 0 deletions
|
@ -82,6 +82,9 @@ function index()
|
|||
page.leaf = true
|
||||
end
|
||||
|
||||
page = entry({"admin", "translations"}, call("action_translations"), nil)
|
||||
page.leaf = true
|
||||
|
||||
-- Logout is last
|
||||
entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
|
||||
end
|
||||
|
@ -102,6 +105,27 @@ function action_logout()
|
|||
luci.http.redirect(dsp.build_url())
|
||||
end
|
||||
|
||||
function action_translations(lang)
|
||||
local i18n = require "luci.i18n"
|
||||
local http = require "luci.http"
|
||||
local fs = require "nixio".fs
|
||||
|
||||
if lang and #lang > 0 then
|
||||
lang = i18n.setlanguage(lang)
|
||||
if lang then
|
||||
local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
|
||||
if s then
|
||||
http.header("Cache-Control", "public, max-age=31536000")
|
||||
http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
http.prepare_content("application/javascript; charset=utf-8")
|
||||
http.write("window.TR=")
|
||||
http.write_json(i18n.dump())
|
||||
end
|
||||
|
||||
|
||||
function lease_status()
|
||||
local s = require "luci.tools.status"
|
||||
|
|
Loading…
Reference in a new issue