-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3889 from Josebaseba/0.12
[fixes #3833] Take locale into account in views.render()
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1><%= __('hello') %></h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"hello": "Hello" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"hello": "Hola" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"hello": "Kaixo" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
|
||
}); |