Skip to content
Browse files

Merged pull request #1 from kitgoncharov/v0.2.

node-i18n v0.2
  • Loading branch information...
2 parents 925860c + 91da188 commit 9fc693a6febe910c1c637e44a8689ba37000ac3b @OhaiBBQ committed
Showing with 68 additions and 45 deletions.
  1. +5 −5 examples/templating.js
  2. +43 −31 lib/i18n.js
  3. +18 −7 package.json
  4. +2 −2 test/test-i18n.js
View
10 examples/templating.js
@@ -1,9 +1,9 @@
var i18n = require("./../index").i18n,
instance = new i18n(__dirname + "/locales");
console.log(instance);
-instance.load("en");
+instance.setLocale("en");
-console.log(instance.t("sup") === "hi"); // true
-console.log(instance.t("object.what.is") === "new"); // true
-console.log(instance.t("object.value" === { hi: "hello"}), "testing hello"); // true
-console.log(instance.t("object.value") === "testing hello"); // false
+console.log(instance.interpolate("sup") === "hi"); // true
+console.log(instance.interpolate("object.what.is") === "new"); // true
+console.log(instance.interpolate("object.value", { hi: "hello"}) === "testing hello"); // true
+console.log(instance.interpolate("object.value") === "testing hello"); // false
View
74 lib/i18n.js
@@ -1,45 +1,57 @@
-var _ = require("underscore");
+// Convenience aliases and the `I18n` function.
+var path = require('path'), I18n = module.exports = function I18n(path, language, locale) {
+ return new I18n.prototype.initialize(path, language, locale);
+};
-var I18n = function(path, language, locale) {
- if(typeof arguments[0] === "object") {
- arguments = Array.prototype.slice.call(arguments[0]);
- }
+// The current version of `node-i18n`. Keep in sync with `package.json`.
+I18n.VERSION = '0.2';
- this.path = arguments[0];
- this.language = arguments[1];
- this.locale = arguments[2];
+// Default template settings; used in `I18n#interpolate`.
+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 `I18n` instance.
+I18n.prototype.initialize = function initialize(path, language, locale) {
+ this.path = path;
+ this.language = language;
+ this.locale = locale;
+ return this;
};
-I18n.prototype.setLocale = function(language) {
- this.language = (this.language !== language)
- ? language
- : language;
+I18n.prototype.initialize.prototype = I18n.prototype;
- 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(".");
+// Interpolates a template using the provided locale.
+I18n.prototype.interpolate = function interpolate(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") {
- return _.template(ret, context);
+ if (typeof context == 'object') {
+ settings = I18n.templateSettings;
+ // JavaScript micro-templating implementation; taken from Underscore.
+ 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) {
+ 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('');"
+ )(context);
}
- return ret;
-};
-
-module.exports = I18n;
+ return result;
+};
View
25 package.json
@@ -1,12 +1,23 @@
{
"name": "node-i18n",
+ "version": "0.2",
"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"],
+ "homepage": "http://github.com/OhaiBBQ/node-i18n",
"main": "index",
- "engines": { "node": ">= 0.2.0" }
+ "keywords": ["i18n", "internationalization", "internationalisation", "translation"],
+ "author": {
+ "name": "Dan Hansen",
+ "email": "mokker1234@gmail.com"
+ },
+ "maintainers": [{
+ "name": "Kit Goncharov",
+ "web": "http://kitgoncharov.github.com"
+ }],
+ "bugs": {
+ "url": "http://github.com/OhaiBBQ/node-i18n/issues"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/OhaiBBQ/node-i18n.git"
+ }
}
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 9fc693a

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