From 634690a9eb3a492da540185a21c711468f2c4a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Tue, 27 Aug 2019 15:51:32 +0200 Subject: [PATCH] fix: locale not set with differentDomains enabled Fixed by re-introducing setting of app.i18n.locale when differentDomains is enabled. Previous refactorings has disabled that code accidentally. Changed code to match previous behavior where, with differentDomains enabled, plugin sets locale while middleware does not. --- src/plugins/main.js | 3 +- src/templates/middleware.js | 10 ++--- test/__snapshots__/module.test.js.snap | 34 ++++++++++++++- test/fixture/basic/pages/index.vue | 1 + test/module.test.js | 57 ++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 8 deletions(-) diff --git a/src/plugins/main.js b/src/plugins/main.js index a74c31a3b..4bcf1b6e4 100644 --- a/src/plugins/main.js +++ b/src/plugins/main.js @@ -121,7 +121,7 @@ export default async (context) => { const loadAndSetLocale = async (newLocale, { initialSetup = false } = {}) => { // Abort if different domains option enabled - if (app.i18n.differentDomains) { + if (!initialSetup && app.i18n.differentDomains) { return } @@ -131,6 +131,7 @@ export default async (context) => { } const oldLocale = app.i18n.locale + if (!initialSetup) { app.i18n.beforeLanguageSwitch(oldLocale, newLocale) diff --git a/src/templates/middleware.js b/src/templates/middleware.js index 268ec55c6..00d71972c 100644 --- a/src/templates/middleware.js +++ b/src/templates/middleware.js @@ -68,15 +68,15 @@ middleware['i18n'] = async (context) => { if (redirectToLocale !== app.i18n.locale) { // We switch the locale before redirect to prevent loops await app.i18n.setLocale(redirectToLocale) - } - } else if (useCookie && !getLocaleCookie()) { - app.i18n.setLocaleCookie(redirectToLocale) } - - return + } else if (useCookie && !getLocaleCookie()) { + app.i18n.setLocaleCookie(redirectToLocale) } + + return } } + } await app.i18n.setLocale(routeLocale ? routeLocale : locale) } diff --git a/test/__snapshots__/module.test.js.snap b/test/__snapshots__/module.test.js.snap index 73f9f39b8..b5c5fac9c 100644 --- a/test/__snapshots__/module.test.js.snap +++ b/test/__snapshots__/module.test.js.snap @@ -9,7 +9,7 @@ exports[`basic / contains EN text, link to /fr/ & link /about-us 1`] = `
Français
Homepage - About us
+ About us
locale: en
" @@ -54,7 +54,7 @@ exports[`basic /fr contains FR text, link to / & link to /fr/a-propos 1`] = `
English
Accueil - À propos
+ À propos
locale: fr
" @@ -111,6 +111,36 @@ exports[`basic sets SEO metadata properly 1`] = ` " `; +exports[`differentDomains enabled matches domain's locale (en) 1`] = ` +" + + + + + +
Français
+ Homepage + About us
locale: en
+ + +" +`; + +exports[`differentDomains enabled matches domain's locale (fr) 1`] = ` +" + + + + + +
English
+ Accueil + À propos
locale: fr
+ + +" +`; + exports[`no_prefix strategy / contains EN text & link /about 1`] = ` " diff --git a/test/fixture/basic/pages/index.vue b/test/fixture/basic/pages/index.vue index 30d60ee59..edc8fb5d2 100644 --- a/test/fixture/basic/pages/index.vue +++ b/test/fixture/basic/pages/index.vue @@ -3,6 +3,7 @@ {{ $t('home') }} {{ $t('about') }} +
locale: {{ $i18n.locale }}
diff --git a/test/module.test.js b/test/module.test.js index 26689e070..1686355a5 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -300,3 +300,60 @@ describe('with router base', () => { expect(newRoute).toBe('/about-us') }) }) + +describe('differentDomains enabled', () => { + let nuxt + + beforeAll(async () => { + const override = { + i18n: { + differentDomains: true, + seo: false + } + } + + const localConfig = loadConfig(__dirname, 'basic', override, { merge: true }) + + // Override after merging options to avoid arrays being merged. + localConfig.i18n.locales = [ + { + code: 'en', + iso: 'en-US', + name: 'English', + domain: 'en.nuxt-app.localhost' + }, + { + code: 'fr', + iso: 'fr-FR', + name: 'Français', + domain: 'fr.nuxt-app.localhost' + } + ] + + nuxt = (await setup(localConfig)).nuxt + }) + + afterAll(async () => { + await nuxt.close() + }) + + test('matches domain\'s locale (en)', async () => { + const requestOptions = { + headers: { + Host: 'en.nuxt-app.localhost' + } + } + const html = await get('/', requestOptions) + expect(cleanUpScripts(html)).toMatchSnapshot() + }) + + test('matches domain\'s locale (fr)', async () => { + const requestOptions = { + headers: { + Host: 'fr.nuxt-app.localhost' + } + } + const html = await get('/', requestOptions) + expect(cleanUpScripts(html)).toMatchSnapshot() + }) +})