summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/src/template_lualib.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-01-23 21:53:26 +0100
committerJo-Philipp Wich <jo@mein.io>2020-01-25 23:21:35 +0100
commit9939fc5a26d07da4756497bb6a7dc51dcf379e98 (patch)
treee79785f03912d2cec48fdf59f132a42528bd4c6f /modules/luci-base/src/template_lualib.c
parent6f6f3e84ca0bc04cd69433f723371fdd3416add9 (diff)
luci-base: add support for plural translations and contexts in Lua api
- Introduce a new luci.template.parser.ntranslate() function which takes a count, a singular and a plural translation string as well as an optional context argument and returns the appropriate, language specific plural translation. - Introduce an optional translation context argument in the existing luci.template.parser.translate() function - Support translation contexts in LuCI template directives. Translation messages are split on the first unescaped pipe character and the reamining string after the pipe is treated as context. Examples: - `string.format(p.ntranslate(n, "1 apple", "%d apples"), n)` will return an appropriate plural translation for the given amount. - `translate("Load", "The system load")` will return an appropiate translation for `Load`, using `The system load` as disambiguation context (a `msgctxt` directive in *.po files). - Likewise `<%:Load|The system load%>` will translate the word `Load` while using the remainder of the string as context. - To use pipes in translations strings literally, they must be escaped: `<%:Use the "\|" character%>` will translate the literal string `Use the "|" character`. 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.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/modules/luci-base/src/template_lualib.c b/modules/luci-base/src/template_lualib.c
index fbab2ccf6e..4efd9f1de6 100644
--- a/modules/luci-base/src/template_lualib.c
+++ b/modules/luci-base/src/template_lualib.c
@@ -148,12 +148,37 @@ static int template_L_get_translations(lua_State *L) {
}
static int template_L_translate(lua_State *L) {
- size_t len;
+ size_t len, ctxlen = 0;
char *tr;
int trlen;
const char *key = luaL_checklstring(L, 1, &len);
+ const char *ctx = luaL_optlstring(L, 2, NULL, &ctxlen);
+
+ switch (lmo_translate_ctxt(key, len, ctx, ctxlen, &tr, &trlen))
+ {
+ case 0:
+ lua_pushlstring(L, tr, trlen);
+ return 1;
+
+ case -1:
+ return 0;
+ }
+
+ lua_pushnil(L);
+ lua_pushstring(L, "no catalog loaded");
+ return 2;
+}
+
+static int template_L_ntranslate(lua_State *L) {
+ size_t slen, plen, ctxlen = 0;
+ char *tr;
+ int trlen;
+ int n = luaL_checkinteger(L, 1);
+ const char *skey = luaL_checklstring(L, 2, &slen);
+ const char *pkey = luaL_checklstring(L, 3, &plen);
+ const char *ctx = luaL_optlstring(L, 4, NULL, &ctxlen);
- switch (lmo_translate(key, len, &tr, &trlen))
+ switch (lmo_translate_plural_ctxt(n, skey, slen, pkey, plen, ctx, ctxlen, &tr, &trlen))
{
case 0:
lua_pushlstring(L, tr, trlen);
@@ -188,6 +213,7 @@ static const luaL_reg R[] = {
{ "change_catalog", template_L_change_catalog },
{ "get_translations", template_L_get_translations },
{ "translate", template_L_translate },
+ { "ntranslate", template_L_ntranslate },
{ "hash", template_L_hash },
{ NULL, NULL }
};