From b607383d7dbc374f2b005886d45e36c91ac52b5e Mon Sep 17 00:00:00 2001 From: aalmonte Date: Wed, 29 Nov 2017 12:37:06 -0300 Subject: [PATCH 1/2] Default Plural Form Support By default supports plural forms 0 and 1. This allows to use locales not listed in __getPluralForm function. --- src/lang.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lang.js b/src/lang.js index ae92676..fe227c5 100644 --- a/src/lang.js +++ b/src/lang.js @@ -222,8 +222,12 @@ } var pluralForm = this._getPluralForm(number); + //Check if pluralForm exists + if (messageParts.length > pluralForm) { + return messageParts[pluralForm]; + } - return messageParts[pluralForm]; + return messageParts[0]; }; /** @@ -645,7 +649,10 @@ : 5)))); default: - return 0; + //By default supports two plural forms + return (count == 1) + ? 0 + : 1; } }; From 879dfc81d06e8e7548a8aa2361ca18c571c116bb Mon Sep 17 00:00:00 2001 From: aalmonte Date: Wed, 29 Nov 2017 14:33:59 -0300 Subject: [PATCH 2/2] Locale parameter - Use locale parameter to resolve plural form of messages - Adds locale parameter to transChoice method - Adds locale parameter and fallback to default in getPluralForm method --- src/lang.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lang.js b/src/lang.js index fe227c5..d0337ac 100644 --- a/src/lang.js +++ b/src/lang.js @@ -221,7 +221,7 @@ } } - var pluralForm = this._getPluralForm(number); + var pluralForm = this._getPluralForm(number, locale); //Check if pluralForm exists if (messageParts.length > pluralForm) { return messageParts[pluralForm]; @@ -236,11 +236,12 @@ * @param key {string} The key of the message. * @param count {number} The number of elements. * @param replacements {object} The replacements to be done in the message. + * @param locale {string} The locale to use, if not passed use the default locale. * * @return {string} The translation message according to an integer value. */ - Lang.prototype.transChoice = function(key, count, replacements) { - return this.choice(key, count, replacements); + Lang.prototype.transChoice = function(key, count, replacements, locale) { + return this.choice(key, count, replacements, locale); }; /** @@ -458,8 +459,9 @@ * @param {Number} count * @return {Number} */ - Lang.prototype._getPluralForm = function(count) { - switch (this.locale) { + Lang.prototype._getPluralForm = function(count, locale) { + locale = locale || this.getLocale(); + switch (locale) { case 'az': case 'bo': case 'dz':