Skip to content

Commit

Permalink
test for localization
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Jan 17, 2016
1 parent 79eae7e commit 4cd3ed6
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
5 changes: 3 additions & 2 deletions actions/randomNumber.js
Expand Up @@ -4,10 +4,11 @@ exports.randomNumber = {
outputExample: {
randomNumber: 0.123
},

run: function(api, data, next){
data.response.randomNumber = Math.random();
data.response.stringRandomNumber = data.connection.localize(['Your random number is %s', Math.random()]);
next(null);
}

};
};
3 changes: 3 additions & 0 deletions config/api.js
Expand Up @@ -64,6 +64,9 @@ exports.test = {
'defaultRoom': {},
'otherRoom': {},
},
paths: {
'locale': [ '/tmp/locale' ]
}
};
}
};
Expand Down
102 changes: 102 additions & 0 deletions test/core/i18n.js
@@ -0,0 +1,102 @@
var should = require('should');
var fs = require('fs');
var actionheroPrototype = require(__dirname + '/../../actionhero.js').actionheroPrototype;
var actionhero = new actionheroPrototype();
var api;

var readLocaleFile = function(locale){
if(!locale){ locale = api.config.i18n.defaultLocale; }
var file = api.config.general.paths.locale[0] + '/' + locale + '.json';
var contents = String( fs.readFileSync(file) );
var json = JSON.parse(contents);
return json;
}

describe('Core: i18n', function(){
before(function(done){

var spanish = {
'Your random number is %s': 'Su número aleatorio es %s',
'That file is not found (%s)': 'Ese archivo no se encuentra (%s)',
'%s is a required parameter for this action': '%s es un parámetro requerido para esta acción',
};
fs.writeFileSync('/tmp/locale/es.json', JSON.stringify(spanish));

actionhero.start(function(err, a){
api = a;
var options = api.config.i18n;
options.directory = api.config.general.paths.locale[0];
options.locales = ['en', 'es'];
api.i18n.i18n.configure(options);
done();
});
});

after(function(done){
// api.utils.deleteDirectorySync( api.config.general.paths.locale[0] );
actionhero.stop(function(){
done();
});
});

it('should create localization files by default, and strings from actions and the server should be included automatically', function(done){
api.specHelper.runAction('randomNumber', function(response){
response.randomNumber.should.be.within(0,1);
var content = readLocaleFile();
[
'*** starting actionhero ***',
'Loaded initializer: %s',
'*** Server Started ***',
'[ action @ %s ]',
'Your random number is %s',
].forEach(function(s){
should.exist( content[s] );
content[s].should.equal(s);
})
done();
});
});

// to test this we would need to temporarliy enable logging for the test ENV...
it('should should respect the content of the localization files for the server logs')

it('should should respect the content of the localization files for generic messages to connections', function(done){
api.i18n.determineConnectionLocale = function(){ return 'en'; }
api.specHelper.runAction('randomNumber', function(response){
response.stringRandomNumber.should.match(/Your random number is/);

api.i18n.determineConnectionLocale = function(){ return 'es'; }
api.specHelper.runAction('randomNumber', function(response){
response.stringRandomNumber.should.match(/Su número aleatorio es/);
done();
});
});
});

it('should should respect the content of the localization files for api errors to connections', function(done){
api.i18n.determineConnectionLocale = function(){ return 'en'; }
api.specHelper.runAction('cacheTest', function(response){
response.error.should.match(/key is a required parameter for this action/);

api.i18n.determineConnectionLocale = function(){ return 'es'; }
api.specHelper.runAction('cacheTest', function(response){
response.error.should.match(/key es un parámetro requerido para esta acción/);
done();
});
});
});

it('should should respect the content of the localization files for http errors to connections', function(done){
api.i18n.determineConnectionLocale = function(){ return 'en'; }
api.specHelper.getStaticFile('missing-file.html', function(data){
data.error.should.match(/That file is not found/);

api.i18n.determineConnectionLocale = function(){ return 'es'; }
api.specHelper.getStaticFile('missing-file.html', function(data){
data.error.should.match(/Ese archivo no se encuentra/);
done();
});
});
})

});

0 comments on commit 4cd3ed6

Please sign in to comment.