diff options
author | Jo-Philipp Wich <jo@mein.io> | 2018-10-18 13:44:39 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2018-11-05 11:01:45 +0100 |
commit | c916b5ed875675749c3a04c7b95340a5d4443722 (patch) | |
tree | 457e344c0628a2f5777956ab4c72dfd767f74779 | |
parent | 8ff68c772b76855954cc7a2c163df4056fa19c56 (diff) |
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>
-rw-r--r-- | modules/luci-base/luasrc/controller/admin/index.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/modules/luci-base/luasrc/controller/admin/index.lua b/modules/luci-base/luasrc/controller/admin/index.lua index 9a084d10e2..39004c6760 100644 --- a/modules/luci-base/luasrc/controller/admin/index.lua +++ b/modules/luci-base/luasrc/controller/admin/index.lua @@ -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" |