From 9939fc5a26d07da4756497bb6a7dc51dcf379e98 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 23 Jan 2020 21:53:26 +0100 Subject: 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 --- build/i18n-scan.pl | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'build/i18n-scan.pl') diff --git a/build/i18n-scan.pl b/build/i18n-scan.pl index ddec094caa..e46863942b 100755 --- a/build/i18n-scan.pl +++ b/build/i18n-scan.pl @@ -124,14 +124,24 @@ sub preprocess_htm($$) { '=' => '(%s)', '_' => 'translate([==[%s]==])', ':' => 'translate([==[%s]==])', - '+' => 'include([==[%s]==)', + '+' => 'include([==[%s]==])', '#' => '--[==[%s]==]', '' => '%s' }; # Translate the .htm source into a valid Lua source using bracket quotes # to avoid the need for complex escaping. - $source =~ s|<%-?([=_:+#]?)(.*?)-?%>|sprintf "]==]; $sub->{$1}; [==[", $2|sge; + $source =~ s!<%-?([=_:+#]?)(.*?)-?%>! + my $t = $1; + my $s = $2; + + # Split translation expressions on first non-escaped pipe. + if ($t eq ':' || $t eq '_') { + $s =~ s/^((?:[^\|\\]|\\.)*)\|(.*)$/$1]==],[==[$2/; + } + + sprintf "]==]; $sub->{$t}; [==[", $s + !sge; # Discover expressions like "lng.translate(...)" or "luci.i18n.translate(...)" # and return them as extra keyword so that xgettext recognizes such expressions -- cgit v1.2.3