diff options
author | Jo-Philipp Wich <jo@mein.io> | 2018-10-17 12:57:34 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2018-11-05 11:01:45 +0100 |
commit | 62102f4f0e8a88ffbdf44517f4ff737049a3f3bf (patch) | |
tree | 014bf7a3f47798a05bcdede54da8779f7a4e8491 /modules/luci-base/src/template_lualib.c | |
parent | 4623a58394b1cc71ddf24865a2f0639ee2119470 (diff) |
luci-base: template: add translation iterator function
Introduce a new luci.template.parser.get_translations() function which will
iterate all loaded translation entries and pass the to the given callback
function.
This is useful to expose the loaded translations in other formats, e.g. for
wrapping them into JSON feeds.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base/src/template_lualib.c')
-rw-r--r-- | modules/luci-base/src/template_lualib.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/modules/luci-base/src/template_lualib.c b/modules/luci-base/src/template_lualib.c index d5c8dd6b4..fbab2ccf6 100644 --- a/modules/luci-base/src/template_lualib.c +++ b/modules/luci-base/src/template_lualib.c @@ -129,6 +129,24 @@ static int template_L_change_catalog(lua_State *L) { return 1; } +static void template_L_get_translations_cb(uint32_t key, const char *val, int len, void *priv) { + lua_State *L = priv; + char hex[9]; + + luaL_checktype(L, 1, LUA_TFUNCTION); + snprintf(hex, sizeof(hex), "%08x", key); + + lua_pushvalue(L, 1); + lua_pushstring(L, hex); + lua_pushlstring(L, val, len); + lua_call(L, 2, 0); +} + +static int template_L_get_translations(lua_State *L) { + lmo_iterate(template_L_get_translations_cb, L); + return 0; +} + static int template_L_translate(lua_State *L) { size_t len; char *tr; @@ -168,6 +186,7 @@ static const luaL_reg R[] = { { "load_catalog", template_L_load_catalog }, { "close_catalog", template_L_close_catalog }, { "change_catalog", template_L_change_catalog }, + { "get_translations", template_L_get_translations }, { "translate", template_L_translate }, { "hash", template_L_hash }, { NULL, NULL } |