Skip to content

Commit

Permalink
refined and documented 'register' option, closes #162
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Feb 15, 2016
1 parent d82f7c1 commit 20f7f64
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ i18n.configure({
logErrorFn: function (msg) {
console.log('error', msg);
}

// object or [obj1, obj2] to bind the i18n api and current locale to - defaults to null
register: global
});
```

Expand All @@ -197,6 +200,34 @@ res.cookie('yourcookiename', 'de', { maxAge: 900000, httpOnly: true });

After this and until the cookie expires, `i18n.init()` will get the value of the cookie to set that language instead of default for every page.

#### Some words on `register` option

Esp. when used in a cli like scriptyou won't use any `i18n.init()` to guess language settings from your user. Thus `i18n` won't bind itself to any `res` or `req` object and will work like a static module.

```js
var anyObject = {};

i18n.configure({
locales: ['en', 'de'],
register: anyObject
});

anyObject.setLocale('de');
anyObject.__('Hallo'); // --> Hallo`
```

Cli usage is a special use case, as we won't need to maintain any transaction / concurrency aware setting of locale, so you could even choose to bind `i18n` to _global_ scope of node:

```js
i18n.configure({
locales: ['en', 'de'],
register: global
});

i18n.setLocale('de');
__('Hello'); // --> Hallo`
```

### i18n.init()

When used as middleware in frameworks like express to setup the current environment for each loop. In contrast to configure the `i18n.init()` should be called within each request-response-cycle.
Expand Down
20 changes: 17 additions & 3 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,18 @@ i18n.version = '0.6.0';

i18n.configure = function i18nConfigure(opt) {

// you may register helpers in global scope, up to you
// you may register i18n in global scope, up to you
if (typeof opt.register === 'object') {
applyAPItoObject(opt.register);
register = opt.register;
// or give an array objects to register to
if (Array.isArray(opt.register)) {
register = opt.register;
register.forEach(function(r) {
applyAPItoObject(r);
});
}else{
applyAPItoObject(opt.register);
}
}

// sets a custom cookie name to parse locale settings from
Expand Down Expand Up @@ -294,7 +302,13 @@ i18n.setLocale = function i18nSetLocale(object, locale, skipImplicitObjects) {

// consider any extra registered objects
if (typeof register === 'object') {
register.locale = target_object.locale;
if (Array.isArray(register) && !skipImplicitObjects) {
register.forEach(function(r) {
r.locale = target_object.locale;
});
}else{
register.locale = target_object.locale;
}
}

// consider res
Expand Down
69 changes: 69 additions & 0 deletions test/i18n.configureRegister.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*jslint nomen: true, undef: true, sloppy: true, white: true, stupid: true, passfail: false, node: true, plusplus: true, indent: 2 */

var i18n = require('../i18n'),
should = require("should"),
fs = require('fs'),
path = require('path');

var i18nPath = 'i18n';
var i18nFilename = path.resolve(i18nPath + '.js');

function reconfigure(config) {
delete require.cache[i18nFilename];
i18n = require(i18nFilename);
i18n.configure(config);
}

describe('configure register', function() {

it('should work on a custom object', function() {
var customObject = {};
reconfigure({
locales: ['en', 'de'],
register: customObject
});
should.equal(customObject.__('Hello'), 'Hello');
customObject.setLocale('de');
should.equal(customObject.__('Hello'), 'Hallo');
});

it('should work on global', function() {
reconfigure({
locales: ['en', 'de'],
register: global
});
should.equal(__('Hello'), 'Hello');
i18n.setLocale('de');
should.equal(__('Hello'), 'Hallo');
});

it('should work on list of objects', function() {
var obj1 = {}, obj2 = {};
reconfigure({
locales: ['en', 'de', 'fr'],
register: [obj1, obj2]
});
should.equal(obj1.__('Hello'), 'Hello');
should.equal(obj2.__('Hello'), 'Hello');

// sets both
i18n.setLocale('fr');
should.equal(obj1.__('Hello'), 'Bonjour');
should.equal(obj2.__('Hello'), 'Bonjour');

// sets both too
obj1.setLocale('en');
should.equal(obj1.__('Hello'), 'Hello');
should.equal(obj2.__('Hello'), 'Hello');

// sets obj2 only
i18n.setLocale([obj2], 'de');
should.equal(obj1.__('Hello'), 'Hello');
should.equal(obj2.__('Hello'), 'Hallo');

// sets obj2 only
i18n.setLocale(obj2, 'fr', true);
should.equal(obj1.__('Hello'), 'Hello');
should.equal(obj2.__('Hello'), 'Bonjour');
});
});

0 comments on commit 20f7f64

Please sign in to comment.