diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-01-23 21:53:26 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-01-25 23:21:35 +0100 |
commit | 9939fc5a26d07da4756497bb6a7dc51dcf379e98 (patch) | |
tree | e79785f03912d2cec48fdf59f132a42528bd4c6f /modules/luci-base/src/plural_formula.y | |
parent | 6f6f3e84ca0bc04cd69433f723371fdd3416add9 (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/plural_formula.y')
-rw-r--r-- | modules/luci-base/src/plural_formula.y | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/modules/luci-base/src/plural_formula.y b/modules/luci-base/src/plural_formula.y new file mode 100644 index 0000000000..1623f8b282 --- /dev/null +++ b/modules/luci-base/src/plural_formula.y @@ -0,0 +1,43 @@ +%name pluralParse +%token_type {int} +%extra_argument {struct parse_state *s} + +%right T_QMARK. +%left T_OR. +%left T_AND. +%left T_EQ T_NE. +%left T_LT T_LE T_GT T_GE. +%left T_ADD T_SUB. +%left T_MUL T_DIV T_MOD. +%right T_NOT. +%nonassoc T_COLON T_N T_LPAREN T_RPAREN. + +%include { +#include <assert.h> + +struct parse_state { + int num; + int res; +}; +} + +input ::= expr(A). { s->res = A; } + +expr(A) ::= expr(B) T_QMARK expr(C) T_COLON expr(D). { A = B ? C : D; } +expr(A) ::= expr(B) T_OR expr(C). { A = B || C; } +expr(A) ::= expr(B) T_AND expr(C). { A = B && C; } +expr(A) ::= expr(B) T_EQ expr(C). { A = B == C; } +expr(A) ::= expr(B) T_NE expr(C). { A = B != C; } +expr(A) ::= expr(B) T_LT expr(C). { A = B < C; } +expr(A) ::= expr(B) T_LE expr(C). { A = B <= C; } +expr(A) ::= expr(B) T_GT expr(C). { A = B > C; } +expr(A) ::= expr(B) T_GE expr(C). { A = B >= C; } +expr(A) ::= expr(B) T_ADD expr(C). { A = B + C; } +expr(A) ::= expr(B) T_SUB expr(C). { A = B - C; } +expr(A) ::= expr(B) T_MUL expr(C). { A = B * C; } +expr(A) ::= expr(B) T_DIV expr(C). { A = B / C; } +expr(A) ::= expr(B) T_MOD expr(C). { A = B % C; } +expr(A) ::= T_NOT expr(B). { A = !B; } +expr(A) ::= T_N. { A = s->num; } +expr(A) ::= T_NUM(B). { A = B; } +expr(A) ::= T_LPAREN expr(B) T_RPAREN. { A = B; } |