From e9e7c47c7411aa28f74c0aee458bad9b14d94302 Mon Sep 17 00:00:00 2001 From: "tg(x)" <*@tg-x.net> Date: Thu, 25 Feb 2016 02:35:19 +0100 Subject: i18n: plural arg --- extension/lib/i18n.ts | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'extension/lib/i18n.ts') diff --git a/extension/lib/i18n.ts b/extension/lib/i18n.ts index 1fcc97e9d..60da6644f 100644 --- a/extension/lib/i18n.ts +++ b/extension/lib/i18n.ts @@ -23,6 +23,23 @@ var jed; var i18nDebug = false; +class PluralNumber { + n: number; + + constructor(n) { + this.n = n; + } + + valueOf () { + return this.n; + } + + toString () { + return this.n.toString(); + } +} + + /** Initialize Jed */ function init () { if ("object" === typeof jed) { @@ -39,11 +56,13 @@ function init () { if (i18nDebug) { let link = m("a[href=https://demo.taler.net]", i18n`free KUDOS`); - let amount = 5, currency = "EUR", date = new Date(), text = "demo.taler.net"; - console.log(i18n`Your balance on ${date} is ${amount} KUDO. Get more at ${text}`); - console.log(i18n.parts`Your balance on ${date} is ${amount} KUDO. Get more at ${link}`); - console.log(i18n.pluralize(i18n`Your balance is ${amount} KUDO.`, - i18n`Your balance is ${amount} KUDOs.`)); + let i = 1, amount = 5, currency = "EUR", date = new Date(), text = "demo.taler.net"; + console.log(i18n`DEBUG: Your balance on ${date} is ${amount} KUDO. Get more at ${text}`); + console.log(i18n.parts`DEBUG: Your balance on ${date} is ${amount} KUDO. Get more at ${link}`); + console.log(i18n.pluralize(i18n`DEBUG: Your balance is ${amount} KUDO.`, + `DEBUG: Your balance is ${amount} KUDOs.`)); + console.log(i18n.pluralize(i18n`DEBUG: #${i}: Your balance is ${i18n.number(amount)} KUDO.`, + `DEBUG: #${i}: Your balance is ${i18n.number(amount)} KUDOs.`)); } } @@ -63,12 +82,15 @@ function toI18nString(strings) { /** Use the first number in values to determine plural form */ function getPluralValue (values) { + let n = null; for (let i = 0; i < values.length; i++) { - if ('number' === typeof values[i]) { - return values[i]; + if ("number" === typeof values[i] || values[i] instanceof PluralNumber) { + if (null === n || values[i] instanceof PluralNumber) { + n = values[i].valueOf(); + } } } - return 1; + return (null === n) ? 1 : n; } @@ -141,3 +163,11 @@ i18n.parts = function(strings, ...values) { i18n.pluralize = function (singular, plural) { return singular; }; + + +/** + * Return a number that is used to determine the plural form for a template. + */ +i18n.number = function (n : number) { + return new PluralNumber (n); +} -- cgit v1.2.3