luci-base: template: add translation iterator function
authorJo-Philipp Wich <jo@mein.io>
Wed, 17 Oct 2018 10:57:34 +0000 (12:57 +0200)
committerJo-Philipp Wich <jo@mein.io>
Mon, 5 Nov 2018 10:01:45 +0000 (11:01 +0100)
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>
modules/luci-base/src/template_lmo.c
modules/luci-base/src/template_lmo.h
modules/luci-base/src/template_lualib.c

index 3d1eaf4e08cffa38728d7ef9537d920c45adcb22..cd4c609a743f36f42be8853cd0a43e42c61610bc 100644 (file)
@@ -216,7 +216,7 @@ int lmo_load_catalog(const char *lang, const char *dir)
        if (!_lmo_active_catalog)
                _lmo_active_catalog = cat;
 
-       return 0;
+       return cat->archives ? 0 : -1;
 
 err:
        if (dh) closedir(dh);
@@ -301,6 +301,20 @@ int lmo_translate(const char *key, int keylen, char **out, int *outlen)
        return -1;
 }
 
+void lmo_iterate(lmo_iterate_cb_t cb, void *priv)
+{
+       unsigned int i;
+       lmo_entry_t *e;
+       lmo_archive_t *ar;
+
+       if (!_lmo_active_catalog)
+               return;
+
+       for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
+               for (i = 0, e = &ar->index[0]; i < ar->length; e = &ar->index[++i])
+                       cb(ntohl(e->key_id), ar->mmap + ntohl(e->offset), ntohl(e->length), priv);
+}
+
 void lmo_close_catalog(const char *lang)
 {
        lmo_archive_t *ar, *next;
index f251c63ddde1d88f1fab7c924b4e5fd7951ca034..587884ea3aabc7cab3bfca77b082515d288c3255 100644 (file)
@@ -73,6 +73,7 @@ struct lmo_catalog {
 
 typedef struct lmo_catalog lmo_catalog_t;
 
+typedef void (*lmo_iterate_cb_t)(uint32_t, const char *, int, void *);
 
 uint32_t sfh_hash(const char *data, int len);
 uint32_t lmo_canon_hash(const char *data, int len);
@@ -87,6 +88,7 @@ extern lmo_catalog_t *_lmo_active_catalog;
 int lmo_load_catalog(const char *lang, const char *dir);
 int lmo_change_catalog(const char *lang);
 int lmo_translate(const char *key, int keylen, char **out, int *outlen);
+void lmo_iterate(lmo_iterate_cb_t cb, void *priv);
 void lmo_close_catalog(const char *lang);
 
 #endif
index d5c8dd6b4c9f1f20c3da8eeebd45bf13a20ea6ae..fbab2ccf6eba5cdfc10a6fea7b71407c6213c651 100644 (file)
@@ -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 }