add i18n.pluralize(); use fixed po2json; fix i18n fallback
This commit is contained in:
parent
646bb769c3
commit
b3242484c1
@ -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
|
||||
|
@ -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 ("function" !== typeof JedModule) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(i18n.lang in i18n.strings)) {
|
||||
i18n.lang = "en-US";
|
||||
}
|
||||
|
||||
const jedModule = window["Jed"];
|
||||
|
||||
if (!jedModule) {
|
||||
return;
|
||||
}
|
||||
|
||||
jed = jedModule(i18n.strings[i18n.lang]);
|
||||
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;
|
||||
};
|
||||
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user