diff --git a/index.js b/index.js index d4f5152..b341823 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,13 @@ var http = require('http'); +function defaultCallback(err) { + if (err) { + throw err; + } +} + function runCallback(err, callback) { - setTimeout(function() { callback(err, exports); }, 0); + setTimeout(function() { (callback || defaultCallback)(err, exports); }, 0); return typeof err == 'undefined'; } @@ -90,19 +96,12 @@ exports.setStore = function(store, config, callback) { this.store = store; } if (typeof config != 'undefined') { - this.store.configure(config, callback); - return true; + return this.store.configure(config, callback || defaultCallback); } } catch (e) { - if (typeof callback != 'undefined') { - callback(e, this); - return false; - } else { - throw e; - } + return (callback || defaultCallback)(e, this.store); } - callback(undefined, this); - return true; + return (callback || function(){return true;})(undefined, this); }; function replaceParams(string, replacements) { diff --git a/stores/module.js b/stores/module.js index 9868adf..335e729 100644 --- a/stores/module.js +++ b/stores/module.js @@ -88,7 +88,7 @@ exports.configure = function configure(options, callback) { modules[name] = options.paths[name]; } } - return callback(err, this); + return callback(undefined, this); }; exports.locales = function locales(prefix, catalogue, callback) { diff --git a/test/index.js b/test/index.js index 30dd41b..a529a76 100644 --- a/test/index.js +++ b/test/index.js @@ -1,23 +1,30 @@ -var i18n = require('..'); +var i18n = require(__dirname + '/..'); i18n.defaultLocale = 'fr'; i18n.debug(); -i18n.load("messages", function(err, locales) { +i18n.setStore('module', {paths: {__default__: __dirname + '/i18n-data'}}, function(err) { if (err) { throw err; } - console.log("Loaded locales", locales); - console.log(i18n.translate('x')); // x (en français) - console.log(i18n.translate('y')); // [T]y[/T] - i18n.debug(false); - console.log(i18n.translate('y')); // y - i18n.debug(true); - console.log(i18n.translate('x', 'en')); // x (in English) - for (var n=0; n<6; n++) { - console.log(i18n.plural('You have {n} messages', n, 'fr')); - } - console.log(i18n.translate('Hello, {name}', {name:'Jones', context:'male'})); // Bonjour, monsieur Jones - console.log(i18n.translate('Hello, {name}', {name:'Jones', context:'female'})); // Bonjour, mademoiselle Jones - console.log(i18n.translate('Hello, {name}', {name:'Jones', context:'unknown'})); // Bonjour, Jones + console.log("Configured store"); + i18n.load("messages", function(err, locales) { + if (err) { + throw err.ALL || err; + } + console.log("Loaded locales", locales); + console.log(i18n.translate('x')); // x (en français) + console.log(i18n.translate('y')); // [T]y[/T] + i18n.debug(false); + console.log(i18n.translate('y')); // y + i18n.debug(true); + console.log(i18n.translate('x', 'en')); // x (in English) + for (var n=0; n<6; n++) { + console.log(i18n.plural('You have {n} messages', n, 'fr')); + } + console.log(i18n.translate('Hello, {name}', {name:'Jones', context:'male'})); // Bonjour, monsieur Jones + console.log(i18n.translate('Hello, {name}', {name:'Jones', context:'female'})); // Bonjour, mademoiselle Jones + console.log(i18n.translate('Hello, {name}', {name:'Jones', context:'unknown'})); // Bonjour, Jones + }); }); +