Skip to content

Commit

Permalink
- added plural form support
Browse files Browse the repository at this point in the history
- added more testing
- changed docs
  • Loading branch information
mashpie committed Mar 28, 2011
1 parent ee03569 commit 6f75fb9
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 13 deletions.
54 changes: 49 additions & 5 deletions README.md
Expand Up @@ -65,15 +65,59 @@ which puts
Hello
Howdy

### plural support

different plural froms are supported as response to `count`:

var singular = __n('%s cat', '%s cats', 1);
var plural = __n('%s cat', '%s cats', 3);

this puts **1 cat** or **3 cats**
and again these could get nested:

var singular = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, 'tree');
var plural = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, 'tree');

putting **There is one monkey in the tree** or **There are 3 monkeys in the tree**

### json file

the above will automatically generate a `en.js` by default inside `./locales/` which looks like

{
"Hello":"Hello",
"Hello %s, how are you today?":"Hello %s, how are you today?",
"weekend":"weekend",
"Hello %s, how are you today? How was your %s.":"Hello %s, how are you today? How was your %s."
"Hello": "Hello",
"Hello %s, how are you today?": "Hello %s, how are you today?",
"weekend": "weekend",
"Hello %s, how are you today? How was your %s.": "Hello %s, how are you today? How was your %s.",
"Hi": "Hi",
"Howdy": "Howdy",
"%s cat": {
"one": "%s cat",
"other": "%s cats"
},
"There is one monkey in the %%s": {
"one": "There is one monkey in the %%s",
"other": "There are %d monkeys in the %%s"
},
"tree": "tree"
}

that file can be edited or just uploaded to webtranslateit for any kind of collaborative translation workflow.
that file can be edited or just uploaded to [webtranslateit](http://docs.webtranslateit.com/file_formats/) for any kind of collaborative translation workflow:

{
"Hello": "Hallo",
"Hello %s, how are you today?": "Hallo %s, wie geht es dir heute?",
"weekend": "Wochenende",
"Hello %s, how are you today? How was your %s.": "Hallo %s, wie geht es dir heute? Wie war dein %s.",
"Hi": "Hi",
"Howdy": "Hallöchen",
"%s cat": {
"one": "%s Katze",
"other": "%s Katzen"
},
"There is one monkey in the %%s": {
"one": "Im %%s sitzt ein Affe",
"other": "Im Baum sitzen %d Affen"
},
"tree": "Baum"
}
37 changes: 32 additions & 5 deletions i18n.js
Expand Up @@ -17,7 +17,7 @@ var vsprintf = require('sprintf').vsprintf, // 0.1.1
// public export
var i18n = exports;

i18n.version = '0.0.2a';
i18n.version = '0.1.0';

i18n.__ = function() {
var msg = translate(arguments[0]);
Expand All @@ -27,6 +27,25 @@ i18n.__ = function() {
return msg;
};

i18n.__n = function() {
var singular = arguments[0];
var plural = arguments[1];
var count = arguments[2];
var msg = translate(singular, plural);

if(parseInt(count) > 1){
msg = vsprintf(msg.other, [count]);
}else{
msg = vsprintf(msg.one, [count]);
}

if (arguments.length > 3) {
msg = vsprintf(msg, Array.prototype.slice.call(arguments, 3));
}

return msg;
}

i18n.setLocale = function() {
locale = arguments[0];
return i18n.getLocale();
Expand All @@ -41,15 +60,23 @@ i18n.getLocale = function() {
// ===================

// read locale file, translate a msg and write to fs if new
function translate(msg) {
function translate(singular, plural) {
if (!locales[locale]) {
read(locale);
}
if (!locales[locale][msg]) {
locales[locale][msg] = msg;

if(plural){
if (!locales[locale][singular]) {
locales[locale][singular] = {'one':singular, 'other':plural};
write(locale);
}
}

if (!locales[locale][singular]) {
locales[locale][singular] = singular;
write(locale);
}
return locales[locale][msg];
return locales[locale][singular];
}

// try reading a file
Expand Down
11 changes: 10 additions & 1 deletion locales/de.js
Expand Up @@ -4,5 +4,14 @@
"weekend": "Wochenende",
"Hello %s, how are you today? How was your %s.": "Hallo %s, wie geht es dir heute? Wie war dein %s.",
"Hi": "Hi",
"Howdy": "Hallöchen"
"Howdy": "Hallöchen",
"%s cat": {
"one": "%s Katze",
"other": "%s Katzen"
},
"There is one monkey in the %%s": {
"one": "Im %%s sitzt ein Affe",
"other": "Im Baum sitzen %d Affen"
},
"tree": "Baum"
}
11 changes: 10 additions & 1 deletion locales/en.js
Expand Up @@ -4,5 +4,14 @@
"weekend": "weekend",
"Hello %s, how are you today? How was your %s.": "Hello %s, how are you today? How was your %s.",
"Hi": "Hi",
"Howdy": "Howdy"
"Howdy": "Howdy",
"%s cat": {
"one": "%s cat",
"other": "%s cats"
},
"There is one monkey in the %%s": {
"one": "There is one monkey in the %%s",
"other": "There are %d monkeys in the %%s"
},
"tree": "tree"
}
32 changes: 31 additions & 1 deletion test/i18n.test.js
Expand Up @@ -2,11 +2,12 @@

var i18n = require('../i18n'),
__ = i18n.__,
__n = i18n.__n,
assert = require('assert');

module.exports = {
'check version': function() {
assert.equal(i18n.version, '0.0.2a');
assert.equal(i18n.version, '0.1.0');
},

'check set/getLocale': function(){
Expand All @@ -28,6 +29,35 @@ module.exports = {

},

'check plural': function() {
i18n.setLocale('en');
var singular = __n('%s cat', '%s cats', 1);
var plural = __n('%s cat', '%s cats', 3);
assert.equal(singular, '1 cat');
assert.equal(plural, '3 cats');

i18n.setLocale('de');
var singular = __n('%s cat', '%s cats', 1);
var plural = __n('%s cat', '%s cats', 3);
assert.equal(singular, '1 Katze');
assert.equal(plural, '3 Katzen');
},

'check nested plural': function() {
i18n.setLocale('en');
var singular = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, __('tree'));
var plural = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, __('tree'));
assert.equal(singular, 'There is one monkey in the tree');
assert.equal(plural, 'There are 3 monkeys in the tree');

i18n.setLocale('de');
var singular = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, __('tree'));
var plural = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, __('tree'));
assert.equal(singular, 'Im Baum sitzt ein Affe');
assert.equal(plural, 'Im Baum sitzen 3 Affen');

},

'check variables': function() {
i18n.setLocale('en');
var greetings = ['Hi', 'Hello', 'Howdy'];
Expand Down

0 comments on commit 6f75fb9

Please sign in to comment.