Permalink
Browse files

Clean up the source code.

  • Loading branch information...
1 parent 13bd746 commit a3ca708d3d14268d28563b8fcdadbb177aee2f15 Kit Goncharov committed Apr 27, 2011
Showing with 31 additions and 46 deletions.
  1. +31 −46 lib/i18n.js
View
@@ -1,64 +1,49 @@
-var I18n = function(path, language, locale) {
- if(typeof arguments[0] === "object") {
- arguments = Array.prototype.slice.call(arguments[0]);
- }
-
- this.path = arguments[0];
- this.language = arguments[1];
- this.locale = arguments[2];
+// 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;
};
+// Default template settings; used in `I18n#t`.
I18n.templateSettings = {
'evaluate': /<%([\s\S]+?)%>/g,
'interpolate': /<%=([\s\S]+?)%>/g
};
-I18n.factory = function(path, language, locale, instance) {
- return (typeof instance !== "undefined")
- ? instance
- : new I18n(arguments);
+// 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);
};
-I18n.prototype.setLocale = function(language) {
- this.language = (this.language !== language)
- ? language
- : language;
-
- this.locale = require(this.path + "/" + language).all;
+// Imports the specified locale.
+I18n.prototype.setLocale = function setLocale(language) {
+ if (language != this.language) {
+ this.language = language;
+ this.locale = require(path.join(this.path, language)).all;
+ }
+ return this;
};
-I18n.prototype.t = function(item, context) {
- var ret,
- _item = item.split("."),
- settings;
+// Interpolates a template using the provided locale.
+I18n.prototype.t = function t(item, context) {
+ var result, property = item.split('.'), section, settings;
- while(part = _item.shift()) {
- ret = (ret) ? ret[part] : this.locale[part];
- if(typeof ret === "undefined") {
- return "";
- }
+ while ((section = property.shift())) {
+ result = (result || this.locale)[section];
+ if (result == null) return '';
}
- if(typeof context === "object") {
+ if (typeof context == 'object') {
settings = I18n.templateSettings;
- return Function('obj', 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
- 'with(obj||{}){__p.push(\'' +
- ret.replace(/\\/g, '\\\\')
- .replace(/'/g, "\\'")
- .replace(settings.interpolate, function(match, code) {
+ return Function('obj', 'var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push(\'' +
+ result.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(settings.interpolate, function(match, code) {
return "'," + code.replace(/\\'/g, "'") + ",'";
- })
- .replace(settings.evaluate || null, function(match, code) {
+ }).replace(settings.evaluate || null, function(match, code) {
return "');" + code.replace(/\\'/g, "'").replace(/[\r\n\t]/g, ' ') + "__p.push('";
- })
- .replace(/\r/g, '\\r')
- .replace(/\n/g, '\\n')
- .replace(/\t/g, '\\t')
- + "');}return __p.join('');"
- )(ret, context);
+ }).replace(/\r/g, '\\r').replace(/\n/g, '\\n').replace(/\t/g, '\\t') +
+ "');}return __p.join('');"
+ )(result, context);
}
-
- return ret;
-};
-
-module.exports = I18n;
+ return result;
+};

0 comments on commit a3ca708

Please sign in to comment.