From baab8be1b65886e823a8d3b9d9925497a0c64b2d Mon Sep 17 00:00:00 2001 From: Inesh Bose <2504266b@student.gla.ac.uk> Date: Mon, 13 Mar 2023 17:05:19 +0000 Subject: [PATCH 1/4] docs: mention callbacks --- docs/content/2.guide/2.runtime-hooks.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/content/2.guide/2.runtime-hooks.md b/docs/content/2.guide/2.runtime-hooks.md index 2bff2fe73..6309b6af5 100644 --- a/docs/content/2.guide/2.runtime-hooks.md +++ b/docs/content/2.guide/2.runtime-hooks.md @@ -2,6 +2,12 @@ Nuxt i18n module exposes some [runtime hooks](https://nuxt.com/docs/guide/going-further/hooks#app-hooks-runtime) as callbacks that you can use to perform specific tasks that depend on the app's language. +::alert{type="warning"} + +For v8.0.0-beta.10 and below, please refer to [callbacks](https://i18n.nuxtjs.org/callbacks/) and [configuration](https://i18n.nuxtjs.org/options-reference#onbeforelanguageswitch). + +:: + --- **Nuxt i18n module** exposes some callbacks that you can use to perform specific tasks that depend on the app's language. From e380f085fe6b3e48bbe6bb718229a82bc5cadc7a Mon Sep 17 00:00:00 2001 From: Inesh Bose <2504266b@student.gla.ac.uk> Date: Wed, 22 Mar 2023 00:44:46 +0000 Subject: [PATCH 2/4] chore: initial commit --- src/module.ts | 5 ++++- src/types.ts | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/module.ts b/src/module.ts index ff37bcd5a..82adca8c8 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,6 +1,7 @@ +import { existsSync } from 'fs' import createDebug from 'debug' import { isBoolean, isObject, isString } from '@intlify/shared' -import { defineNuxtModule, isNuxt2, isNuxt3, getNuxtVersion, addPlugin, addTemplate, addImports } from '@nuxt/kit' +import { defineNuxtModule, isNuxt2, isNuxt3, getNuxtVersion, addPlugin, addTemplate, addImports, resolvePath } from '@nuxt/kit' import { resolve, relative, isAbsolute } from 'pathe' import { setupAlias, resolveVueI18nAlias } from './alias' import { setupPages } from './pages' @@ -95,6 +96,8 @@ export default defineNuxtModule({ ? resolve(nuxt.options.rootDir, options.vueI18n) : { legacy: false } + const configPath = await resolvePath(options.vueI18n?.configFile || 'vue-i18n.options', { cwd: nuxt.options.rootDir, extensions: ['.ts', '.mjs', '.js'] }) + /** * extend messages via 3rd party nuxt modules */ diff --git a/src/types.ts b/src/types.ts index a18fbe4bc..80166de60 100644 --- a/src/types.ts +++ b/src/types.ts @@ -52,7 +52,9 @@ export type NuxtI18nOptions = { skipSettingLocaleOnNavigate?: boolean // sortRoutes?: boolean strategy?: Strategies - vueI18n?: I18nOptions | string + vueI18n?: { + configFile?: string + } types?: 'composition' | 'legacy' debug?: boolean dynamicRouteParams?: boolean From 56ad478a1aaae628f3ad1ea778c764f4aaeee599 Mon Sep 17 00:00:00 2001 From: Inesh Bose <2504266b@student.gla.ac.uk> Date: Thu, 23 Mar 2023 10:40:22 +0000 Subject: [PATCH 3/4] chore: progress --- .../1.getting-started/2.basic-usage.md | 45 +++++++---------- docs/content/2.guide/11.locale-fallback.md | 35 ++++++------- docs/content/2.guide/15.migrating.md | 10 ++++ .../2.guide/8.lazy-load-translations.md | 4 -- docs/content/3.options/1.vue-i18n.md | 49 ++++++++++++------- docs/content/4.API/1.composables.md | 14 ++++++ playground/i18n.config.ts | 18 +++++++ playground/nuxt.config.ts | 19 +------ src/gen.ts | 46 +++-------------- src/module.ts | 28 ++++++----- src/runtime/composables.ts | 13 ++++- src/types.ts | 2 + 12 files changed, 143 insertions(+), 140 deletions(-) create mode 100644 playground/i18n.config.ts diff --git a/docs/content/1.getting-started/2.basic-usage.md b/docs/content/1.getting-started/2.basic-usage.md index 568534631..f925e7caa 100644 --- a/docs/content/1.getting-started/2.basic-usage.md +++ b/docs/content/1.getting-started/2.basic-usage.md @@ -8,7 +8,7 @@ The basics to get started with the Nuxt i18n module is to translate with Vue I18 The basic to get started with **Nuxt i18n module** is to **translate with Vue I18n via the `vueI18n` option** -So, let's get started configuring the following `nuxt.config`: +So, let's get started by adding the module to `nuxt.config`: ```ts {}[nuxt.config.ts] export default defineNuxtConfig({ @@ -16,26 +16,29 @@ export default defineNuxtConfig({ '@nuxtjs/i18n' ], i18n: { - // add `vueI18n` option to `@nuxtjs/i18n` module options - vueI18n: { - legacy: false, - locale: 'en', - messages: { - en: { - welcome: 'Welcome' - }, - fr: { - welcome: 'Bienvenue' - } - } + vueI18n: { configPath: './i18n.config.ts' } // if you are using custom path + } +}) +``` + +```ts {}[i18n.config.ts] +export default defineI18nConfig({ + legacy: false, + locale: 'en', + messages: { + en: { + welcome: 'Welcome' + }, + fr: { + welcome: 'Bienvenue' } } }) ``` -The `vueI18n` option is the same as `createI18n` function option of Vue I18n. `vueI18n` option is passed to the `createI18n` function via the nuxt plugin of this module internally. +The `defineI18nConfig` function is the same as `createI18n` function option of Vue I18n. The configuration is passed to the `createI18n` function via the nuxt plugin (runtime) of this module internally. -For more details about the `vueI18n` option, see the [Vue I18n documentation](https://vue-i18n.intlify.dev/api/general.html#createi18n). +For more details about configuration, see the [Vue I18n documentation](https://vue-i18n.intlify.dev/api/general.html#createi18n). Now, put (or edit) the following the page component in `pages` directory of you project: @@ -103,18 +106,6 @@ For localizing links, you can use the code provided from the `locales` option as i18n: { + locales: ['en', 'fr'], // used in URL path prefix + defaultLocale: 'en', // default locale of your project for Nuxt pages and routings - vueI18n: { - legacy: false, - locale: 'en', - messages: { - en: { - welcome: 'Welcome' - }, - fr: { - welcome: 'Bienvenue' - } - } - } } }) ``` diff --git a/docs/content/2.guide/11.locale-fallback.md b/docs/content/2.guide/11.locale-fallback.md index 9e924b0e2..dd93ddf45 100644 --- a/docs/content/2.guide/11.locale-fallback.md +++ b/docs/content/2.guide/11.locale-fallback.md @@ -7,27 +7,20 @@ How a fallback gets selected when a translation is missing. **Nuxt i18n module** takes advantage of **Vue I18n** ability to handle localization fallback. It is possible to define a single fallback locale, an array of locales, or a decision map for more specific needs. -```js [nuxt.config.js] -export default defineNuxtConfig({ - // ... - - i18n: { - vueI18n: { - fallbackLocale: 'en', - // or - fallbackLocale: ['en', 'fr'], - // or - fallbackLocale: { - 'de-CH': ['fr', 'it'], - 'zh-Hant': ['zh-Hans'], - 'es-CL': ['es-AR'], - 'es': ['en-GB'], - 'pt': ['es-AR'], - 'default': ['en', 'da'] - } - } - }, - +```js [i18n.config.ts] +export default defineI18nConfig({ + fallbackLocale: 'en', + // or + fallbackLocale: ['en', 'fr'], + // or + fallbackLocale: { + 'de-CH': ['fr', 'it'], + 'zh-Hant': ['zh-Hans'], + 'es-CL': ['es-AR'], + 'es': ['en-GB'], + 'pt': ['es-AR'], + 'default': ['en', 'da'] + } // ... }) ``` diff --git a/docs/content/2.guide/15.migrating.md b/docs/content/2.guide/15.migrating.md index c7d3cc40a..a9b81a25d 100644 --- a/docs/content/2.guide/15.migrating.md +++ b/docs/content/2.guide/15.migrating.md @@ -4,6 +4,10 @@ Follow this guide to upgrade from one major version to the other. --- +::alert{type="warning"} +Here, deprecated = removed +:: + ## Upgrading from `nuxtjs/i18n` v7.x ### Change the route key rules in `pages` option @@ -177,3 +181,9 @@ The following APIs are no longer available: - `localeRoute()` - `localeLocation()` - `switchLocalePath()` + +### Deprecated `vueI18n` option to not accept `createI18n` options + +This is to ensure stability and distinction between compile / build-time and runtime since `vue-i18n` is used in runtime. + +You can continue defining `vue-i18n` options in `i18n.config.ts`. Refer to [Vue i18n](/options/vue-i18n) section and [basic usage](/getting-started/basic-usage/#translate-with-vue-i18n) for an example. diff --git a/docs/content/2.guide/8.lazy-load-translations.md b/docs/content/2.guide/8.lazy-load-translations.md index dd99b0513..d61421fbe 100755 --- a/docs/content/2.guide/8.lazy-load-translations.md +++ b/docs/content/2.guide/8.lazy-load-translations.md @@ -164,10 +164,6 @@ export default defineNuxtConfig({ lazy: true, langDir: 'lang', defaultLocale: 'en', - vueI18n: { - // If fallback is needed, you need to define - fallbackLocale: 'en', - } }, // ... diff --git a/docs/content/3.options/1.vue-i18n.md b/docs/content/3.options/1.vue-i18n.md index 8ff6d3089..4712b262e 100644 --- a/docs/content/3.options/1.vue-i18n.md +++ b/docs/content/3.options/1.vue-i18n.md @@ -6,26 +6,37 @@ Related Vue I18n options. ## `vueI18n` -- type: `object` or `string` -- default: `{}` - -Configuration for the `vue-i18n` library that is used internally by this module. See full documentation at https://vue-i18n.intlify.dev/api/general.html#createi18n - -::alert{type="info"} - -It's also supported to set this property to a path to a local configuration file. The file needs to export a function or plain object. If a function, it will be passed a Nuxt Context as a parameter. It's necessary to use that approach when overriding more complex types (like functions) that can't be stringified correctly. - -```ts {}[~/vue-i18n.options.ts] -import type { I18nOptions } from 'vue-i18n' -import type { NuxtApp } from 'nuxt/dist/app/index' +- type: `object` +- default: `undefined` + +Build-time configuration for the `vue-i18n` options that is used internally by this module. See full documentation at https://vue-i18n.intlify.dev/api/general.html#createi18n + +Configuration for `createI18n` can be passed using a configuration file. By default, the module will scan for a `i18n.config.{js,mjs,ts}` if nothing is specified. + +```ts {}[nuxt.config.ts] +export default defineNuxtConfig({ + modules: [ + '@nuxtjs/i18n' + ], + i18n: { + vueI18n: { configPath: './nuxt-i18n.js' } // custom path example + } +}) +``` -export default function (nuxt: NuxtApp) { - return { - modifiers: { - snakeCase: (str) => str.split(' ').join('-') +```ts {}[nuxt-i18n.js] +export default defineI18nConfig({ + legacy: false, + locale: 'en', + messages: { + en: { + welcome: 'Welcome' + }, + fr: { + welcome: 'Bienvenue' } - } as I18nOptions -} + } +}) ``` :: @@ -34,6 +45,6 @@ export default function (nuxt: NuxtApp) { The `messages` option cannot be included in an object and exported with function due to limitations in Vue I18n v9 handling of locale messages. -As the workaround, you can use [lazy-load transtions](/guide/lazy-load-translations) in Nuxt i18n module. locale messages handled with lazy-load transtions will be loaded as locale messges inside Vue i18n. +As the workaround, you can use [lazy-load transtions](/guide/lazy-load-translations) in Nuxt i18n module. locale messages handled with lazy-load transtions will be loaded as locale messages inside Vue i18n. :: diff --git a/docs/content/4.API/1.composables.md b/docs/content/4.API/1.composables.md index 19ed11708..8db56ec0c 100644 --- a/docs/content/4.API/1.composables.md +++ b/docs/content/4.API/1.composables.md @@ -220,3 +220,17 @@ A function that is the dynamic locale messages loading, that has the following p - when you switch the locale with `setLocale`. - when the locale is switched with ``. for example, the route path resolved by `useSwitchLocalePath` or `$switchLocalePath`. + +## `defineI18nConfig` + +The `defineI18nConfig` defines the configuration to be passed to the `vue-i18n` plugin in runtime. + +You can use at JavaScript and TypeScript extension formats. + +### Parameters + +#### `config` + +**Type**: `I18nOptions` + +The object with configuration definition. diff --git a/playground/i18n.config.ts b/playground/i18n.config.ts new file mode 100644 index 000000000..ffb9f7e4e --- /dev/null +++ b/playground/i18n.config.ts @@ -0,0 +1,18 @@ +// import { defineI18nConfig, type I18nOptions } from '@nuxtjs/i18n' + +// export default { +export default defineI18nConfig({ + legacy: false, + locale: 'en', + fallbackLocale: 'fr' + // messages: { + // ja: { + // hello: 'こんにちは!' + // } + // } + // fallbackLocale: { + // en: ['ja', 'fr', 'en-US'], + // ja: ['en', 'fr', 'ja-JP'], + // fr: ['en', 'ja', 'fr-FR'] + // } +}) diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index c46c7fd82..34774c5cd 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -77,7 +77,7 @@ export default defineNuxtConfig({ }, // differentDomains: true, // skipSettingLocaleOnNavigate: true, - detectBrowserLanguage: false, + detectBrowserLanguage: false // detectBrowserLanguage: { // useCookie: true, // // alwaysRedirect: true @@ -85,21 +85,6 @@ export default defineNuxtConfig({ // // // cookieKey: 'my_custom_cookie_name', // // redirectOn: 'root' // }, - // vueI18n: './vue-i18n.options.ts' - vueI18n: { - legacy: false, - locale: 'en', - fallbackLocale: 'fr' - // messages: { - // ja: { - // hello: 'こんにちは!' - // } - // } - // fallbackLocale: { - // en: ['ja', 'fr', 'en-US'], - // ja: ['en', 'fr', 'ja-JP'], - // fr: ['en', 'ja', 'fr-FR'] - // } - } + // vueI18n: { configFile: './i18n.config.ts' } } }) diff --git a/src/gen.ts b/src/gen.ts index cf641ce58..f778d05f5 100644 --- a/src/gen.ts +++ b/src/gen.ts @@ -43,7 +43,8 @@ export function generateLoaderOptions( dev: boolean ssg: boolean ssr: boolean - } = { dev: true, ssg: false, ssr: true } + } = { dev: true, ssg: false, ssr: true }, + vueI18nPath: string | false = false ) { const generatedImports = new Map() const importMapper = new Map() @@ -83,7 +84,7 @@ export function generateLoaderOptions( return gen } - let genCode = '' + let genCode = vueI18nPath ? `${genImport(vueI18nPath, 'vueI18nOptions')}\n` : '' const localeInfo = options.localeInfo || [] const syncLocaleFiles = new Set() const asyncLocaleFiles = new Set() @@ -135,25 +136,9 @@ export function generateLoaderOptions( genCode += `${Object.entries(options).map(([rootKey, rootValue]) => { if (rootKey === 'nuxtI18nOptions') { let genCodes = `export const resolveNuxtI18nOptions = async (context) => {\n` - genCodes += ` const ${rootKey} = Object({})\n` - for (const [key, value] of Object.entries(rootValue)) { - if (key === 'vueI18n') { - const optionLoaderVariable = `${key}OptionsLoader` - genCodes += ` const ${optionLoaderVariable} = ${isObject(value) - ? `async (context) => ${generateVueI18nOptions(value, misc.dev)}\n` - : isString(value) - ? `async (context) => import(${toCode(value)}).then(r => (r.default || r)(context))\n` - : `async (context) => ${toCode({})}\n` - }` - genCodes += ` ${rootKey}.${key} = await ${optionLoaderVariable}(context)\n` - if (isString(value)) { - const parsedLoaderPath = parsePath(value) - const loaderFilename = `${parsedLoaderPath.name}${parsedLoaderPath.ext}` - genCodes += ` if (${rootKey}.${key}.messages) { console.warn("[${NUXT_I18N_MODULE_ID}]: Cannot include 'messages' option in '${loaderFilename}'. Please use Lazy-load translations."); ${rootKey}.${key}.messages = {}; }\n` - } - } else { - genCodes += ` ${rootKey}.${key} = ${toCode(key === 'locales' ? stripPathFromLocales(value) : value)}\n` - } + genCodes += ` const ${rootKey} = ${JSON.stringify(rootValue)}\n` + if (vueI18nPath) { + genCodes += `${rootKey}.vueI18n = vueI18nOptions\n` } genCodes += ` return nuxtI18nOptions\n` genCodes += `}\n` @@ -300,25 +285,6 @@ function resolveLocaleRelativePath(relativeBase: string, langDir: string, file: return normalize(`${relativeBase}/${langDir}/${file}`) } -function generateVueI18nOptions(options: Record, dev: boolean): string { - let genCode = 'Object({' - for (const [key, value] of Object.entries(options)) { - if (key === 'messages') { - genCode += `${JSON.stringify(key)}: Object({` - for (const [locale, localeMessages] of Object.entries(value)) { - genCode += `${JSON.stringify(locale)}:${ - generateJSON(JSON.stringify(localeMessages), { type: 'bare', env: dev ? 'development' : 'production' }).code - },` - } - genCode += '}),' - } else { - genCode += `${JSON.stringify(key)}:${toCode(value)},` - } - } - genCode += '})' - return genCode -} - function generateAdditionalMessages(value: Record, dev: boolean): string { let genCode = 'Object({' for (const [locale, messages] of Object.entries(value)) { diff --git a/src/module.ts b/src/module.ts index 552df5b73..69f2d8c1b 100644 --- a/src/module.ts +++ b/src/module.ts @@ -12,7 +12,8 @@ import { addImports, addServerHandler, useLogger, - addPrerenderRoutes // TODO: remove? + addPrerenderRoutes, // TODO: remove? + resolvePath } from '@nuxt/kit' import { resolve, relative, isAbsolute } from 'pathe' import { defu } from 'defu' @@ -130,14 +131,18 @@ export default defineNuxtModule({ * resolve vue-i18n options */ - // prettier-ignore - options.vueI18n = isObject(options.vueI18n) - ? options.vueI18n - : isString(options.vueI18n) - ? resolve(nuxt.options.rootDir, options.vueI18n) - : { legacy: false } + const configPath = await resolvePath(options.vueI18n?.configFile || 'i18n.config', { + cwd: nuxt.options.rootDir, + extensions: ['.ts', '.mjs', '.js'] + }) + const configPathExists = existsSync(configPath) - const configPath = await resolvePath(options.vueI18n?.configFile || 'vue-i18n.options', { cwd: nuxt.options.rootDir, extensions: ['.ts', '.mjs', '.js'] }) + if (options.vueI18n?.configFile && !configPathExists) { + logger.warn(`Configuration file does not exist at ${configPath}. Skipping..`) + } + + // prettier-ignore + const vueI18n = configPathExists ? configPath : { legacy: false } /** * extend messages via 3rd party nuxt modules @@ -211,7 +216,8 @@ export default defineNuxtModule({ ssg: nuxt.options._generate, ssr: nuxt.options.ssr, dev: nuxt.options.dev - } + }, + configPathExists ? configPath : false ) } }) @@ -232,8 +238,8 @@ export default defineNuxtModule({ const isLegacyMode = () => { return isString(options.types) ? options.types === 'legacy' - : isObject(options.vueI18n) && isBoolean(options.vueI18n.legacy) - ? options.vueI18n.legacy + : isObject(vueI18n) && isBoolean(vueI18n.legacy) + ? vueI18n.legacy : false } diff --git a/src/runtime/composables.ts b/src/runtime/composables.ts index eb87b02cf..76bf03d67 100644 --- a/src/runtime/composables.ts +++ b/src/runtime/composables.ts @@ -15,7 +15,7 @@ import type { DetectBrowserLanguageOptions } from '#build/i18n.options.mjs' export * from 'vue-i18n' export type { LocaleObject } from 'vue-i18n-routing' -import type { Locale, LocaleMessages, DefineLocaleMessage } from 'vue-i18n' +import type { Locale, LocaleMessages, DefineLocaleMessage, I18nOptions } from 'vue-i18n' /** * The `useRouteBaseName` composable returns function that get the route base name. @@ -266,3 +266,14 @@ export function defineI18nLocale, ): LocaleLoader { return loader } + +// WIP - not exported yet (since this is build-time) +/** + * Define configuration for vue-i18n runtime plugin + * + * @param config - The target configuration for vue-i18n + * @returns The defined configuration + */ +export function defineI18nConfig(config: Config) { + return config +} diff --git a/src/types.ts b/src/types.ts index 95e321136..38133c297 100644 --- a/src/types.ts +++ b/src/types.ts @@ -41,6 +41,8 @@ export interface LocaleMessagePrecompileOptions { escapeHtml?: boolean } +export { I18nOptions } + export type NuxtI18nOptions = { experimental?: ExperimentalFeatures precompile?: LocaleMessagePrecompileOptions From 9da8fe7bfa58b03e77462f3063c806c0a6443eb1 Mon Sep 17 00:00:00 2001 From: Inesh Bose <2504266b@student.gla.ac.uk> Date: Fri, 24 Mar 2023 12:06:39 +0000 Subject: [PATCH 4/4] chore: updating minor bits and tests --- .../1.getting-started/2.basic-usage.md | 10 ++- docs/content/2.guide/11.locale-fallback.md | 4 +- docs/content/3.options/1.vue-i18n.md | 4 +- docs/content/4.API/1.composables.md | 14 ---- playground/i18n.config.ts | 10 +-- specs/fixtures/basic/i18n.config.ts | 33 +++++++++ specs/fixtures/basic/nuxt.config.ts | 35 +--------- specs/fixtures/basic_usage/i18n.config.ts | 16 +++++ specs/fixtures/basic_usage/nuxt.config.ts | 16 ----- specs/fixtures/fallback/i18n.config.ts | 18 +++++ specs/fixtures/fallback/nuxt.config.ts | 20 +----- specs/fixtures/head/i18n.config.ts | 18 +++++ specs/fixtures/head/nuxt.config.ts | 20 +----- specs/fixtures/issues/1721/i18n.config.ts | 16 +++++ specs/fixtures/issues/1721/nuxt.config.ts | 18 +---- specs/fixtures/lazy/i18n.config.ts | 5 ++ specs/fixtures/lazy/nuxt.config.ts | 7 +- specs/fixtures/switcher/i18n.config.ts | 18 +++++ specs/fixtures/switcher/nuxt.config.ts | 20 +----- .../vue_i18n_options_loader/nuxt.config.ts | 5 +- .../vue-i18n.messages.options.ts | 31 ++++----- .../vue-i18n.options.ts | 19 +++-- src/gen.ts | 2 +- src/messages.ts | 69 ++----------------- src/module.ts | 2 +- test/__snapshots__/gen.test.ts.snap | 61 ++-------------- test/gen.test.ts | 11 +-- 27 files changed, 189 insertions(+), 313 deletions(-) create mode 100644 specs/fixtures/basic/i18n.config.ts create mode 100644 specs/fixtures/basic_usage/i18n.config.ts create mode 100644 specs/fixtures/fallback/i18n.config.ts create mode 100644 specs/fixtures/head/i18n.config.ts create mode 100644 specs/fixtures/issues/1721/i18n.config.ts create mode 100644 specs/fixtures/lazy/i18n.config.ts create mode 100644 specs/fixtures/switcher/i18n.config.ts diff --git a/docs/content/1.getting-started/2.basic-usage.md b/docs/content/1.getting-started/2.basic-usage.md index f925e7caa..7f8cbfd28 100644 --- a/docs/content/1.getting-started/2.basic-usage.md +++ b/docs/content/1.getting-started/2.basic-usage.md @@ -22,7 +22,9 @@ export default defineNuxtConfig({ ``` ```ts {}[i18n.config.ts] -export default defineI18nConfig({ +import type { I18nOptions } from 'vue-i18n' + +const config: I18nOptions = { legacy: false, locale: 'en', messages: { @@ -33,10 +35,12 @@ export default defineI18nConfig({ welcome: 'Bienvenue' } } -}) +} + +export default config ``` -The `defineI18nConfig` function is the same as `createI18n` function option of Vue I18n. The configuration is passed to the `createI18n` function via the nuxt plugin (runtime) of this module internally. +The `i18n.config` file exports the same options as `createI18n` function of Vue I18n. The configuration is passed to the `createI18n` function via the nuxt plugin (runtime) of this module internally. For more details about configuration, see the [Vue I18n documentation](https://vue-i18n.intlify.dev/api/general.html#createi18n). diff --git a/docs/content/2.guide/11.locale-fallback.md b/docs/content/2.guide/11.locale-fallback.md index dd93ddf45..1e2e3ceaa 100644 --- a/docs/content/2.guide/11.locale-fallback.md +++ b/docs/content/2.guide/11.locale-fallback.md @@ -8,7 +8,7 @@ How a fallback gets selected when a translation is missing. or a decision map for more specific needs. ```js [i18n.config.ts] -export default defineI18nConfig({ +export default { fallbackLocale: 'en', // or fallbackLocale: ['en', 'fr'], @@ -22,7 +22,7 @@ export default defineI18nConfig({ 'default': ['en', 'da'] } // ... -}) +} ``` More information in [Vue I18n documentation](https://vue-i18n.intlify.dev/guide/essentials/fallback.html) diff --git a/docs/content/3.options/1.vue-i18n.md b/docs/content/3.options/1.vue-i18n.md index 4712b262e..f7995244a 100644 --- a/docs/content/3.options/1.vue-i18n.md +++ b/docs/content/3.options/1.vue-i18n.md @@ -25,7 +25,7 @@ export default defineNuxtConfig({ ``` ```ts {}[nuxt-i18n.js] -export default defineI18nConfig({ +export default { legacy: false, locale: 'en', messages: { @@ -36,7 +36,7 @@ export default defineI18nConfig({ welcome: 'Bienvenue' } } -}) +} ``` :: diff --git a/docs/content/4.API/1.composables.md b/docs/content/4.API/1.composables.md index 8db56ec0c..19ed11708 100644 --- a/docs/content/4.API/1.composables.md +++ b/docs/content/4.API/1.composables.md @@ -220,17 +220,3 @@ A function that is the dynamic locale messages loading, that has the following p - when you switch the locale with `setLocale`. - when the locale is switched with ``. for example, the route path resolved by `useSwitchLocalePath` or `$switchLocalePath`. - -## `defineI18nConfig` - -The `defineI18nConfig` defines the configuration to be passed to the `vue-i18n` plugin in runtime. - -You can use at JavaScript and TypeScript extension formats. - -### Parameters - -#### `config` - -**Type**: `I18nOptions` - -The object with configuration definition. diff --git a/playground/i18n.config.ts b/playground/i18n.config.ts index ffb9f7e4e..bb1a829f7 100644 --- a/playground/i18n.config.ts +++ b/playground/i18n.config.ts @@ -1,7 +1,7 @@ -// import { defineI18nConfig, type I18nOptions } from '@nuxtjs/i18n' +import type { I18nOptions } from 'vue-i18n' -// export default { -export default defineI18nConfig({ +// /** @type {import('@nuxtjs/i18n').I18nOptions} */ +const config: I18nOptions = { legacy: false, locale: 'en', fallbackLocale: 'fr' @@ -15,4 +15,6 @@ export default defineI18nConfig({ // ja: ['en', 'fr', 'ja-JP'], // fr: ['en', 'ja', 'fr-FR'] // } -}) +} + +export default config diff --git a/specs/fixtures/basic/i18n.config.ts b/specs/fixtures/basic/i18n.config.ts new file mode 100644 index 000000000..b5a4528d1 --- /dev/null +++ b/specs/fixtures/basic/i18n.config.ts @@ -0,0 +1,33 @@ +export default { + legacy: false, + locale: 'en', + messages: { + fr: { + welcome: 'Bienvenue', + home: 'Accueil', + profile: 'Profil', + about: 'À propos', + posts: 'Articles', + dynamic: 'Dynamique', + pages: { + blog: { + article: "Cette page d'article de blog" + } + } + }, + en: { + welcome: 'Welcome', + home: 'Homepage', + profile: 'Profile', + about: 'About us', + posts: 'Posts', + dynamic: 'Dynamic', + pages: { + blog: { + article: 'This is blog article page' + } + } + } + }, + fallbackLocale: 'en' +} diff --git a/specs/fixtures/basic/nuxt.config.ts b/specs/fixtures/basic/nuxt.config.ts index 819645a36..62e87307f 100644 --- a/specs/fixtures/basic/nuxt.config.ts +++ b/specs/fixtures/basic/nuxt.config.ts @@ -20,39 +20,6 @@ export default defineNuxtConfig({ } ], defaultLocale: 'en', - detectBrowserLanguage: false, - vueI18n: { - legacy: false, - locale: 'en', - messages: { - fr: { - welcome: 'Bienvenue', - home: 'Accueil', - profile: 'Profil', - about: 'À propos', - posts: 'Articles', - dynamic: 'Dynamique', - pages: { - blog: { - article: "Cette page d'article de blog" - } - } - }, - en: { - welcome: 'Welcome', - home: 'Homepage', - profile: 'Profile', - about: 'About us', - posts: 'Posts', - dynamic: 'Dynamic', - pages: { - blog: { - article: 'This is blog article page' - } - } - } - }, - fallbackLocale: 'en' - } + detectBrowserLanguage: false } }) diff --git a/specs/fixtures/basic_usage/i18n.config.ts b/specs/fixtures/basic_usage/i18n.config.ts new file mode 100644 index 000000000..25c4aca6b --- /dev/null +++ b/specs/fixtures/basic_usage/i18n.config.ts @@ -0,0 +1,16 @@ +export default { + legacy: false, + locale: 'en', + messages: { + fr: { + welcome: 'Bienvenue', + home: 'Accueil', + profile: 'Profil' + }, + en: { + welcome: 'Welcome', + home: 'Homepage', + profile: 'Profile' + } + } +} diff --git a/specs/fixtures/basic_usage/nuxt.config.ts b/specs/fixtures/basic_usage/nuxt.config.ts index ebaa95837..1a9c99391 100644 --- a/specs/fixtures/basic_usage/nuxt.config.ts +++ b/specs/fixtures/basic_usage/nuxt.config.ts @@ -4,21 +4,5 @@ export default defineNuxtConfig({ i18n: { // debug: true, - vueI18n: { - legacy: false, - locale: 'en', - messages: { - fr: { - welcome: 'Bienvenue', - home: 'Accueil', - profile: 'Profil' - }, - en: { - welcome: 'Welcome', - home: 'Homepage', - profile: 'Profile' - } - } - } } }) diff --git a/specs/fixtures/fallback/i18n.config.ts b/specs/fixtures/fallback/i18n.config.ts new file mode 100644 index 000000000..65e740a24 --- /dev/null +++ b/specs/fixtures/fallback/i18n.config.ts @@ -0,0 +1,18 @@ +export default { + legacy: false, + fallbackLocale: 'en', + messages: { + en: { + home: 'Homepage', + about: 'About us', + posts: 'Posts', + dynamic: 'Dynamic' + }, + fr: { + home: 'Accueil', + about: 'À propos', + posts: 'Articles', + dynamic: 'Dynamique' + } + } +} diff --git a/specs/fixtures/fallback/nuxt.config.ts b/specs/fixtures/fallback/nuxt.config.ts index 607b1a94a..5a2dfac22 100644 --- a/specs/fixtures/fallback/nuxt.config.ts +++ b/specs/fixtures/fallback/nuxt.config.ts @@ -6,24 +6,6 @@ export default defineNuxtConfig({ lazy: false, defaultLocale: 'en', detectBrowserLanguage: false, - locales: ['fr', 'ja', 'en'], - vueI18n: { - legacy: false, - fallbackLocale: 'en', - messages: { - en: { - home: 'Homepage', - about: 'About us', - posts: 'Posts', - dynamic: 'Dynamic' - }, - fr: { - home: 'Accueil', - about: 'À propos', - posts: 'Articles', - dynamic: 'Dynamique' - } - } - } + locales: ['fr', 'ja', 'en'] } }) diff --git a/specs/fixtures/head/i18n.config.ts b/specs/fixtures/head/i18n.config.ts new file mode 100644 index 000000000..35b8af2ff --- /dev/null +++ b/specs/fixtures/head/i18n.config.ts @@ -0,0 +1,18 @@ +export default { + legacy: false, + messages: { + fr: { + home: 'Accueil', + about: 'À propos', + posts: 'Articles', + dynamic: 'Dynamique' + }, + en: { + home: 'Homepage', + about: 'About us', + posts: 'Posts', + dynamic: 'Dynamic' + } + }, + fallbackLocale: 'en' +} diff --git a/specs/fixtures/head/nuxt.config.ts b/specs/fixtures/head/nuxt.config.ts index be65024d1..d5c141944 100644 --- a/specs/fixtures/head/nuxt.config.ts +++ b/specs/fixtures/head/nuxt.config.ts @@ -18,24 +18,6 @@ export default defineNuxtConfig({ ], defaultLocale: 'en', detectBrowserLanguage: false, - lazy: false, - vueI18n: { - legacy: false, - messages: { - fr: { - home: 'Accueil', - about: 'À propos', - posts: 'Articles', - dynamic: 'Dynamique' - }, - en: { - home: 'Homepage', - about: 'About us', - posts: 'Posts', - dynamic: 'Dynamic' - } - }, - fallbackLocale: 'en' - } + lazy: false } }) diff --git a/specs/fixtures/issues/1721/i18n.config.ts b/specs/fixtures/issues/1721/i18n.config.ts new file mode 100644 index 000000000..2b69a90a5 --- /dev/null +++ b/specs/fixtures/issues/1721/i18n.config.ts @@ -0,0 +1,16 @@ +export default { + legacy: false, + locale: 'en', + fallbackLocale: ['en'], + messages: { + en: { + welcome: 'Welcome' + }, + fr: { + welcome: 'Bienvenue' + }, + es: { + welcome: 'Bienvenido' + } + } +} diff --git a/specs/fixtures/issues/1721/nuxt.config.ts b/specs/fixtures/issues/1721/nuxt.config.ts index 3cb0f911b..6337dff60 100644 --- a/specs/fixtures/issues/1721/nuxt.config.ts +++ b/specs/fixtures/issues/1721/nuxt.config.ts @@ -23,22 +23,6 @@ export default defineNuxtConfig({ code: 'es', name: 'ES' } - ], - vueI18n: { - legacy: false, - locale: 'en', - fallbackLocale: ['en'], - messages: { - en: { - welcome: 'Welcome' - }, - fr: { - welcome: 'Bienvenue' - }, - es: { - welcome: 'Bienvenido' - } - } - } + ] } }) diff --git a/specs/fixtures/lazy/i18n.config.ts b/specs/fixtures/lazy/i18n.config.ts new file mode 100644 index 000000000..48ac97f9f --- /dev/null +++ b/specs/fixtures/lazy/i18n.config.ts @@ -0,0 +1,5 @@ +export default { + legacy: false, + messages: {}, + fallbackLocale: 'en' +} diff --git a/specs/fixtures/lazy/nuxt.config.ts b/specs/fixtures/lazy/nuxt.config.ts index 4b2be332a..1aec23f87 100644 --- a/specs/fixtures/lazy/nuxt.config.ts +++ b/specs/fixtures/lazy/nuxt.config.ts @@ -22,11 +22,6 @@ export default defineNuxtConfig({ ], */ defaultLocale: 'fr', - detectBrowserLanguage: false, - vueI18n: { - legacy: false, - messages: {}, - fallbackLocale: 'en' - } + detectBrowserLanguage: false } }) diff --git a/specs/fixtures/switcher/i18n.config.ts b/specs/fixtures/switcher/i18n.config.ts new file mode 100644 index 000000000..35b8af2ff --- /dev/null +++ b/specs/fixtures/switcher/i18n.config.ts @@ -0,0 +1,18 @@ +export default { + legacy: false, + messages: { + fr: { + home: 'Accueil', + about: 'À propos', + posts: 'Articles', + dynamic: 'Dynamique' + }, + en: { + home: 'Homepage', + about: 'About us', + posts: 'Posts', + dynamic: 'Dynamic' + } + }, + fallbackLocale: 'en' +} diff --git a/specs/fixtures/switcher/nuxt.config.ts b/specs/fixtures/switcher/nuxt.config.ts index 3eac59c2f..b41f47698 100644 --- a/specs/fixtures/switcher/nuxt.config.ts +++ b/specs/fixtures/switcher/nuxt.config.ts @@ -28,24 +28,6 @@ export default defineNuxtConfig({ ], defaultLocale: 'en', skipSettingLocaleOnNavigate: true, - detectBrowserLanguage: false, - vueI18n: { - legacy: false, - messages: { - fr: { - home: 'Accueil', - about: 'À propos', - posts: 'Articles', - dynamic: 'Dynamique' - }, - en: { - home: 'Homepage', - about: 'About us', - posts: 'Posts', - dynamic: 'Dynamic' - } - }, - fallbackLocale: 'en' - } + detectBrowserLanguage: false } }) diff --git a/specs/fixtures/vue_i18n_options_loader/nuxt.config.ts b/specs/fixtures/vue_i18n_options_loader/nuxt.config.ts index 5ab27e113..bc77eae8a 100644 --- a/specs/fixtures/vue_i18n_options_loader/nuxt.config.ts +++ b/specs/fixtures/vue_i18n_options_loader/nuxt.config.ts @@ -26,6 +26,9 @@ export default defineNuxtConfig({ } ], defaultLocale: 'en', - detectBrowserLanguage: false + detectBrowserLanguage: false, + vueI18n: { + configFile: './vue-i18n.messages.options.ts' + } } }) diff --git a/specs/fixtures/vue_i18n_options_loader/vue-i18n.messages.options.ts b/specs/fixtures/vue_i18n_options_loader/vue-i18n.messages.options.ts index 07e2577af..5d99e4bcb 100644 --- a/specs/fixtures/vue_i18n_options_loader/vue-i18n.messages.options.ts +++ b/specs/fixtures/vue_i18n_options_loader/vue-i18n.messages.options.ts @@ -1,21 +1,18 @@ import type { I18nOptions } from 'vue-i18n' -import type { NuxtApp } from 'nuxt/dist/app/index' -export default function (nuxt: NuxtApp) { - return { - legacy: false, - locale: 'en', - fallbackLocale: 'en', - modifiers: { - snakeCase: (str: string) => str.split(' ').join('-') +export default { + legacy: false, + locale: 'en', + fallbackLocale: 'en', + modifiers: { + snakeCase: (str: string) => str.split(' ').join('-') + }, + messages: { + en: { + hello: 'Hello!' }, - messages: { - en: { - hello: 'Hello!' - }, - fr: { - hello: 'Bonjour!' - } + fr: { + hello: 'Bonjour!' } - } as I18nOptions -} + } +} as I18nOptions diff --git a/specs/fixtures/vue_i18n_options_loader/vue-i18n.options.ts b/specs/fixtures/vue_i18n_options_loader/vue-i18n.options.ts index c88e2a98f..ef18539af 100644 --- a/specs/fixtures/vue_i18n_options_loader/vue-i18n.options.ts +++ b/specs/fixtures/vue_i18n_options_loader/vue-i18n.options.ts @@ -1,13 +1,10 @@ import type { I18nOptions } from 'vue-i18n' -import type { NuxtApp } from 'nuxt/dist/app/index' -export default function (nuxt: NuxtApp) { - return { - legacy: false, - locale: 'en', - fallbackLocale: 'en', - modifiers: { - snakeCase: (str: string) => str.split(' ').join('-') - } - } as I18nOptions -} +export default { + legacy: false, + locale: 'en', + fallbackLocale: 'en', + modifiers: { + snakeCase: (str: string) => str.split(' ').join('-') + } +} as I18nOptions diff --git a/src/gen.ts b/src/gen.ts index f778d05f5..1bb117613 100644 --- a/src/gen.ts +++ b/src/gen.ts @@ -136,7 +136,7 @@ export function generateLoaderOptions( genCode += `${Object.entries(options).map(([rootKey, rootValue]) => { if (rootKey === 'nuxtI18nOptions') { let genCodes = `export const resolveNuxtI18nOptions = async (context) => {\n` - genCodes += ` const ${rootKey} = ${JSON.stringify(rootValue)}\n` + genCodes += ` const ${rootKey} = ${JSON.stringify(rootValue || {})}\n` if (vueI18nPath) { genCodes += `${rootKey}.vueI18n = vueI18nOptions\n` } diff --git a/src/messages.ts b/src/messages.ts index ce885fef5..f68e489cd 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -1,85 +1,26 @@ import createDebug from 'debug' -import { isString, isArray, isObject, hasOwn } from '@intlify/shared' import type { Nuxt } from '@nuxt/schema' -import type { DefineLocaleMessage, FallbackLocale, Locale, LocaleMessages } from 'vue-i18n' -import type { NuxtI18nOptions } from './types' +import type { DefineLocaleMessage, Locale, LocaleMessages } from 'vue-i18n' const debug = createDebug('@nuxtjs/i18n:messages') export type AdditionalMessages = Record -export async function extendMessages( - nuxt: Nuxt, - localeCodes: string[], - nuxtOptions: Required -): Promise { +export async function extendMessages(nuxt: Nuxt, localeCodes: string[]): Promise { const additionalMessages: LocaleMessages[] = [] await nuxt.callHook('i18n:extend-messages', additionalMessages, localeCodes) debug('i18n:extend-messages additional messages', additionalMessages) - return normalizeMessages(additionalMessages, localeCodes, nuxtOptions) + return normalizeMessages(additionalMessages, localeCodes) } -const isNotObjectOrIsArray = (val: unknown) => !isObject(val) || isArray(val) - -// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types -function deepCopy(src: Record, des: Record): void { - for (const key in src) { - if (hasOwn(src, key)) { - if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) { - des[key] = src[key] - } else { - deepCopy(src[key], des[key]) - } - } - } -} - -function getLocaleCodes(fallback: FallbackLocale, locales: Locale[]): Locale[] { - let fallbackLocales: string[] = [] - if (isArray(fallback)) { - fallbackLocales = fallback - } else if (isObject(fallback)) { - const targets = [...locales, 'default'] - for (const locale of targets) { - if (fallback[locale]) { - fallbackLocales = [...fallbackLocales, ...fallback[locale].filter(Boolean)] - } - } - } else if (isString(fallback) && locales.every(locale => locale !== fallback)) { - fallbackLocales.push(fallback) - } - return fallbackLocales -} - -async function normalizeMessages( - additional: LocaleMessages[], - localeCodes: string[], - nuxtOptions: Required -) { +async function normalizeMessages(additional: LocaleMessages[], localeCodes: string[]) { /** * merge additional messages into vueI18n messages */ - let targetLocaleCodes = [...localeCodes] - if (isObject(nuxtOptions.vueI18n)) { - nuxtOptions.vueI18n.messages = nuxtOptions.vueI18n.messages || {} - const locale = nuxtOptions.defaultLocale || nuxtOptions.vueI18n.locale || 'en-US' - const locales = nuxtOptions.vueI18n.fallbackLocale - ? getLocaleCodes(nuxtOptions.vueI18n.fallbackLocale, [locale]) - : [locale] - for (const locale of locales) { - nuxtOptions.vueI18n.messages[locale] = nuxtOptions.vueI18n.messages[locale] || {} - } - for (const [, messages] of Object.entries(additional)) { - for (const locale of locales) { - deepCopy(messages[locale], nuxtOptions.vueI18n.messages[locale]) - } - } - targetLocaleCodes = localeCodes.filter(code => !locales.includes(code)) - debug('vueI18n messages', nuxtOptions.vueI18n.messages) - } + const targetLocaleCodes = [...localeCodes] /** * collect additional messages for each locale diff --git a/src/module.ts b/src/module.ts index 69f2d8c1b..e5ba472ac 100644 --- a/src/module.ts +++ b/src/module.ts @@ -148,7 +148,7 @@ export default defineNuxtModule({ * extend messages via 3rd party nuxt modules */ - const additionalMessages = await extendMessages(nuxt, localeCodes, options) + const additionalMessages = await extendMessages(nuxt, localeCodes) /** * setup nuxt/pages diff --git a/test/__snapshots__/gen.test.ts.snap b/test/__snapshots__/gen.test.ts.snap index 875f53bf9..c391628a2 100644 --- a/test/__snapshots__/gen.test.ts.snap +++ b/test/__snapshots__/gen.test.ts.snap @@ -15,12 +15,7 @@ export const localeMessages = { export const additionalMessages = Object({}) export const resolveNuxtI18nOptions = async (context) => { - const nuxtI18nOptions = Object({}) - nuxtI18nOptions.defaultLocale = \\"en\\" - const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{ - \\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})() -},}),}) - nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context) + const nuxtI18nOptions = {\\"defaultLocale\\":\\"en\\"} return nuxtI18nOptions } @@ -47,12 +42,7 @@ export const localeMessages = { export const additionalMessages = Object({}) export const resolveNuxtI18nOptions = async (context) => { - const nuxtI18nOptions = Object({}) - nuxtI18nOptions.defaultLocale = \\"en\\" - const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{ - \\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})() -},}),}) - nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context) + const nuxtI18nOptions = {\\"defaultLocale\\":\\"en\\"} return nuxtI18nOptions } @@ -77,12 +67,7 @@ export const localeMessages = { export const additionalMessages = Object({}) export const resolveNuxtI18nOptions = async (context) => { - const nuxtI18nOptions = Object({}) - nuxtI18nOptions.defaultLocale = \\"en\\" - const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{ - \\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})() -},}),}) - nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context) + const nuxtI18nOptions = {\\"defaultLocale\\":\\"en\\"} return nuxtI18nOptions } @@ -109,12 +94,7 @@ export const localeMessages = { export const additionalMessages = Object({}) export const resolveNuxtI18nOptions = async (context) => { - const nuxtI18nOptions = Object({}) - nuxtI18nOptions.defaultLocale = \\"en\\" - const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{ - \\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})() -},}),}) - nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context) + const nuxtI18nOptions = {\\"defaultLocale\\":\\"en\\"} return nuxtI18nOptions } @@ -133,19 +113,7 @@ exports[`toCode: function (arrow) 1`] = ` export const additionalMessages = Object({}) export const resolveNuxtI18nOptions = async (context) => { - const nuxtI18nOptions = Object({}) - nuxtI18nOptions.defaultLocale = \\"en\\" - const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{ - \\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})() -},}),}) - nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context) - nuxtI18nOptions.locales = [Object({\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"testFunc\\":((prop) => { - return \`Hello \${prop}\`; - })}),Object({\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"testFunc\\":((prop) => { - return \`Hello \${prop}\`; - })}),Object({\\"code\\":\\"fr\\",\\"file\\":\\"fr.json\\",\\"testFunc\\":((prop) => { - return \`Hello \${prop}\`; - })})] + const nuxtI18nOptions = {\\"defaultLocale\\":\\"en\\",\\"locales\\":[{\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"path\\":\\"/path/to/en.json\\"},{\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"path\\":\\"/path/to/ja.json\\"},{\\"code\\":\\"fr\\",\\"file\\":\\"fr.json\\",\\"path\\":\\"/path/to/fr.json\\"}]} return nuxtI18nOptions } @@ -166,19 +134,7 @@ exports[`toCode: function (named) 1`] = ` export const additionalMessages = Object({}) export const resolveNuxtI18nOptions = async (context) => { - const nuxtI18nOptions = Object({}) - nuxtI18nOptions.defaultLocale = \\"en\\" - const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"fr\\",\\"messages\\": Object({\\"en\\":{ - \\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})() -},}),}) - nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context) - nuxtI18nOptions.locales = [Object({\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"testFunc\\":(function (prop) { - return \`Hello \${prop}\`; - })}),Object({\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"testFunc\\":(function (prop) { - return \`Hello \${prop}\`; - })}),Object({\\"code\\":\\"fr\\",\\"file\\":\\"fr.json\\",\\"testFunc\\":(function (prop) { - return \`Hello \${prop}\`; - })})] + const nuxtI18nOptions = {\\"defaultLocale\\":\\"en\\",\\"locales\\":[{\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"path\\":\\"/path/to/en.json\\"},{\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"path\\":\\"/path/to/ja.json\\"},{\\"code\\":\\"fr\\",\\"file\\":\\"fr.json\\",\\"path\\":\\"/path/to/fr.json\\"}]} return nuxtI18nOptions } @@ -220,10 +176,7 @@ export const additionalMessages = Object({\\"en\\":[() => Promise.resolve({ }),],}) export const resolveNuxtI18nOptions = async (context) => { - const nuxtI18nOptions = Object({}) - const vueI18nOptionsLoader = async (context) => import(\\"~/plugins/vue-i18n.js\\").then(r => (r.default || r)(context)) - nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context) - if (nuxtI18nOptions.vueI18n.messages) { console.warn(\\"[@nuxtjs/i18n]: Cannot include 'messages' option in 'vue-i18n.js'. Please use Lazy-load translations.\\"); nuxtI18nOptions.vueI18n.messages = {}; } + const nuxtI18nOptions = {\\"vueI18n\\":{\\"configFile\\":\\"~/plugins/vue-i18n.js\\"}} return nuxtI18nOptions } diff --git a/test/gen.test.ts b/test/gen.test.ts index 17dc75a7e..0790cf7c9 100644 --- a/test/gen.test.ts +++ b/test/gen.test.ts @@ -34,14 +34,7 @@ const ADDITIONAL_MESSAGES = { ] } as AdditionalMessages const NUXT_I18N_OPTIONS = { - defaultLocale: 'en', - vueI18n: { - locale: 'en', - fallbackLocale: 'fr', - messages: { - en: { hello: 'Hello!' } - } - } + defaultLocale: 'en' } as NuxtI18nOptions const NUXT_I18N_INTERNAL_OPTIONS = { @@ -180,7 +173,7 @@ test('vueI18n: path', () => { localeInfo: LOCALE_INFO, additionalMessages: ADDITIONAL_MESSAGES, nuxtI18nOptions: { - vueI18n: '~/plugins/vue-i18n.js' + vueI18n: { configFile: '~/plugins/vue-i18n.js' } }, nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS },