Browse files

added {{variables}} and tests

  • Loading branch information...
1 parent 0e5d850 commit 7ae126fe9d4893c63cb7ee2c964ba8720f84e4aa Guillaume Denis committed Sep 14, 2012
Showing with 75 additions and 27 deletions.
  1. +37 −10 lib/node-i18n.js
  2. +1 −0 test/fixtures/en.json
  3. +1 −0 test/fixtures/fr.json
  4. +36 −17 test/greetings-test.js
View
47 lib/node-i18n.js
@@ -4,11 +4,24 @@ _ = require('underscore')._
module.exports = function (options) {
var options = _.extend({
- 'lang': 'fr'
+ 'locale': 'fr'
, 'dir' : './assets/private/i18n'
}, options)
options.dir = process.cwd() + '/' + options.dir
+
+ var findLabels = function(word) {
+ var labels = []
+ var finder = /({{.*?}})/
+ while (match = finder.exec(word)) {
+ var l = match[1];
+ l = l.replace(/{{/, '').replace(/}}/, '')
+ labels.push(l)
+ // remove caught variable
+ word = word.replace(/{{.*?}}/, '')
+ }
+ return labels
+ }
var i18n = function(){
@@ -24,20 +37,25 @@ module.exports = function (options) {
return this
}
- this.setLocale = function(lang){
- this.options.lang = lang
+ this.setLocale = function(locale){
+ this.options.locale = locale
return this
}
- this.t = function(key, lang){
-
- word = ''
+ this.t = function(key, variables, locale){
+
+ var variables = variables
+ var locale = locale
+ if(typeof(variables) === 'string') {
+ locale = variables
+ variables = {}
+ }
+
+ word = this.words[locale ? locale : this.options.locale]
- if(!this.words[lang ? lang : this.options.lang]){
- if(!this.words['en']) return '' //no lang found
+ if(!word){
+ if(!this.words['en']) return '' //no locale found
else word = this.words['en']
- }else{
- word = this.words[lang ? lang : this.options.lang]
}
keys = key.split(".")
@@ -46,6 +64,15 @@ module.exports = function (options) {
if(tmp) word = tmp
else return '' //no text found
}
+
+ /* Replace {{labels}} with variables */
+ if(!_.isEmpty(variables)) {
+ labels = findLabels(word)
+ _.each(labels, function(l) {
+ var finder = new RegExp("{{" + l + "}}")
+ word = word.replace(finder, variables[l])
+ })
+ }
return word
View
1 test/fixtures/en.json
@@ -1,6 +1,7 @@
{
"en": {
"greeting": "Hello",
+ "greetingTwoPeople": "Hello {{person1}} and {{person2}}",
"evening": {
"greeting": "Good evening"
}
View
1 test/fixtures/fr.json
@@ -1,6 +1,7 @@
{
"fr": {
"greeting": "Bonjour",
+ "greetingTwoPeople": "Bonjour {{person1}} et {{person2}}",
"evening": {
"greeting": "Bonsoir"
}
View
53 test/greetings-test.js
@@ -2,23 +2,42 @@ var vows = require('vows')
, assert = require('assert')
, i18n = require('../index')({ dir: './test/fixtures'}).load()
-vows.describe('Greetings').addBatch({
- 'when greeting someone': {
- topic: 'greeting',
- 'in English, we say Hello': function(topic) {
- assert.equal(i18n.setLocale('en').t(topic), 'Hello')
- },
- 'in French, we say Bonjour': function(topic) {
- assert.equal(i18n.setLocale('fr').t(topic), 'Bonjour')
- },
- 'in the evening': {
- topic: function(sayWhat) { return 'evening.' + sayWhat },
- 'in English, we say Good evening': function(topic) {
- assert.equal(i18n.setLocale('en').t(topic), 'Good evening')
+vows.describe('Greetings')
+ .addBatch({
+ 'when greeting someone': {
+ topic: 'greeting',
+ 'in English, we say Hello': function(topic) {
+ assert.equal(i18n.setLocale('en').t(topic), 'Hello') // classic API
+ assert.equal(i18n.setLocale('fr').t(topic, 'en'), 'Hello') // using a different API
},
- 'in French, we say Bonsoir': function(topic) {
- assert.equal(i18n.setLocale('fr').t(topic), 'Bonsoir')
+ 'in French, we say Bonjour': function(topic) {
+ assert.equal(i18n.setLocale('fr').t(topic), 'Bonjour')
+ },
+ 'in the evening': {
+ topic: function(sayWhat) { return 'evening.' + sayWhat },
+ 'in English, we say Good evening': function(topic) {
+ assert.equal(i18n.setLocale('en').t(topic), 'Good evening')
+ },
+ 'in French, we say Bonsoir': function(topic) {
+ assert.equal(i18n.setLocale('fr').t(topic), 'Bonsoir')
+ }
+ }
+ }
+ })
+ .addBatch({
+ 'when greeting Billy the kid and Calamity Jane': {
+ topic: function() {
+ this.callback('greetingTwoPeople', {
+ person1: 'Billy the kid',
+ person2: 'Calamity Jane'
+ })
+ },
+ 'in English, we say Hello Billy the kid and Calamity Jane': function(sayWhat, toWho) {
+ assert.equal(i18n.setLocale('en').t(sayWhat, toWho), 'Hello Billy the kid and Calamity Jane')
+ },
+ 'in French, we say Bonjour Billy the kid et Calamity Jane': function(sayWhat, toWho) {
+ assert.equal(i18n.setLocale('fr').t(sayWhat, toWho), 'Bonjour Billy the kid et Calamity Jane')
}
}
- }
-}).export(module)
+ })
+ .export(module)

0 comments on commit 7ae126f

Please sign in to comment.