diff --git a/docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md b/docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md index 083cd1164..28d435e32 100644 --- a/docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md +++ b/docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md @@ -50,4 +50,13 @@ Reasons for change ## Locale `iso` renamed to `language` -The `iso` property on a locale object has been renamed to `language` to be consistent with the usage of Language Tags on the web (e.g. `navigator.language` and `Accept-Language`). The original `iso` property name referred to ISO standards which describe valid Language Tags, see the [related issue](https://github.com/nuxt-modules/i18n/issues/2449) for more details. \ No newline at end of file +The `iso` property on a locale object has been renamed to `language` to be consistent with the usage of Language Tags on the web (e.g. `navigator.language` and `Accept-Language`). The original `iso` property name referred to ISO standards which describe valid Language Tags, see the [related issue](https://github.com/nuxt-modules/i18n/issues/2449) for more details. + +## Runtime config properties + +Some properties have changed or swapped names to better fit their functionality, the runtime config properties configured by this module are mostly used for internal purposes and should not be relied upon but it's worth noting these changes. + +| v8 | v9 | Notes | +| --- | --- | --- | +| `locales` | `domainLocales` | This also changes the environment variable key to `NUXT_PUBLIC_I18N_DOMAIN_LOCALES_{code}_DOMAIN`, see [`runtimeConfig`](/docs/options/runtime-config#domainLocales) | +| `configLocales` | `locales` | | \ No newline at end of file diff --git a/docs/content/docs/5.v9/3.options/9.runtime-config.md b/docs/content/docs/5.v9/3.options/9.runtime-config.md index c08a8b77b..25619db26 100644 --- a/docs/content/docs/5.v9/3.options/9.runtime-config.md +++ b/docs/content/docs/5.v9/3.options/9.runtime-config.md @@ -54,9 +54,9 @@ This runtime config option is the same as the [`baseUrl`](/docs/options/routing# Note that the `baseUrl` module option allows you to set the function, but the runtime config does not due to limitations. :: -### `locales` +### `domainLocales` - property: `locales[code].domain` -- key: `NUXT_PUBLIC_I18N_LOCALES_{code}_DOMAIN` +- key: `NUXT_PUBLIC_I18N_DOMAIN_LOCALES_{code}_DOMAIN` This runtime config option allows overriding the domain set in the [`locales`](/docs/options/routing#locales) module option. diff --git a/specs/different_domains/different_domains.spec.ts b/specs/different_domains/different_domains.spec.ts index cfe8fd694..1d569c2ad 100644 --- a/specs/different_domains/different_domains.spec.ts +++ b/specs/different_domains/different_domains.spec.ts @@ -11,7 +11,7 @@ await setup({ runtimeConfig: { public: { i18n: { - locales: { + domainLocales: { kr: { domain: 'kr.staging.nuxt-app.localhost' } diff --git a/src/module.ts b/src/module.ts index 2f5410d1f..65c0b7419 100644 --- a/src/module.ts +++ b/src/module.ts @@ -143,18 +143,7 @@ export default defineNuxtModule({ skipSettingLocaleOnNavigate: options.skipSettingLocaleOnNavigate, differentDomains: options.differentDomains, trailingSlash: options.trailingSlash, - configLocales: options.locales, - locales: options.locales.reduce( - (obj, locale) => { - if (typeof locale === 'string') { - obj[locale] = { domain: undefined } - } else { - obj[locale.code] = { domain: locale.domain } - } - return obj - }, - {} as Record - ), + locales: options.locales, detectBrowserLanguage: options.detectBrowserLanguage ?? DEFAULT_OPTIONS.detectBrowserLanguage, experimental: options.experimental, multiDomainLocales: options.multiDomainLocales @@ -245,7 +234,7 @@ export default defineNuxtModule({ } // @ts-expect-error type error - nuxt.options.runtimeConfig.public.i18n.configLocales = simplifyLocaleOptions(nuxt, defu({}, options)) + nuxt.options.runtimeConfig.public.i18n.locales = simplifyLocaleOptions(nuxt, defu({}, options)) addTemplate({ filename: NUXT_I18N_TEMPLATE_OPTIONS_KEY, @@ -387,6 +376,13 @@ export interface ModulePublicRuntimeConfig { rootRedirect: NuxtI18nOptions['rootRedirect'] multiDomainLocales?: NuxtI18nOptions['multiDomainLocales'] + /** + * Overwritten at build time, used to pass generated options to runtime + * + * @internal + */ + domainLocales: { [key: Locale]: { domain: string | undefined } } + /** * Overwritten at build time, used to pass generated options to runtime * @@ -398,7 +394,7 @@ export interface ModulePublicRuntimeConfig { * * @internal */ - configLocales: NonNullable>['locales']> + locales: NonNullable>['locales']> /** * Overwritten at build time, used to pass generated options to runtime * diff --git a/src/runtime/internal.ts b/src/runtime/internal.ts index adeb2c378..1a9b71b26 100644 --- a/src/runtime/internal.ts +++ b/src/runtime/internal.ts @@ -343,13 +343,9 @@ export function getDomainFromLocale(localeCode: Locale): string | undefined { const nuxtApp = useNuxtApp() const host = getHost() // lookup the `differentDomain` origin associated with given locale. - const config = runtimeConfig.public.i18n as { locales?: Record } + const config = runtimeConfig.public.i18n const lang = normalizedLocales.find(locale => locale.code === localeCode) - const domain = - config?.locales?.[localeCode]?.domain || - lang?.domain || - config?.locales?.[localeCode]?.domains?.find((v: string) => v === host) || - lang?.domains?.find((v: string) => v === host) + const domain = config?.domainLocales?.[localeCode]?.domain || lang?.domain || lang?.domains?.find(v => v === host) if (domain) { if (hasProtocol(domain, { strict: true })) { diff --git a/src/runtime/plugins/i18n.ts b/src/runtime/plugins/i18n.ts index 284e62acd..368afcfa2 100644 --- a/src/runtime/plugins/i18n.ts +++ b/src/runtime/plugins/i18n.ts @@ -53,9 +53,9 @@ export default defineNuxtPlugin({ const { vueApp: app } = nuxt const nuxtContext = nuxt as unknown as NuxtApp const host = getHost() - const { configLocales, defaultLocale, multiDomainLocales, strategy } = nuxtContext.$config.public.i18n + const { locales, defaultLocale, multiDomainLocales, strategy } = nuxtContext.$config.public.i18n - const hasDefaultForDomains = configLocales.some( + const hasDefaultForDomains = locales.some( (l): l is LocaleObject => typeof l !== 'string' && Array.isArray(l.defaultForDomains) ) @@ -63,7 +63,7 @@ export default defineNuxtPlugin({ if (defaultLocale) { defaultLocaleDomain = defaultLocale } else if (hasDefaultForDomains) { - const findDefaultLocale = configLocales.find((l): l is LocaleObject => + const findDefaultLocale = locales.find((l): l is LocaleObject => typeof l === 'string' || !Array.isArray(l.defaultForDomains) ? false : l.defaultForDomains.includes(host ?? '') ) @@ -175,7 +175,7 @@ export default defineNuxtPlugin({ extendI18n(i18n, { extendComposer(composer) { const route = useRoute() - const _locales = ref(runtimeI18n.configLocales) + const _locales = ref(runtimeI18n.locales) const _localeCodes = ref(localeCodes) const _baseUrl = ref('') diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index 13d84dbf5..a98424f15 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -282,7 +282,7 @@ export async function navigate( { status = 302, enableNavigate = false }: { status?: number; enableNavigate?: boolean } = {} ) { const { nuxtApp, i18n, locale, route } = args - const { rootRedirect, differentDomains, multiDomainLocales, skipSettingLocaleOnNavigate, configLocales, strategy } = + const { rootRedirect, differentDomains, multiDomainLocales, skipSettingLocaleOnNavigate, locales, strategy } = nuxtApp.$config.public.i18n const logger = /*#__PURE__*/ createLogger('navigate') let { redirectPath } = args @@ -304,6 +304,9 @@ export async function navigate( redirectPath = '/' + rootRedirect.path status = rootRedirect.statusCode } + + // TODO: resolve type errors for nuxt context extensions + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment redirectPath = nuxtApp.$localePath(redirectPath, locale) __DEBUG__ && logger.log('rootRedirect mode', { redirectPath, status }) return _navigate(redirectPath, status) @@ -321,7 +324,7 @@ export async function navigate( if (multiDomainLocales && strategy === 'prefix_except_default') { const host = getHost() - const currentDomain = configLocales.find(locale => { + const currentDomain = locales.find(locale => { if (typeof locale !== 'string') { return locale.defaultForDomains?.find(domain => domain === host) }