Skip to content

Commit

Permalink
fixed setting locale in request object
Browse files Browse the repository at this point in the history
  • Loading branch information
vonschau committed Nov 13, 2017
1 parent 5c969eb commit 82e9d38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ app.prepare().then(() => {
})
```

> RequestHandler automatically sets req.locale to locale of matched route so you can use it in your app.
Optionally you can pass a custom handler, for example:

```javascript
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Routes {
const {route, query, parsedUrl} = this.match(req.url)

if (route) {
req.locale = route.locale
if (customHandler) {
customHandler({req, res, route, query})
} else {
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ describe('Request handler', () => {
expect(app.render).toBeCalledWith(req, res, route.page, query)
})

test('find route and test locale is set correctly', () => {
const routes = nextRoutes()
const app = {getRequestHandler: jest.fn(), render: jest.fn()}
const req = {url: '/cs/test'}

routes.add('test', 'cs', '/test')
routes.getRequestHandler(app)(req, {})
expect(req.locale).toEqual('cs')
})

test('find route and call custom handler', () => {
const {routes, app, req, res} = setup('/en/a')
const {route, query} = routes.add('a', 'en').match('/en/a')
Expand Down

0 comments on commit 82e9d38

Please sign in to comment.