diff options
author | tg(x) <*@tg-x.net> | 2016-02-25 00:48:39 +0100 |
---|---|---|
committer | tg(x) <*@tg-x.net> | 2016-02-25 00:48:39 +0100 |
commit | b3242484c1cf08da23ac2d5fd94645a98bc5f22d (patch) | |
tree | a971ae6d22c0ba651e0129e4b59f200e4e96e5d9 | |
parent | 646bb769c3ee948957cc425c50fbc7749f2728ba (diff) |
add i18n.pluralize(); use fixed po2json; fix i18n fallback
-rw-r--r-- | extension/Makefile | 2 | ||||
-rw-r--r-- | extension/lib/i18n.ts | 51 | ||||
-rw-r--r-- | extension/package.json | 2 |
3 files changed, 38 insertions, 17 deletions
diff --git a/extension/Makefile b/extension/Makefile index a5bef133a..cb8e63bf2 100644 --- a/extension/Makefile +++ b/extension/Makefile @@ -49,7 +49,7 @@ pogen: $(ts) pogen/pogen.js node_modules lib/i18n-strings.js: $(ts) node_modules truncate -s0 $@ for lang in $(langs); do \ - $(po2json) -f jed1.x -d $$lang $(poname)-$$lang.po $(poname)-$$lang.json; \ + $(po2json) -F -f jed1.x -d $$lang $(poname)-$$lang.po $(poname)-$$lang.json; \ (echo -n "i18n.strings['$$lang'] = "; cat $(poname)-$$lang.json; echo ';') >> $@; \ rm $(poname)-$$lang.json; \ done diff --git a/extension/lib/i18n.ts b/extension/lib/i18n.ts index d442320fb..1fcc97e9d 100644 --- a/extension/lib/i18n.ts +++ b/extension/lib/i18n.ts @@ -17,33 +17,38 @@ "use strict"; declare var i18n: any; + +const JedModule = window["Jed"]; var jed; var i18nDebug = false; + +/** Initialize Jed */ function init () { - if ("object" !== typeof jed) { + if ("object" === typeof jed) { return; } - if (!(i18n.lang in i18n.strings)) { - i18n.lang = "en-US"; - } - - const jedModule = window["Jed"]; - - if (!jedModule) { + if ("function" !== typeof JedModule) { return; } - jed = jedModule(i18n.strings[i18n.lang]); + if (!(i18n.lang in i18n.strings)) { + i18n.lang = "en-US"; + } + jed = new JedModule(i18n.strings[i18n.lang]); 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.`)); } } + +/** Convert template strings to a msgid */ function toI18nString(strings) { let str = ""; for (let i = 0; i < strings.length; i++) { @@ -55,22 +60,25 @@ function toI18nString(strings) { return str; } + +/** Use the first number in values to determine plural form */ 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]) { + if ('number' === typeof values[i]) { return values[i]; } } return 1; } + var i18n = <any>function i18n(strings, ...values) { init(); - if (!jed) { + if ("object" !== typeof jed) { // Fallback implementation in case i18n lib is not there return String.raw(strings, ...values); } + let str = toI18nString (strings); let n = getPluralValue (values); let tr = jed.translate(str).ifPlural(n, str).fetch(...values); @@ -85,11 +93,14 @@ var i18n = <any>function i18n(strings, ...values) { i18n.lang = chrome.i18n.getUILanguage(); i18n.strings = {}; -// Interpolate i18nized values with arbitrary objects and -// return array of strings/objects. + +/** + * Interpolate i18nized values with arbitrary objects. + * @return Array of strings/objects. + */ i18n.parts = function(strings, ...values) { init(); - if (!jed) { + if ("object" !== typeof jed) { // Fallback implementation in case i18n lib is not there let parts = []; @@ -101,6 +112,7 @@ i18n.parts = function(strings, ...values) { } return parts; } + let str = toI18nString (strings); let n = getPluralValue (values); let tr = jed.ngettext(str, str, n).split(/%(\d+)\$s/); @@ -120,3 +132,12 @@ i18n.parts = function(strings, ...values) { } return parts; }; + + +/** + * Pluralize based on first numeric parameter in the template. + * @todo The plural argument is used for extraction by pogen.js + */ +i18n.pluralize = function (singular, plural) { + return singular; +}; diff --git a/extension/package.json b/extension/package.json index a1934b7c8..7cef4f55c 100644 --- a/extension/package.json +++ b/extension/package.json @@ -23,7 +23,7 @@ "jed": "^1.1.0", "map-stream": "0.0.6", "mocha": "^2.3.4", - "po2json": "^0.4.1", + "po2json": "git://taler.net/po2json.git", "systemjs": "^0.19.14", "through2": "^2.0.1", "typescript": "^1.8.0-dev.20160118", |