From 71ac5dd7db280ae07ac08ed8d6d6e4e58e0ab39a Mon Sep 17 00:00:00 2001 From: Bobbie Goede Date: Thu, 14 Dec 2023 00:12:08 +0100 Subject: [PATCH 1/2] fix: layer inline module options not used in generation --- src/gen.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/gen.ts b/src/gen.ts index 01572c446..25a6dedaf 100644 --- a/src/gen.ts +++ b/src/gen.ts @@ -4,7 +4,7 @@ import createDebug from 'debug' import { EXECUTABLE_EXTENSIONS } from './constants' import { genImport, genDynamicImport } from 'knitwork' import { withQuery } from 'ufo' -import { getLocalePaths, toCode } from './utils' +import { getLayerI18n, getLocalePaths, toCode } from './utils' import type { Nuxt } from '@nuxt/schema' import type { PrerenderTarget } from './utils' @@ -32,14 +32,16 @@ const generateVueI18nConfiguration = (config: Required, i } function simplifyLocaleOptions(nuxt: Nuxt, options: NuxtI18nOptions) { - const hasObjectLocales = - nuxt.options._layers.some(layer => layer?.config?.i18n?.locales?.some(x => typeof x !== 'string')) || - options?.i18nModules?.some(module => module?.locales?.some(x => typeof x !== 'string')) + const isLocaleObjectsArray = (locales?: string[] | LocaleObject[]) => locales?.some(x => typeof x !== 'string') + + const hasLocaleObjects = + nuxt.options._layers.some(layer => isLocaleObjectsArray(getLayerI18n(layer)?.locales)) || + options?.i18nModules?.some(module => isLocaleObjectsArray(module?.locales)) const locales = (options.locales ?? []) as LocaleObject[] // eslint-disable-next-line @typescript-eslint/no-unused-vars return locales.map(({ meta, ...locale }) => { - if (!hasObjectLocales) { + if (!hasLocaleObjects) { return locale.code } From 903d86e0d0c3c3a23b1f990f4158378c316e45b9 Mon Sep 17 00:00:00 2001 From: Bobbie Goede Date: Thu, 14 Dec 2023 00:12:25 +0100 Subject: [PATCH 2/2] test: add regression test for #2617 --- specs/fixtures/issues/2617/app.vue | 5 ++ .../issues/2617/components/BasicUsage.vue | 79 +++++++++++++++++++ .../issues/2617/components/LangSwitcher.vue | 40 ++++++++++ .../issues/2617/lang/locale-file-en.json | 6 ++ specs/fixtures/issues/2617/nuxt.config.ts | 23 ++++++ specs/fixtures/issues/2617/package.json | 14 ++++ .../issues/2617/pages/category/[slug].vue | 3 + specs/fixtures/issues/2617/pages/index.vue | 65 +++++++++++++++ .../issues/2617/pages/user/profile.vue | 3 + specs/issues/2617.spec.ts | 23 ++++++ 10 files changed, 261 insertions(+) create mode 100644 specs/fixtures/issues/2617/app.vue create mode 100644 specs/fixtures/issues/2617/components/BasicUsage.vue create mode 100644 specs/fixtures/issues/2617/components/LangSwitcher.vue create mode 100644 specs/fixtures/issues/2617/lang/locale-file-en.json create mode 100644 specs/fixtures/issues/2617/nuxt.config.ts create mode 100644 specs/fixtures/issues/2617/package.json create mode 100644 specs/fixtures/issues/2617/pages/category/[slug].vue create mode 100644 specs/fixtures/issues/2617/pages/index.vue create mode 100644 specs/fixtures/issues/2617/pages/user/profile.vue create mode 100644 specs/issues/2617.spec.ts diff --git a/specs/fixtures/issues/2617/app.vue b/specs/fixtures/issues/2617/app.vue new file mode 100644 index 000000000..2b1be0907 --- /dev/null +++ b/specs/fixtures/issues/2617/app.vue @@ -0,0 +1,5 @@ + diff --git a/specs/fixtures/issues/2617/components/BasicUsage.vue b/specs/fixtures/issues/2617/components/BasicUsage.vue new file mode 100644 index 000000000..198dd6e11 --- /dev/null +++ b/specs/fixtures/issues/2617/components/BasicUsage.vue @@ -0,0 +1,79 @@ + + + diff --git a/specs/fixtures/issues/2617/components/LangSwitcher.vue b/specs/fixtures/issues/2617/components/LangSwitcher.vue new file mode 100644 index 000000000..3d0aa0276 --- /dev/null +++ b/specs/fixtures/issues/2617/components/LangSwitcher.vue @@ -0,0 +1,40 @@ + + + diff --git a/specs/fixtures/issues/2617/lang/locale-file-en.json b/specs/fixtures/issues/2617/lang/locale-file-en.json new file mode 100644 index 000000000..815a4ddee --- /dev/null +++ b/specs/fixtures/issues/2617/lang/locale-file-en.json @@ -0,0 +1,6 @@ +{ + "home": "Homepage", + "about": "About us", + "posts": "Posts", + "dynamic": "Dynamic" +} diff --git a/specs/fixtures/issues/2617/nuxt.config.ts b/specs/fixtures/issues/2617/nuxt.config.ts new file mode 100644 index 000000000..016c4a5bd --- /dev/null +++ b/specs/fixtures/issues/2617/nuxt.config.ts @@ -0,0 +1,23 @@ +// https://nuxt.com/docs/guide/directory-structure/nuxt.config +export default defineNuxtConfig({ + modules: [ + [ + '@nuxtjs/i18n', + { + debug: false, + lazy: false, + langDir: 'lang', + defaultLocale: 'en', + detectBrowserLanguage: false, + locales: [ + { + code: 'en', + iso: 'en-US', + file: 'locale-file-en.json' + } + ] + } + ] + ], + debug: false +}) diff --git a/specs/fixtures/issues/2617/package.json b/specs/fixtures/issues/2617/package.json new file mode 100644 index 000000000..c151d99b2 --- /dev/null +++ b/specs/fixtures/issues/2617/package.json @@ -0,0 +1,14 @@ +{ + "name": "nuxt3-test-basic-usage-layers", + "private": true, + "type": "module", + "scripts": { + "dev": "nuxi dev", + "build": "nuxt build", + "start": "node .output/server/index.mjs" + }, + "devDependencies": { + "@nuxtjs/i18n": "latest", + "nuxt": "latest" + } +} diff --git a/specs/fixtures/issues/2617/pages/category/[slug].vue b/specs/fixtures/issues/2617/pages/category/[slug].vue new file mode 100644 index 000000000..2f5611f7c --- /dev/null +++ b/specs/fixtures/issues/2617/pages/category/[slug].vue @@ -0,0 +1,3 @@ + diff --git a/specs/fixtures/issues/2617/pages/index.vue b/specs/fixtures/issues/2617/pages/index.vue new file mode 100644 index 000000000..f08f82f61 --- /dev/null +++ b/specs/fixtures/issues/2617/pages/index.vue @@ -0,0 +1,65 @@ + + + diff --git a/specs/fixtures/issues/2617/pages/user/profile.vue b/specs/fixtures/issues/2617/pages/user/profile.vue new file mode 100644 index 000000000..6c50b8ae3 --- /dev/null +++ b/specs/fixtures/issues/2617/pages/user/profile.vue @@ -0,0 +1,3 @@ + diff --git a/specs/issues/2617.spec.ts b/specs/issues/2617.spec.ts new file mode 100644 index 000000000..f2ca69b7a --- /dev/null +++ b/specs/issues/2617.spec.ts @@ -0,0 +1,23 @@ +import { test, expect, describe } from 'vitest' +import { fileURLToPath } from 'node:url' +import { setup } from '../utils' +import { renderPage } from '../helper' + +describe('#2617', async () => { + await setup({ + rootDir: fileURLToPath(new URL(`../fixtures/issues/2617`, import.meta.url)), + browser: true + }) + + test('should use inline locale objects', async () => { + const { consoleLogs } = await renderPage('/') + + expect( + consoleLogs.some( + log => + log.type === 'warning' && + log.text.includes('[vue-i18n-routing] Locale ISO code is required to generate alternate link') + ) + ).toBe(false) + }) +})