Skip to content
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
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function t(str, obj){
obj = obj || {};
if (t[lang]) str = t[lang][str] || str;
return str.replace(/\{([^}]+)\}/g, function(_, name){
return obj[name] || _;
return get(name, obj) || _;
});
}

Expand All @@ -40,4 +40,17 @@ function t(str, obj){
exports.lang = function(code){
if (0 == arguments.length) return lang;
lang = code;
};
};

/**
* Get Object's path value
*
* @param {String} path
* @param {Object} obj
* @return {Mixed}
* @api private
*/

function get (path, obj) {
return new Function('_', 'return _.' + path)(obj);
}
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('t(str)', function(){
t('Hello {name}').should.equal('Hello {name}');
t('{name} is {age} year(s) old', { name: 'Tobi', age: 2 })
.should.equal('Tobi is 2 year(s) old');
t('{name.first} is from {name.last}\'s family.', { name: { first: 'Tobi', last: 'Goldman' } })
.should.equal('Tobi is from Goldman\'s family.');
})
})

Expand Down