Permalink
Browse files

add translation support

  • Loading branch information...
1 parent c75f9de commit 4e12b643528a7d6aa713fd818dc5583b8e6e765a @tj tj committed Sep 12, 2012
Showing with 31 additions and 2 deletions.
  1. +22 −2 index.js
  2. +9 −0 test/index.js
View
@@ -1,9 +1,15 @@
/**
+ * Current language.
+ */
+
+var lang = 'en';
+
+/**
* Expose `t`.
*/
-module.exports = t;
+exports = module.exports = t;
/**
* Translate the given `str` with substitions
@@ -17,7 +23,21 @@ module.exports = t;
function t(str, obj){
obj = obj || {};
+ if (t[lang]) str = t[lang][str] || str;
return str.replace(/\{([^}]+)\}/g, function(_, name){
return obj[name] || _;
});
-}
+}
+
+/**
+ * Get / set language `code`.
+ *
+ * @param {String} code
+ * @return {String}
+ * @api public
+ */
+
+exports.lang = function(code){
+ if (0 == arguments.length) return lang;
+ lang = code;
+};
View
@@ -12,4 +12,13 @@ describe('t(str)', function(){
t('{name} is {age} year(s) old', { name: 'Tobi', age: 2 })
.should.equal('Tobi is 2 year(s) old');
})
+})
+
+describe('t.lang()', function(){
+ it('should utilize language definitions', function(){
+ t.es = { 'Hello {name}': 'Hola {name}' };
+ t.lang('es');
+ t.lang().should.equal('es');
+ t('Hello {name}', { name: 'Tobi' }).should.equal('Hola Tobi');
+ })
})

0 comments on commit 4e12b64

Please sign in to comment.