Skip to content

Commit

Permalink
Merge branch 'canary' into alex-language-setup
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Jun 22, 2021
2 parents 37310d1 + fb5fb7f commit 3c105e3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,15 @@ export function getUtils({

let defaultLocale = i18n.defaultLocale
let detectedLocale = detectLocaleCookie(req, i18n.locales)
let acceptPreferredLocale =
i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale
let acceptPreferredLocale
try {
acceptPreferredLocale =
i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale
} catch (_) {
acceptPreferredLocale = detectedLocale
}

const { host } = req.headers || {}
// remove port from host and remove port if present
Expand Down
14 changes: 9 additions & 5 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,15 @@ export default class Server {

let defaultLocale = i18n.defaultLocale
let detectedLocale = detectLocaleCookie(req, i18n.locales)
let acceptPreferredLocale =
i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale

let acceptPreferredLocale
try {
acceptPreferredLocale =
i18n.localeDetection !== false
? accept.language(req.headers['accept-language'], i18n.locales)
: detectedLocale
} catch (_) {
acceptPreferredLocale = detectedLocale
}
const { host } = req?.headers || {}
// remove port from host if present
const hostname = host?.split(':')[0].toLowerCase()
Expand Down
22 changes: 22 additions & 0 deletions test/integration/i18n-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,28 @@ describe('i18n Support', () => {
expect($('#router-as-path').text()).toBe('/')
})

it('should ignore the invalid accept-language header', async () => {
nextConfig.replace('localeDetection: false', 'localeDetection: true')
const res = await fetchViaHTTP(
ctx.appPort,
'/',
{},
{
headers: {
'accept-language': 'ldfir;',
},
}
)

expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect($('html').attr('lang')).toBe('en-US')
expect($('#router-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
expect($('#router-pathname').text()).toBe('/')
expect($('#router-as-path').text()).toBe('/')
})

it('should set locale from detected path', async () => {
for (const locale of nonDomainLocales) {
const res = await fetchViaHTTP(
Expand Down

0 comments on commit 3c105e3

Please sign in to comment.