Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node-i18n v0.2 #1

Merged
merged 8 commits into from Apr 27, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/templating.js
Original file line number Diff line number Diff line change
@@ -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
74 changes: 43 additions & 31 deletions lib/i18n.js
Original file line number Diff line number Diff line change
@@ -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;
};
25 changes: 18 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
{
"name": "node-i18n",
"version": "0.2",
"description": "Minimalistic i18n module with templating",
"version": "0.1",
"author": "Dan Hansen <[email protected]>",
"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": "[email protected]"
},
"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"
}
}
4 changes: 2 additions & 2 deletions test/test-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand All @@ -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();
Expand Down