Skip to content

Commit

Permalink
Merge pull request #3889 from Josebaseba/0.12
Browse files Browse the repository at this point in the history
[fixes #3833] Take locale into account in views.render()
  • Loading branch information
mikermcneil authored Nov 16, 2016
2 parents 29af6cf + ae97587 commit d0fa3b6
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/hooks/views/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ module.exports = function (sails) {

// Initialize i18n if hook is enabled
if (sails.hooks.i18n) {

// If a locale was specified as an option, render the view with that locale
req.headers['accept-language'] = options.locale || sails.hooks.i18n.defaultLocale;

require('i18n').init(req, options, function() {

// Set the locale if necessary
Expand Down
1 change: 1 addition & 0 deletions test/hooks/views/ejs/index.i18n.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1><%= __('hello') %></h1>
3 changes: 3 additions & 0 deletions test/hooks/views/locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "Hello"
}
3 changes: 3 additions & 0 deletions test/hooks/views/locales/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "Hola"
}
3 changes: 3 additions & 0 deletions test/hooks/views/locales/eu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "Kaixo"
}
102 changes: 102 additions & 0 deletions test/hooks/views/res.render.i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Module dependencies
*/

var assert = require('assert');
var Sails = require('../../../lib').constructor;
var path = require('path');

describe('sails.hooks.views.render() with i18n', function (){

var renderWithLocale = function(req, res){
var data = {
locale: req.param('lang'),
layout: false
};
sails.hooks.views.render('index.i18n', data, function(err, html){
if(err) return res.send(500, err);
return res.send(200, html);
});
};

var renderWithoutLocale = function(req, res){
var data = {
layout: false
};
sails.hooks.views.render('index.i18n', data, function(err, html){
if(err) return res.send(500, err);
return res.send(200, html);
});
};

// Load a Sails app
var app;
before(function (done) {
app = new Sails()
.load({
globals: true,
loadHooks: [
'moduleloader',
'userconfig',
'http',
'logger',
'i18n',
'views'
],
routes: {
'get /render/:lang': renderWithLocale,
'get /render' : renderWithoutLocale
},
i18n: {
locales: ['en', 'es', 'eu'],
defaultLocale: 'eu',
directory: path.join(__dirname, './locales')
},
paths: {
views: path.join(__dirname, './ejs')
}
}, done);
});


it('should show the message in basque', function (done) {
app
.request({url:'/render/eu', method: 'get'}, function(err, res){
assert.equal('<h1>Kaixo</h1>\n', res.body);
done();
});
});

it('should show the message in spanish', function (done) {
app
.request({url:'/render/es', method: 'get'}, function(err, res){
assert.equal('<h1>Hola</h1>\n', res.body);
done();
});
});

it('should show the message in english', function (done) {
app
.request({url:'/render/en', method: 'get'}, function(err, res){
assert.equal('<h1>Hello</h1>\n', res.body);
done();
});
});

it('should show the message in basque by default if unknown lang', function (done) {
app
.request({url:'/render/de', method: 'get'}, function(err, res){
assert.equal('<h1>Kaixo</h1>\n', res.body);
done();
});
});

it('should show the message in basque by default', function (done) {
app
.request({url:'/render', method: 'get'}, function(err, res){
assert.equal('<h1>Kaixo</h1>\n', res.body);
done();
});
});

});

0 comments on commit d0fa3b6

Please sign in to comment.