diff --git a/README.md b/README.md index 6ad9948..74237d0 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ var options = { // These are the defaults: debug: false, //[Boolean]: Logs missing translations to console if true`. namespaceSplitter: '::', // [String|RegExp]: Customizes the translationKey namespace splitter. + pluralize: function(n,translKey){ return n; } //[Function(count,translationKey)]: Provides a custom pluralization mapping function. }; var t = translate(messages, [options]); @@ -107,3 +108,27 @@ t('hits', 99) => '99 Hits' t('date', 2, {day: '13', year: 2014}) => '13. February 2014' ``` + +### Custom pluralization + +You can also do customized pluralization like this: + +```js +var messages_IS = { + sheep: { + 0: 'Engar kindur', + 1: '{n} kind', + 2: '{n} kindur' + } +}; +var pluralize_IS = function ( n, tarnslationKey ) { + // Icelandic rules: Numbers ending in 1 are singular - unless ending in 11. + return n===0 ? 0 : (n%10 !== 1 || n%100 === 11) ? 2 : 1; +}; +var t = translate( messages_IS, { pluralize:pluralize_IS }); + +t('sheep', 0) => 'Engar kindur' +t('sheep', 1) => '1 kind' +t('sheep', 2) => '2 kindur' +t('sheep', 21) => '21 kind' +``` diff --git a/index.js b/index.js index 338c2db..7f696e1 100644 --- a/index.js +++ b/index.js @@ -10,15 +10,16 @@ * translationKey: 'value123' * } * } - * + * * var options = { * // These are the defaults: * debug: false, //[Boolean]: Logs missing translations to console if `true`. * namespaceSplitter: '::', //[String|RegExp]: Customizes the translationKey namespace splitter. + * pluralize: function(n,translKey){ return n; } //[Function(count,translationKey)]: Provides a custom pluralization mapping function. * } - * + * * var t = libTranslate.getTranslationFunction(messages, [options]) - * + * * t('translationKey') * t('translationKey', count) * t('translationKey', {replaceKey: 'replacevalue'}) @@ -57,7 +58,7 @@ module.exports = function(messageObject, options) { } //@todo make this more robust. maybe support more levels? - var components = translationKey.split(namespaceSplitter); + var components = translationKey.split(namespaceSplitter); var namespace = components[0]; var key = components[1]; @@ -74,9 +75,11 @@ module.exports = function(messageObject, options) { debug && console.log('[Translation] No plural forms found.'); return null; } - - if(translation[count]){ - translation = translation[count]; + var mappedCount = options.pluralize ? + options.pluralize( count, translation ): + count; + if(translation[mappedCount]){ + translation = translation[mappedCount]; } else if(translation.n) { translation = translation.n; } else { diff --git a/test.js b/test.js index 4a1ae24..440f11a 100644 --- a/test.js +++ b/test.js @@ -12,6 +12,11 @@ describe("translate.js", function() { 3: '{n} Hitses', //some slavic langs have multiple plural forms n: '{n} Hits' }, + icelandicSheep: { + 0: 'Engar kindur', + 1: '{n} kind', // some languages use singular for any number that ends with 1 (i.e. 101, 21, 31, 51) + 2: '{n} kindur' + }, date: { 1: '{day}. January {year}', 2: '{day}. February {year}', @@ -40,6 +45,11 @@ describe("translate.js", function() { 3: '{n} Hitses', //some slavic langs have multiple plural forms n: '{n} Hits' }, + icelandicSheep: { + 0: 'Engar kindur', + 1: '{n} kind', // some languages use singular for any number that ends with 1 (i.e. 101, 21, 31, 51) + 2: '{n} kindur' + }, date: { 1: '{day}. January {year}', 2: '{day}. February {year}', @@ -184,6 +194,35 @@ describe("translate.js", function() { expect(t3('namespaceA.date', 2, {day: '13', year: 2014})).to.equal('13. February 2014'); }); + // custom isPlural function + var pluralize_IS = function ( n, tarnslationKey ) { + // Icelandic rules: Numbers ending in 1 are singular - unless ending in 11. + return n===0 ? 0 : (n%10 !== 1 || n%100 === 11) ? 2 : 1; + }; + var t5 = translate(translationsObject, { pluralize: pluralize_IS }); + ['','namespaceA::'].forEach(function (ns) { + var nsTitle = ns ? ' [namespace support]' : ''; + + it('should pluralize (0) correctly in Icelandic'+nsTitle, function () { + expect(t5(ns+'icelandicSheep', 0)).to.equal('Engar kindur'); + }); + it('should pluralize (1) correctly in Icelandic'+nsTitle, function () { + expect(t5(ns+'icelandicSheep', 1)).to.equal('1 kind'); + }); + it('should pluralize (2) correctly in Icelandic'+nsTitle, function () { + expect(t5(ns+'icelandicSheep', 2)).to.equal('2 kindur'); + }); + it('should pluralize (11) correctly in Icelandic'+nsTitle, function () { + expect(t5(ns+'icelandicSheep', 11)).to.equal('11 kindur'); + }); + it('should pluralize (21) correctly in Icelandic'+nsTitle, function () { + expect(t5(ns+'icelandicSheep', 21)).to.equal('21 kind'); + }); + it('should pluralize (29) correctly in Icelandic'+nsTitle, function () { + expect(t5(ns+'icelandicSheep', 29)).to.equal('29 kindur'); + }); + }) + //wrong arguments var t4 = translate(translationsObject, 'asd'); it("should return a translated string with the correct plural form and replaced placeholders: t(key, count, replacements) [namespace support + wrong options arg]", function() {