From 5738253998d7cb66d035579c13e2ee33b0bdcb25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 28 Feb 2017 10:02:29 +0100 Subject: [PATCH] FIX: locale fallback with pluralized strings --- app/assets/javascripts/locales/i18n.js | 13 +++------ test/javascripts/lib/i18n-test.js.es6 | 38 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/locales/i18n.js b/app/assets/javascripts/locales/i18n.js index 3722d746f07..0f2153e1037 100644 --- a/app/assets/javascripts/locales/i18n.js +++ b/app/assets/javascripts/locales/i18n.js @@ -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]; diff --git a/test/javascripts/lib/i18n-test.js.es6 b/test/javascripts/lib/i18n-test.js.es6 index e3f47b8b426..618feb58d6b 100644 --- a/test/javascripts/lib/i18n-test.js.es6 +++ b/test/javascripts/lib/i18n-test.js.es6 @@ -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"); +});