Skip to content

Commit

Permalink
feat(): detect language from browser
Browse files Browse the repository at this point in the history
  • Loading branch information
FbPalmabit committed Sep 27, 2018
1 parent 032799e commit 89afd48
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import pick from 'lodash.pick'
import NextLink from 'next/link'
import NextRouter from 'next/router'
import Route from './Route'
import { generateRouteFromObjectName, redirectToLocalizedHome } from './helpers/routeHelper'
import { generateRouteFromObjectName, redirectToLocalizedHome, detectLocale } from './helpers/routeHelper'
import MiddlewareManager from './middleware/MiddlewareManager'

export default class Routes {
Expand Down Expand Up @@ -127,7 +127,7 @@ export default class Routes {
req.nextRoute = route
req.siteUrl = this.siteUrl
req.getMultilanguageUrls = () => this.getMultilanguageUrls(route, query)

const middleware = MiddlewareManager(route.middlewares, { req, res, route, query })
middleware((err, data) => {
if (err) {
Expand All @@ -145,8 +145,10 @@ export default class Routes {
return
}


if (req.url === '/' && this.forceLocale) {
redirectToLocalizedHome(res, this.locale)
const detectedLocale = detectLocale({ req, routes: this.routes, defaultLocale: this.locale })
redirectToLocalizedHome(res, detectedLocale)
return
}

Expand Down
7 changes: 7 additions & 0 deletions src/helpers/routeHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ export const redirectToLocalizedHome = (res, locale) => {
res.writeHead(301, { 'Location': `/${locale}` })
res.end()
}
}

export const detectLocale = ({ req, routes, defaultLocale }) => {
const acceptsLangs = typeof req.acceptsLanguages === 'function' && req.acceptsLanguages()
const langs = !acceptsLangs ? [] : acceptsLangs.filter(lang => lang.length === 2)
const routesLangs = routes.map(({ locale }) => locale)
return routesLangs.indexOf(langs[0]) > -1 ? langs[0] : defaultLocale
}
24 changes: 23 additions & 1 deletion test/helpers/routeHelper.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateRouteFromObjectName } from './../../src/helpers/routeHelper'
import { generateRouteFromObjectName, detectLocale } from './../../src/helpers/routeHelper'

describe('generateRouteFromObject()', () => {
it('thrown err if name not exist', () => {
Expand All @@ -22,4 +22,26 @@ describe('generateRouteFromObject()', () => {
expect(result).toHaveProperty('update')
expect(result).not.toHaveProperty('foo')
})

it('return detected locale', () => {
const req = {
acceptsLanguages: () => { return ['it-IT', 'it', 'en', 'es'] }
}

const routes = [{ locale: 'en' }, { locale: 'it' }]
const result = detectLocale({ req, routes, defaultLocale: 'en' })

expect(result).toBe('it')
})

it('return default locale if detected is non supoorted', () => {
const req = {
acceptsLanguages: () => { return ['it-IT', 'es'] }
}

const routes = [{ locale: 'en' }, { locale: 'it' }]
const result = detectLocale({ req, routes, defaultLocale: 'en' })

expect(result).toBe('en')
})
})

0 comments on commit 89afd48

Please sign in to comment.