FIX: locale fallback with pluralized strings

This commit is contained in:
Régis Hanol 2017-02-28 10:02:29 +01:00
parent 54577db8a4
commit 5738253998
2 changed files with 42 additions and 9 deletions

View File

@ -151,7 +151,7 @@ I18n.translate = function(scope, options) {
try {
if (typeof translation === "object") {
if (typeof options.count === "number") {
return this.pluralize(options.count, scope, options);
return this.pluralize(translation, scope, options);
} else {
return translation;
}
@ -258,16 +258,11 @@ I18n.findAndTranslateValidNode = function(keys, translation) {
return null;
};
I18n.pluralize = function(count, scope, options) {
var translation;
try { translation = this.lookup(scope, options); } catch (error) {}
if (!translation) { return this.missingTranslation(scope); }
I18n.pluralize = function(translation, scope, options) {
options = this.prepareOptions(options);
options.count = count.toString();
var count = options.count.toString();
var pluralizer = this.pluralizer(this.currentLocale());
var pluralizer = this.pluralizer(options.locale || this.currentLocale());
var key = pluralizer(Math.abs(count));
var keys = ((typeof key === "object") && (key instanceof Array)) ? key : [key];

View File

@ -13,6 +13,14 @@ module("lib:i18n", {
"reply": {
"title": "Répondre",
}
},
"character_count": {
"zero": "{{count}} ZERO",
"one": "{{count}} ONE",
"two": "{{count}} TWO",
"few": "{{count}} FEW",
"many": "{{count}} MANY",
"other": "{{count}} OTHER"
}
}
},
@ -25,10 +33,24 @@ module("lib:i18n", {
"reply": {
"help": "begin composing a reply to this topic"
}
},
"word_count": {
"one": "1 word",
"other": "{{count}} words"
}
}
}
};
// fake pluralization rules
I18n.pluralizationRules.fr = function(n) {
if (n == 0) return "zero";
if (n == 1) return "one";
if (n == 2) return "two";
if (n >= 3 && n <= 9) return "few";
if (n >= 10 && n <= 99) return "many";
return "other";
}
},
teardown() {
@ -53,3 +75,19 @@ test("extra translations", function() {
equal(I18n.t("admin.title"), "Discourse Admin", "it check extra translations when they exists");
});
test("pluralizations", function() {
equal(I18n.t("character_count", { count: 0 }), "0 ZERO");
equal(I18n.t("character_count", { count: 1 }), "1 ONE");
equal(I18n.t("character_count", { count: 2 }), "2 TWO");
equal(I18n.t("character_count", { count: 3 }), "3 FEW");
equal(I18n.t("character_count", { count: 10 }), "10 MANY");
equal(I18n.t("character_count", { count: 100 }), "100 OTHER");
equal(I18n.t("word_count", { count: 0 }), "0 words");
equal(I18n.t("word_count", { count: 1 }), "1 word");
equal(I18n.t("word_count", { count: 2 }), "2 words");
equal(I18n.t("word_count", { count: 3 }), "3 words");
equal(I18n.t("word_count", { count: 10 }), "10 words");
equal(I18n.t("word_count", { count: 100 }), "100 words");
});