Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't detect locale from route when using no_prefix #1421

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/templates/plugin.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export default async (context) => {

const finalLocale =
(options.detectBrowserLanguage && doDetectBrowserLanguage(route)) ||
(!options.differentDomains && getLocaleFromRoute(route)) ||
(!options.differentDomains && options.strategy !== Constants.STRATEGIES.NO_PREFIX && getLocaleFromRoute(route)) ||
app.i18n.locale || app.i18n.defaultLocale || ''

if (options.skipSettingLocaleOnNavigate) {
Expand Down
4 changes: 2 additions & 2 deletions src/templates/utils-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function getLocaleDomain (locales, req) {
* @return {RegExp}
*/
export function getLocalesRegex (localeCodes) {
return new RegExp(`^/(${localeCodes.join('|')})(?:/|$)`, 'i')
return new RegExp(`^/(${localeCodes.join('|')})(?:/|$)`)
}

/**
Expand All @@ -130,7 +130,7 @@ export function getLocalesRegex (localeCodes) {
export function createLocaleFromRouteGetter (localeCodes, { routesNameSeparator, defaultLocaleRouteNameSuffix }) {
const localesPattern = `(${localeCodes.join('|')})`
const defaultSuffixPattern = `(?:${routesNameSeparator}${defaultLocaleRouteNameSuffix})?`
const regexpName = new RegExp(`${routesNameSeparator}${localesPattern}${defaultSuffixPattern}$`, 'i')
const regexpName = new RegExp(`${routesNameSeparator}${localesPattern}${defaultSuffixPattern}$`)
const regexpPath = getLocalesRegex(localeCodes)
/**
* Extract locale code from given route:
Expand Down
6 changes: 6 additions & 0 deletions test/fixture/no-lang-switcher/layouts/error.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<div>
<p>Page not found</p>
<p id="locale-properties">{{ JSON.stringify($i18n.localeProperties) }}</p>
</div>
</template>
28 changes: 27 additions & 1 deletion test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ describe('locales as string array', () => {
expect(dom.querySelector('#current-page')?.textContent).toBe('page: À propos')
})

test('detects locale from route when locale case does not match', async () => {
const html = await get('/FR/about')
const dom = getDom(html)
expect(dom.querySelector('#current-page')?.textContent).toBe('page: À propos')
})

test('dir attribute will not be added to the html element', async () => {
const html = await get('/about')
const dom = getDom(html)
Expand Down Expand Up @@ -1221,6 +1227,8 @@ describe('prefix_and_default strategy', () => {
describe('no_prefix strategy', () => {
/** @type {Nuxt} */
let nuxt
/** @type {NuxtConfig} */
let localConfig

beforeAll(async () => {
const override = {
Expand All @@ -1229,7 +1237,8 @@ describe('no_prefix strategy', () => {
}
}

nuxt = (await setup(loadConfig(__dirname, 'no-lang-switcher', override, { merge: true }))).nuxt
localConfig = loadConfig(__dirname, 'no-lang-switcher', override, { merge: true })
nuxt = (await setup(localConfig)).nuxt
})

afterAll(async () => {
Expand Down Expand Up @@ -1323,6 +1332,23 @@ describe('no_prefix strategy', () => {
const dom = getDom(html)
expect(dom.querySelector('#current-locale')?.textContent).toBe('locale: fr')
})

test('does not detect locale from route when locale case does not match', async () => {
/** @type {any} */
let resonseError
try {
await get('/FR/about')
} catch (error) {
resonseError = error
}
expect(resonseError).toBeDefined()
expect(resonseError.statusCode).toBe(404)
// Verify localeProperties is set to default locale.
const dom = getDom(resonseError.response.body)
const localeProperties = JSON.parse(dom.querySelector('#locale-properties')?.textContent || '{}')
const configLocales = /** @type {any[]} */(localConfig?.i18n?.locales)
expect(localeProperties).toMatchObject(configLocales.find(localeObject => localeObject.code === localConfig?.i18n?.defaultLocale))
})
})

describe('no_prefix strategy + differentDomains', () => {
Expand Down
18 changes: 4 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4585,20 +4585,10 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001219, caniuse-lite@^1.0.30001228:
version "1.0.30001239"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001239.tgz#66e8669985bb2cb84ccb10f68c25ce6dd3e4d2b8"
integrity sha512-cyBkXJDMeI4wthy8xJ2FvDU6+0dtcZSJW3voUF8+e9f1bBeuvyZfc3PNbkOETyhbR+dGCPzn9E7MA3iwzusOhQ==

caniuse-lite@^1.0.30001274:
version "1.0.30001274"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001274.tgz#26ca36204d15b17601ba6fc35dbdad950a647cc7"
integrity sha512-+Nkvv0fHyhISkiMIjnyjmf5YJcQ1IQHZN6U9TLUMroWR38FNwpsC51Gb68yueafX1V6ifOisInSgP9WJFS13ew==

caniuse-lite@^1.0.30001280:
version "1.0.30001283"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001283.tgz#8573685bdae4d733ef18f78d44ba0ca5fe9e896b"
integrity sha512-9RoKo841j1GQFSJz/nCXOj0sD7tHBtlowjYlrqIUS812x9/emfBLBt6IyMz1zIaYc/eRL8Cs6HPUVi2Hzq4sIg==
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001219, caniuse-lite@^1.0.30001228, caniuse-lite@^1.0.30001274, caniuse-lite@^1.0.30001280:
version "1.0.30001323"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001323.tgz"
integrity sha512-e4BF2RlCVELKx8+RmklSEIVub1TWrmdhvA5kEUueummz1XyySW0DVk+3x9HyhU9MuWTa2BhqLgEuEmUwASAdCA==

caseless@~0.12.0:
version "0.12.0"
Expand Down