luci/modules/luci-base/src/plural_formula.y
Jo-Philipp Wich 9939fc5a26 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>
2020-01-25 23:21:35 +01:00

44 lines
1.3 KiB
Plaintext

%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; }