Skip to content

Commit

Permalink
Merge branch 'release/v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
musterknabe committed Jun 16, 2015
2 parents d22016a + 4aabe07 commit 8db99ef
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 29 deletions.
1 change: 0 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),


copy: {
main: {
expand: true,
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
translate.js (v1.0.0)
translate.js (v1.1.0)
=====================

Javascript micro library for translations (i18n) with support for placeholders and multiple plural forms.
Expand All @@ -14,8 +14,9 @@ var messages = {
}

var options = {
debug: true, //[Boolean]: Logs missing translations to console and adds @@-markers around output. Defaults to false.
namespaceSplitter: '.' //[String|RegExp]: You can customize the part which splits namespace and translationKeys. Defaults to '::'.
// These are the defaults:
debug: false, //[Boolean]: Logs missing translations to console and adds @@-markers around output.
namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys.
}

var t = libTranslate.getTranslationFunction(messages, [options])
Expand All @@ -38,13 +39,14 @@ First create a language specific object for your translations:
var messages = {
like: 'I like this.',
likeThing: 'I like {thing}!',
simpleCounter: 'The count is {n}.',
simpleCounter: 'The count is {n}.', //{n} is automatically replace with count
hits: {
0: 'No Hits',
1: '{n} Hit',
2: '{n} Hitse', //some slavic langs have multiple plural forms
3: '{n} Hitses', //some slavic langs have multiple plural forms
n: '{n} Hits'
2: '{n} Hitse', //some slavic languages have multiple plural forms
3: '{n} Hitses', //some slavic languages have multiple plural forms
n: '{n} Hits', //use 'n' as key to match everything not matched more explicitly
gt99: '99+ Hits' //greater than support (gtX where X is an integer)
},
date: {
1: '{day}. January {year}',
Expand All @@ -61,7 +63,7 @@ var messages = {
12: '{day}. December {year}'
},

'Prosa Key': 'This is prosa!',
'Prose Key': 'This is prose!',

namespaceA: {
like: 'I like this namespace.',
Expand All @@ -78,7 +80,7 @@ And use it like this:
```JavaScript
//simple
t('like') => 'I like this.'
t('Prosa Key') => 'This is prosa!'
t('Prose Key') => 'This is prose!'

//namespace support
t('namespaceA::like') => 'I like this namespace.'
Expand All @@ -92,6 +94,7 @@ t('hits', 0) => 'No Hits'
t('hits', 1) => '1 Hit'
t('hits', 3) => '3 Hitses'
t('hits', 99) => '99 Hits'
t('hits', 100) => '99+ Hits'

//combined count and placeholders
t('date', 2, {day: '13', year: 2014}) => '13. February 2014'
Expand Down
20 changes: 16 additions & 4 deletions build/translate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Microlib for translations with support for placeholders and multiple plural forms.
*
* v1.0.1
* v1.1.0
*
* Usage:
* var messages = {
Expand All @@ -12,8 +12,9 @@
* }
*
* var options = {
* debug: true, //[Boolean]: Logs missing translations to console and adds @@-markers around output. Defaults to false.
* namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys. Defaults to '::'.
* // These are the defaults:
* debug: false, //[Boolean]: Logs missing translations to console and adds @@-markers around output.
* namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys.
* }
*
* var t = libTranslate.getTranslationFunction(messages, [options])
Expand Down Expand Up @@ -63,13 +64,24 @@

function getPluralValue(translation, count) {
if (isObject(translation)) {
if(Object.keys(translation).length === 0) {
var keys = Object.keys(translation);
var upperCap;

if(keys.length === 0) {
debug && console.log('[Translation] No plural forms found.');
return null;
}

for(var i = 0; i < keys.length; i++) {
if(keys[i].indexOf('gt') === 0) {
upperCap = parseInt(keys[i].replace('gt', ''), 10);
}
}

if(translation[count]){
translation = translation[count];
} else if(count > upperCap) { //int > undefined returns false
translation = translation['gt' + upperCap];
} else if(translation.n) {
translation = translation.n;
} else {
Expand Down
4 changes: 2 additions & 2 deletions build/translate.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "translate.js",
"version": "1.0.1",
"version": "1.1.0",
"description": "Javascript micro library for translations (i18n) with support for placeholders and multiple plural forms.",
"main": "Gruntfile.js",
"dependencies": {
Expand Down
33 changes: 25 additions & 8 deletions spec/defaultSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe("translate.js", function() {
1: '{n} Hit',
2: '{n} Hitse', //some slavic langs have multiple plural forms
3: '{n} Hitses', //some slavic langs have multiple plural forms
n: '{n} Hits'
n: '{n} Hits',
gt99: '99+ Hits' //greater than upper boundaries
},
date: {
1: '{day}. January {year}',
Expand All @@ -22,10 +23,11 @@ describe("translate.js", function() {
9: '{day}. September {year}',
10: '{day}. October {year}',
11: '{day}. November {year}',
12: '{day}. December {year}'
12: '{day}. December {year}',
gt12: 'The year {year} didn\'t end as expected' //greater than upper boundaries
},

'Prosa Key': 'This is prosa!',
'Prose Key': 'This is prose!',

namespaceA: {
plain: 'I like this.',
Expand Down Expand Up @@ -53,7 +55,7 @@ describe("translate.js", function() {
12: '{day}. December {year}'
},

'Prosa Key': 'This is prosa!'
'Prose Key': 'This is prose!'
}
}

Expand All @@ -67,8 +69,8 @@ describe("translate.js", function() {
expect(t('plain')).toEqual('I like this.');
});

it("should return a translated string for prosa keys", function() {
expect(t('Prosa Key')).toEqual('This is prosa!');
it("should return a translated string for prose keys", function() {
expect(t('Prose Key')).toEqual('This is prose!');
});

it("should return a translated string and replace a placeholder ", function() {
Expand Down Expand Up @@ -103,6 +105,14 @@ describe("translate.js", function() {
expect(t('hits', 4)).toEqual('4 Hits');
});

it("should return a translated string with the correct plural form respecting grater than syntax (100)", function() {
expect(t('hits', 100)).toEqual('99+ Hits');
});

it("should return a translated string with the correct plural form respecting grater than syntax (100)", function() {
expect(t('hits', 100)).toEqual('99+ Hits');
});

it("should return a translated string with the correct plural form and replaced placeholders: t(key, replacements, count)", function() {
expect(t('date', {day: '13', year: 2014}, 2)).toEqual('13. February 2014');
});
Expand All @@ -111,14 +121,17 @@ describe("translate.js", function() {
expect(t('date', 2, {day: '13', year: 2014})).toEqual('13. February 2014');
});

it("should return a translated string with the correct plural form respecting grater than syntax and replaced placeholders: t(key, replacements, count)", function() {
expect(t('date', {year: 2014}, 13)).toEqual('The year 2014 didn\'t end as expected');
});

//every thing with namespace support
it("should return a translated string [namespace support]", function() {
expect(t('namespaceA::plain')).toEqual('I like this.');
});

it("should return a translated string for prosa keys [namespace support]", function() {
expect(t('namespaceA::Prosa Key')).toEqual('This is prosa!');
it("should return a translated string for prose keys [namespace support]", function() {
expect(t('namespaceA::Prose Key')).toEqual('This is prose!');
});

it("should return a translated string and replace a placeholder [namespace support]", function() {
Expand Down Expand Up @@ -153,6 +166,10 @@ describe("translate.js", function() {
expect(t('namespaceA::hits', 4)).toEqual('4 Hits');
});

it("should return a translated string with the correct plural form respecting grater than syntax (100) [namespace support]", function() {
expect(t('hits', 100)).toEqual('99+ Hits');
});

it("should return a translated string with the correct plural form and replaced placeholders: t(key, replacements, count) [namespace support]", function() {
expect(t('namespaceA::date', {day: '13', year: 2014}, 2)).toEqual('13. February 2014');
});
Expand Down
20 changes: 16 additions & 4 deletions src/translate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Microlib for translations with support for placeholders and multiple plural forms.
*
* v1.0.1
* v1.1.0
*
* Usage:
* var messages = {
Expand All @@ -12,8 +12,9 @@
* }
*
* var options = {
* debug: true, //[Boolean]: Logs missing translations to console and adds @@-markers around output. Defaults to false.
* namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys. Defaults to '::'.
* // These are the defaults:
* debug: false, //[Boolean]: Logs missing translations to console and adds @@-markers around output.
* namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys.
* }
*
* var t = libTranslate.getTranslationFunction(messages, [options])
Expand Down Expand Up @@ -63,13 +64,24 @@

function getPluralValue(translation, count) {
if (isObject(translation)) {
if(Object.keys(translation).length === 0) {
var keys = Object.keys(translation);
var upperCap;

if(keys.length === 0) {
debug && console.log('[Translation] No plural forms found.');
return null;
}

for(var i = 0; i < keys.length; i++) {
if(keys[i].indexOf('gt') === 0) {
upperCap = parseInt(keys[i].replace('gt', ''), 10);
}
}

if(translation[count]){
translation = translation[count];
} else if(count > upperCap) { //int > undefined returns false
translation = translation['gt' + upperCap];
} else if(translation.n) {
translation = translation.n;
} else {
Expand Down

0 comments on commit 8db99ef

Please sign in to comment.