Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove Underscore as a dependency; add `I18n.templateSettings`.

  • Loading branch information...
commit 13bd7460bde0d95a74a7efed22142953cdb476be 1 parent 925860c
Kit Goncharov authored
Showing with 23 additions and 7 deletions.
  1. +23 −4 lib/i18n.js
  2. +0 −3  package.json
View
27 lib/i18n.js
@@ -1,5 +1,3 @@
-var _ = require("underscore");
-
var I18n = function(path, language, locale) {
if(typeof arguments[0] === "object") {
arguments = Array.prototype.slice.call(arguments[0]);
@@ -10,6 +8,11 @@ var I18n = function(path, language, locale) {
this.locale = arguments[2];
};
+I18n.templateSettings = {
+ 'evaluate': /<%([\s\S]+?)%>/g,
+ 'interpolate': /<%=([\s\S]+?)%>/g
+};
+
I18n.factory = function(path, language, locale, instance) {
return (typeof instance !== "undefined")
? instance
@@ -26,7 +29,8 @@ I18n.prototype.setLocale = function(language) {
I18n.prototype.t = function(item, context) {
var ret,
- _item = item.split(".");
+ _item = item.split("."),
+ settings;
while(part = _item.shift()) {
ret = (ret) ? ret[part] : this.locale[part];
@@ -36,7 +40,22 @@ I18n.prototype.t = function(item, context) {
}
if(typeof context === "object") {
- return _.template(ret, context);
+ 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 "'," + code.replace(/\\'/g, "'") + ",'";
+ })
+ .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);
}
return ret;
View
3  package.json
@@ -3,9 +3,6 @@
"description": "Minimalistic i18n module with templating",
"version": "0.1",
"author": "Dan Hansen <mokker1234@gmail.com>",
- "dependencies": {
- "underscore": ">= 1.1.4"
- },
"keywords": ["i18n", "internationalization", "internationalisation", "translation"],
"main": "index",
"engines": { "node": ">= 0.2.0" }
Please sign in to comment.
Something went wrong with that request. Please try again.