summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-01-22 21:56:28 +0100
committerJo-Philipp Wich <jo@mein.io>2020-01-22 22:02:24 +0100
commit894752610d80a2fa1cdb6a845526728eeb0bc984 (patch)
tree73015bfd148e51cbda12d6e3264c75910bea8011
parent9c1bac4168d151c2a3a5a1f957adbd68b6ae0746 (diff)
luci-base: cbi.js: support plural translations and disambiguation contexts
- Implement `N_(count, "String singular", "String plural" [, "Context"])` plural translation function. - Extend `_()` to optionally accept a second disambiguation context argument. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--modules/luci-base/htdocs/luci-static/resources/cbi.js20
1 files changed, 18 insertions, 2 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/cbi.js b/modules/luci-base/htdocs/luci-static/resources/cbi.js
index ff198027e3..d962a1d1af 100644
--- a/modules/luci-base/htdocs/luci-static/resources/cbi.js
+++ b/modules/luci-base/htdocs/luci-static/resources/cbi.js
@@ -94,8 +94,24 @@ function sfh(s) {
return (0x100000000 + hash).toString(16).substr(1);
}
-function _(s) {
- return (window.TR && TR[sfh(String(s).trim().replace(/[ \t\n]+/g, ' '))]) || s;
+var plural_function = null;
+
+function trimws(s) {
+ return String(s).trim().replace(/[ \t\n]+/g, ' ');
+}
+
+function _(s, c) {
+ return (window.TR && TR[sfh(trimws(s))]) || s;
+}
+
+function N_(n, s, p, c) {
+ if (plural_function == null && window.TR)
+ plural_function = new Function('n', (TR['00000000'] || 'plural=(n != 1);') + 'return +plural');
+
+ var i = plural_function ? plural_function(n) : (n != 1),
+ k = (c != null ? trimws(c) + '\u0001' : '') + trimws(s) + '\u0002' + i.toString();
+
+ return (window.TR && TR[sfh(k)]) || (i ? p : s);
}