Skip to content
Browse files

Add support for invoking the `I18n` function as a factory method. Rem…

…ove `I18n.factory`.
  • Loading branch information...
1 parent a3ca708 commit f838599bac9a6bd2535aea602b165ff9786cf47b Kit Goncharov committed
Showing with 11 additions and 8 deletions.
  1. +9 −6 lib/i18n.js
  2. +2 −2 test/test-i18n.js
View
15 lib/i18n.js
@@ -1,8 +1,6 @@
// Convenience aliases and the `I18n` function.
var path = require('path'), I18n = module.exports = function I18n(path, language, locale) {
- this.path = path;
- this.language = language;
- this.locale = locale;
+ return new I18n.prototype.initialize(path, language, locale);
};
// Default template settings; used in `I18n#t`.
@@ -11,11 +9,16 @@ I18n.templateSettings = {
'interpolate': /<%=([\s\S]+?)%>/g
};
-// Creates a new or returns an existing `I18n` instance.
-I18n.factory = function factory(path, language, locale, instance) {
- return instance == null ? instance : new I18n(path, language, locale);
+// Creates a new `I18n` instance.
+I18n.prototype.initialize = function initialize(path, language, locale) {
+ this.path = path;
+ this.language = language;
+ this.locale = locale;
+ return this;
};
+I18n.prototype.initialize.prototype = I18n.prototype;
+
// Imports the specified locale.
I18n.prototype.setLocale = function setLocale(language) {
if (language != this.language) {
View
4 test/test-i18n.js
@@ -4,7 +4,7 @@ exports["factory"] = {};
exports["factory"]["creates a new instance if one isnt passed"] = function(test) {
test.equal(
- i18n.factory("asdf", "asdf").path,
+ i18n("asdf", "asdf").path,
new i18n("asdf", "asdf").path
);
@@ -13,7 +13,7 @@ exports["factory"]["creates a new instance if one isnt passed"] = function(test)
exports["setLocale"] = {};
exports["setLocale"]["sets the locale to module.exports.all"] = function(test) {
- var instance = i18n.factory(__dirname + "/locales");
+ var instance = i18n(__dirname + "/locales");
instance.setLocale("en");
test.equal(instance.locale, require(__dirname + "/locales/en").all);
test.done();

0 comments on commit f838599

Please sign in to comment.
Something went wrong with that request. Please try again.