Skip to content

Commit

Permalink
Add support for custom pluralize function.
Browse files Browse the repository at this point in the history
  • Loading branch information
maranomynet committed Jun 11, 2015
1 parent 64288f9 commit 0c118c1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var messages = {

var options = {
debug: true, //[Boolean]: Logs missing translations to console. Defaults to false.
pluralize: function(n,translKey){ return n; } //[Function(count,translationKey)]: Provides a custom pluralization mapping function.
};

var t = translate(messages, [options]);
Expand Down Expand Up @@ -105,3 +106,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'
```
17 changes: 10 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
* translationKey: 'value123'
* }
* }
*
*
* var options = {
* debug: true, //[Boolean]: Logs missing translations to console. Defaults to false.
* namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys. Defaults to '::'.
* 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'})
Expand Down Expand Up @@ -56,7 +57,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];

Expand All @@ -73,9 +74,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 {
Expand Down
39 changes: 39 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}',
Expand Down Expand Up @@ -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}',
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 0c118c1

Please sign in to comment.