From 835efda6e381a32dc7f396d6ede9d7922d652b79 Mon Sep 17 00:00:00 2001 From: "tg(x)" <*@tg-x.net> Date: Tue, 23 Feb 2016 18:28:12 +0100 Subject: i18n --- extension/lib/i18n.ts | 44 ++++++++++++++++++++++++++++++++------------ extension/popup/popup.tsx | 6 +++++- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/extension/lib/i18n.ts b/extension/lib/i18n.ts index 93bf9a622..a722e8322 100644 --- a/extension/lib/i18n.ts +++ b/extension/lib/i18n.ts @@ -28,26 +28,46 @@ function init () { } } +function getI18nString (strings) { + let str = ''; + for (let i = 0; i < strings.length; i++) { + str += strings[i]; + if (i < strings.length - 1) { + str += '%'+ (i+1) +'$s'; + } + } + return str; +} + +function getPluralValue (values) { + // use the first number in values to determine plural form + for (let i = 0; i < values.length; i++) { + if ('number' == typeof values[i]) { + return values[i]; + } + } + return 1; +} + var i18n = function i18n(strings, ...values) { init(); - console.log('i18n:', strings[0]); - return jed.translate(strings[0]).fetch(); + let str = getI18nString (strings); + let n = getPluralValue (values); + console.log('i18n:', n, str, strings, values); + console.log('i18n:', jed.translate(str).ifPlural(n, str).fetch(...values);); + return jed.translate(str).ifPlural(n, str).fetch(...values); }; i18n.lang = chrome.i18n.getUILanguage(); i18n.strings = {}; -// Interpolate i8nized values with arbitrary objects and +// Interpolate i18nized values with arbitrary objects and // return array of strings/objects. i18n.parts = function(strings, ...values) { init(); - let str = ''; - for (let i = 0; i < strings.length; i++) { - str += strings[i]; - if (i < strings.length - 1) { - str += '%'+ (i+1) +'$s'; - } - } - console.log('i18n.parts:', str, ...values[0].children); - return jed.translate(str).fetch(...values[0].children); + let str = getI18nString (strings); + let n = getPluralValue (values); + console.log('i18n.parts:', n, str, values, ...values); + console.log('i18n.parts:', jed.translate(str).ifPlural(n, str).fetch(...values)); + return jed.translate(str).ifPlural(n, str).fetch(...values); }; diff --git a/extension/popup/popup.tsx b/extension/popup/popup.tsx index 11f722d4b..a5d271b5c 100644 --- a/extension/popup/popup.tsx +++ b/extension/popup/popup.tsx @@ -116,6 +116,11 @@ namespace WalletBalance { let link = m("a[href=https://demo.taler.net]", {config: openInExtension}, i18n`free KUDOS`); + // i18n test + 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}`); + return i18n.parts`You have no balance to show. Want to get some ${link}?`; } } @@ -272,4 +277,3 @@ function openTab(page) { }); } } - -- cgit v1.2.3