From 53f16e6ad92487d1d7b2db479e8d9da5ee03b0d6 Mon Sep 17 00:00:00 2001 From: Bobbie Goede Date: Thu, 15 Aug 2024 17:15:27 +0200 Subject: [PATCH] fix: `strategy: 'no_prefix'` when using `differentDomains` (#3061) * fix: route localization with `differentDomains` * fix: prevent route removal * fix: `switchLocalePath` resolution for `differentDomains` * docs: update notes to clarify `differentDomains` case --- docs/content/docs/2.guide/1.index.md | 2 +- docs/content/docs/2.guide/3.custom-paths.md | 2 +- .../2.guide/4.ignoring-localized-routes.md | 2 +- docs/content/docs/3.options/5.domain.md | 2 +- docs/content/docs/5.v7/7.custom-paths.md | 2 +- .../docs/5.v7/8.ignoring-localized-routes.md | 2 +- docs/content/docs/5.v9/2.guide/1.index.md | 92 +++++++ .../docs/5.v9/2.guide/3.custom-paths.md | 243 ++++++++++++++++++ .../2.guide/4.ignoring-localized-routes.md | 56 ++++ docs/content/docs/5.v9/3.options/5.domain.md | 11 + .../different_domains.spec.ts | 33 +++ .../pages/localized-route.vue | 11 + src/module.ts | 2 +- src/pages.ts | 8 +- src/routing.ts | 30 ++- src/runtime/routing/compatibles/routing.ts | 17 +- src/runtime/routing/utils.ts | 14 +- test/routing-utils.test.ts | 12 +- 18 files changed, 508 insertions(+), 33 deletions(-) create mode 100644 docs/content/docs/5.v9/2.guide/1.index.md create mode 100644 docs/content/docs/5.v9/2.guide/3.custom-paths.md create mode 100644 docs/content/docs/5.v9/2.guide/4.ignoring-localized-routes.md create mode 100644 docs/content/docs/5.v9/3.options/5.domain.md create mode 100644 specs/fixtures/different_domains/pages/localized-route.vue diff --git a/docs/content/docs/2.guide/1.index.md b/docs/content/docs/2.guide/1.index.md index e681b97c2..4d4f02d93 100644 --- a/docs/content/docs/2.guide/1.index.md +++ b/docs/content/docs/2.guide/1.index.md @@ -58,7 +58,7 @@ There are 4 supported strategies that affect how app's routes are generated: With this strategy, your routes won't have a locale prefix added. The locale will be detected & changed without changing the URL. This implies that you have to rely on browser & cookie detection, and implement locale switches by calling the i18n API. ::callout{icon="i-heroicons-light-bulb"} -This strategy doesn't support [Custom paths](/docs/guide/custom-paths) and [Ignore routes](/docs/guide/ignoring-localized-routes) features. +This strategy doesn't support [Custom paths](/docs/guide/custom-paths) and [Ignore routes](/docs/guide/ignoring-localized-routes) features unless you're also using [`differentDomains`](/docs/guide/different-domains). :: ### `prefix_except_default` diff --git a/docs/content/docs/2.guide/3.custom-paths.md b/docs/content/docs/2.guide/3.custom-paths.md index b3484bee2..3649ada44 100644 --- a/docs/content/docs/2.guide/3.custom-paths.md +++ b/docs/content/docs/2.guide/3.custom-paths.md @@ -6,7 +6,7 @@ description: Customize the names of the paths for specific locale. In some cases, you might want to translate URLs in addition to having them prefixed with the locale code. There are 2 ways of configuring custom paths for your [Module configuration](#module-configuration) or your pages [Page component](#page-component). ::callout{icon="i-heroicons-exclamation-triangle" color="amber"} -Custom paths are not supported when using the `no-prefix` [strategy](/docs/guide). +Custom paths are not supported when using the `no_prefix` [strategy](/docs/guide) unless combined with [`differentDomains`](/docs/guide/different-domains). :: ### Module configuration diff --git a/docs/content/docs/2.guide/4.ignoring-localized-routes.md b/docs/content/docs/2.guide/4.ignoring-localized-routes.md index 3feb8e6d5..ea091424f 100644 --- a/docs/content/docs/2.guide/4.ignoring-localized-routes.md +++ b/docs/content/docs/2.guide/4.ignoring-localized-routes.md @@ -4,7 +4,7 @@ description: Customize localized route exclusions per page component. --- ::callout{icon="i-heroicons-exclamation-triangle" color="amber"} -This feature is not supported with the `no-prefix` [strategy](/docs/guide). +This feature is not supported when using the `no_prefix` [strategy](/docs/guide) unless you're also using [`differentDomains`](/docs/guide/different-domains). :: If you'd like some pages to be available in some languages only, you can configure the list of supported languages to override the global settings. The options can be specified within either the page components themselves or globally, within the module configuration. diff --git a/docs/content/docs/3.options/5.domain.md b/docs/content/docs/3.options/5.domain.md index b1eff90c2..fb8741869 100644 --- a/docs/content/docs/3.options/5.domain.md +++ b/docs/content/docs/3.options/5.domain.md @@ -8,4 +8,4 @@ description: Browser locale management options. - type: `boolean` - default: `false` -Set this to `true` when using different domains for each locale. If enabled, no prefix is added to your routes and you MUST configure locales as an array of objects, each containing a `domain` key. Refer to the [Different domains](/docs/guide/different-domains) for more information. +Set this to `true` when using different domains for each locale, with this enabled you MUST configure locales as an array of objects, each containing a `domain` key. Refer to the [Different domains](/docs/guide/different-domains) for more information. diff --git a/docs/content/docs/5.v7/7.custom-paths.md b/docs/content/docs/5.v7/7.custom-paths.md index d53e09cce..dd68a19bb 100644 --- a/docs/content/docs/5.v7/7.custom-paths.md +++ b/docs/content/docs/5.v7/7.custom-paths.md @@ -6,7 +6,7 @@ description: nuxt/i18n v7 custom route paths. In some cases, you might want to translate URLs in addition to having them prefixed with the locale code. There are 2 ways of configuring custom paths for your pages: [in-component options](#in-component-options) or via the [module's configuration](#modules-configuration). ::callout{icon="i-heroicons-light-bulb"} -Custom paths are not supported when using the `no-prefix` [strategy](./strategies). +Custom paths are not supported when using the `no_prefix` [strategy](./strategies). :: ### In-component options diff --git a/docs/content/docs/5.v7/8.ignoring-localized-routes.md b/docs/content/docs/5.v7/8.ignoring-localized-routes.md index 9d2914308..44b8d4ed2 100644 --- a/docs/content/docs/5.v7/8.ignoring-localized-routes.md +++ b/docs/content/docs/5.v7/8.ignoring-localized-routes.md @@ -3,7 +3,7 @@ title: Ignoring Localized Routes --- ::callout{icon="i-heroicons-light-bulb"} -This feature is not supported with the `no-prefix` [strategy](./strategies). +This feature is not supported with the `no_prefix` [strategy](./strategies). :: If you'd like some pages to be available in some languages only, you can configure the list of supported languages to override the global settings. The options can be specified within either the page components themselves or globally, within then module options. diff --git a/docs/content/docs/5.v9/2.guide/1.index.md b/docs/content/docs/5.v9/2.guide/1.index.md new file mode 100644 index 000000000..4d4f02d93 --- /dev/null +++ b/docs/content/docs/5.v9/2.guide/1.index.md @@ -0,0 +1,92 @@ +--- +title: Routing Strategies +description: Nuxt i18n module overrides Nuxt default routes to add locale prefixes to every URL with routing strategies. +--- + +::callout{icon="i-heroicons-light-bulb"} +This feature works under [Nuxt routing](https://nuxt.com/docs/getting-started/routing). It's assumed that you are using the `pages` directory to control routing. +:: + +## Routing + +**Nuxt i18n module** overrides Nuxt default routes to add locale prefixes to every URL (except in `no_prefix` strategy). + +Say your app supports two languages: French and English as the default language, and you have the following pages in your project: + +``` +└── pages + ├── about + │ └── index.vue + └── index.vue +``` + +This would result in the following routes being generated + +```js +[ + { + path: "/", + name: "index___en", + ... + }, + { + path: "/fr/", + name: "index___fr", + ... + }, + { + path: "/about", + name: "about___en", + ... + }, + { + path: "/fr/about", + name: "about___fr", + ... + } +] +``` + +Note that routes for the English version do not have any prefix because it is the default language, see the routing strategies section for more details. + +## Strategies + +There are 4 supported strategies that affect how app's routes are generated: + +### `no_prefix` + +With this strategy, your routes won't have a locale prefix added. The locale will be detected & changed without changing the URL. This implies that you have to rely on browser & cookie detection, and implement locale switches by calling the i18n API. + +::callout{icon="i-heroicons-light-bulb"} +This strategy doesn't support [Custom paths](/docs/guide/custom-paths) and [Ignore routes](/docs/guide/ignoring-localized-routes) features unless you're also using [`differentDomains`](/docs/guide/different-domains). +:: + +### `prefix_except_default` + +Using this strategy, all of your routes will have a locale prefix added except for the default language. + +### `prefix` + +With this strategy, all routes will have a locale prefix. + +### `prefix_and_default` + +This strategy combines both previous strategies behaviours, meaning that you will get URLs with prefixes for every language, but URLs for the default language will also have a non-prefixed version (though the prefixed version will be preferred when `detectBrowserLanguage` is enabled). + +### Configuration + +To configure the strategy, use the `strategy` option. +Make sure that you have a `defaultLocale` defined, especially if using `prefix_except_default`, `prefix_and_default` or `no_prefix` strategy. For other strategies it's also recommended to set this as it will be used as a fallback when attempting to redirect from a 404 page. + +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + // ... + + i18n: { + strategy: 'prefix_except_default', + defaultLocale: 'en' + } + + // ... +}) +``` diff --git a/docs/content/docs/5.v9/2.guide/3.custom-paths.md b/docs/content/docs/5.v9/2.guide/3.custom-paths.md new file mode 100644 index 000000000..a403f8e64 --- /dev/null +++ b/docs/content/docs/5.v9/2.guide/3.custom-paths.md @@ -0,0 +1,243 @@ +--- +title: Custom Route Paths +description: Customize the names of the paths for specific locale. +--- + +In some cases, you might want to translate URLs in addition to having them prefixed with the locale code. There are two methods of configuring custom paths, through [Module configuration](#module-configuration) or from within each [Page component](#page-component). + +Which method is used is configured by setting the [`customRoutes` options](/docs/options/routing#customroutes) this is set to `'page'` by default. Using both methods at the same time is not possible. + +::callout{icon="i-heroicons-exclamation-triangle" color="amber"} +Custom paths are not supported when using the `no_prefix` [strategy](/docs/guide) unless combined with [`differentDomains`](/docs/guide/different-domains). +:: + +### Module configuration + +Make sure you set the `customRoutes` option to `config` and add your custom paths in the `pages` option: + +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + i18n: { + customRoutes: 'config', // disable custom route with page components + pages: { + about: { + en: '/about-us', // -> accessible at /about-us (no prefix since it's the default locale) + fr: '/a-propos', // -> accessible at /fr/a-propos + es: '/sobre' // -> accessible at /es/sobre + } + } + } +}) +``` + +Note that each key within the `pages` object should **correspond to the relative file-based path (excluding `.vue` file extension) of the route within your `pages/` directory excluding the leading `/`**. + +Customized route paths **must start with a `/`** and **not include the locale prefix**. + +You can now use the `localePath` function or the `` component but be sure to use named routes. For example route `/services/advanced` should be `services-advanced`: + +```vue + + + +``` + +Or: + +```vue + + + +``` + +::callout{icon="i-heroicons-exclamation-triangle" color="amber"} +Specifying a path to `localePath` is not supported, currently. +:: + +#### Example 1: Basic URL localization + +You have some routes with the following `pages` directory: + +``` +pages/ +├── about.vue +├── me.vue +├── services/ +├──── index.vue +├──── advanced.vue +``` + +You would need to set up your `pages` property as follows: + +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + i18n: { + customRoutes: 'config', + pages: { + about: { + fr: '/a-propos', + }, + me: { + fr: '/moi', + }, + 'services/index': { + fr: '/offres', + }, + 'services/advanced': { + fr: '/offres/avancee', + } + } + } +}) +``` + +If you want customize the URL of a static vue file, you should use the file's name. +If the view is in a sub-directory you should use folder name and vue files name with trailing slash. + +::callout{icon="i-heroicons-exclamation-triangle" color="amber"} +All URLs must start with `/` +:: + +#### Example 2: Localize the part of URL + +You have some routes with the following `pages` directory: + +``` +pages/ +├── about.vue +├── services/ +├──── coaching.vue +├──── index.vue +├──── development/ +├────── app.vue +├────── website.vue +├────── index.vue +``` + +You would need to set up your `pages` property as follows: + +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + i18n: { + customRoutes: 'config', + pages: { + about: { + fr: '/a-propos' + }, + 'services/index': { + fr: '/offres' + }, + 'services/development/index': { + fr: '/offres/developement' + }, + 'services/development/app': { + fr: '/offres/developement/app' + }, + 'services/development/website': { + fr: '/offres/developement/site-web' + }, + 'services/coaching': { + fr: '/offres/formation' + } + } + } +}) +``` + +If a custom path is missing for one of the locales, the `defaultLocale` custom path is used, if set. + +#### Example 3: Dynamic Routes + +Say you have some dynamic routes like: + +``` +pages/ +├── blog/ +├──── [date]/ +├────── [slug].vue +``` + +Here's how you would configure these particular pages in the configuration: + +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + i18n: { + customRoutes: 'config', + pages: { + 'blog/[date]/[slug]': { + // params need to be put back here as you would with Nuxt Dynamic Routes + // https://nuxt.com/docs/guide/directory-structure/pages#dynamic-routes + ja: '/blog/tech/[date]/[slug]' + // ... + } + } + } +}) +``` + +### Page component + +::callout{icon="i-heroicons-exclamation-triangle" color="amber" title="notice"} +Note for those updating to `v8.0.1` or higher +:br :br +Path parameters parsing has been changed to match that of [Nuxt 3](https://nuxt.com/docs/guide/directory-structure/pages#dynamic-routes), you will have to update your custom paths (e.g. `/example/:param` should now be `/example/[param]`) +:: + +You can use the `defineI18nRoute` compiler macro to set custom paths for each page component. + +```html {}[pages/about.vue] + +``` + +To configure a custom path for a dynamic route, you need to use it in double square brackets in the paths similarly to how you would do it in [Nuxt Dynamic Routes](https://nuxt.com/docs/guide/directory-structure/pages#dynamic-routes): + +```html {}[pages/articles/[name].vue] + +``` + +::callout{icon="i-heroicons-light-bulb"} +`defineI18nRoute` compiler macro is tree-shaked out at build time and is not included in the dist files. +:: + + +### `definePageMeta({ name: '...' })` caveat + +By default Nuxt overwrites generated route values at build time which breaks custom named routes (setting `name` with `definePageMeta`) when resolving localized paths. + +Nuxt v3.10 introduced the experimental feature [`scanPageMeta`](https://nuxt.com/docs/guide/going-further/experimental-features#scanpagemeta), this needs to be enabled for custom named routes to work when using Nuxt I18n. + +This experimental feature can be enabled as shown here: + +```typescript {}[nuxt.config.ts] +export default defineNuxtConfig({ + experimental: { + scanPageMeta: true, + } +}) +``` diff --git a/docs/content/docs/5.v9/2.guide/4.ignoring-localized-routes.md b/docs/content/docs/5.v9/2.guide/4.ignoring-localized-routes.md new file mode 100644 index 000000000..ea091424f --- /dev/null +++ b/docs/content/docs/5.v9/2.guide/4.ignoring-localized-routes.md @@ -0,0 +1,56 @@ +--- +title: Ignoring Localized Routes +description: Customize localized route exclusions per page component. +--- + +::callout{icon="i-heroicons-exclamation-triangle" color="amber"} +This feature is not supported when using the `no_prefix` [strategy](/docs/guide) unless you're also using [`differentDomains`](/docs/guide/different-domains). +:: + +If you'd like some pages to be available in some languages only, you can configure the list of supported languages to override the global settings. The options can be specified within either the page components themselves or globally, within the module configuration. + +### Pick localized routes + +::code-group + +```vue [pages/about.vue] + +``` + +```ts [nuxt.config.ts] +i18n: { + customRoutes: false, + pages: { + about: { + en: false, + } + } +} +``` + +:: + +### Disable localized routes + +::code-group + +```vue [pages/about.vue] + +``` + +```ts {}[nuxt.config.ts] +i18n: { + customRoutes: 'config', + pages: { + about: false + } +} +``` + +:: diff --git a/docs/content/docs/5.v9/3.options/5.domain.md b/docs/content/docs/5.v9/3.options/5.domain.md new file mode 100644 index 000000000..fb8741869 --- /dev/null +++ b/docs/content/docs/5.v9/3.options/5.domain.md @@ -0,0 +1,11 @@ +--- +title: Domain +description: Browser locale management options. +--- + +## `differentDomains` + +- type: `boolean` +- default: `false` + +Set this to `true` when using different domains for each locale, with this enabled you MUST configure locales as an array of objects, each containing a `domain` key. Refer to the [Different domains](/docs/guide/different-domains) for more information. diff --git a/specs/different_domains/different_domains.spec.ts b/specs/different_domains/different_domains.spec.ts index 9ae2ab2fe..cfe8fd694 100644 --- a/specs/different_domains/different_domains.spec.ts +++ b/specs/different_domains/different_domains.spec.ts @@ -45,6 +45,15 @@ await setup({ strategy: 'no_prefix', detectBrowserLanguage: { useCookie: true + }, + customRoutes: 'config', + pages: { + 'localized-route': { + en: '/localized-in-english', + fr: '/localized-in-french', + ja: '/localized-in-japanese', + nl: '/localized-in-dutch' + } } } } @@ -152,3 +161,27 @@ test('(#2374) detect with x-forwarded-host on server', async () => { expect(dom.querySelector('#welcome-text').textContent).toEqual('Bienvenue') }) + +test("supports custom routes with `strategy: 'no_prefix'`", async () => { + const res = await undiciRequest('/localized-in-french', { + headers: { + host: 'fr.nuxt-app.localhost' + } + }) + const resBody = await res.body.text() + const dom = getDom(resBody) + + // `en` link uses project domain configuration, overrides layer + expect(dom.querySelector('#switch-locale-path-usages .switch-to-en a').getAttribute('href')).toEqual( + `http://en.nuxt-app.localhost/localized-in-english` + ) + + // `nl` link uses layer domain configuration + expect(dom.querySelector('#switch-locale-path-usages .switch-to-nl a').getAttribute('href')).toEqual( + `http://layer-nl.example.com/localized-in-dutch` + ) + // `ja` link uses layer domain configuration + expect(dom.querySelector('#switch-locale-path-usages .switch-to-ja a').getAttribute('href')).toEqual( + `http://layer-ja.example.com/localized-in-japanese` + ) +}) diff --git a/specs/fixtures/different_domains/pages/localized-route.vue b/specs/fixtures/different_domains/pages/localized-route.vue new file mode 100644 index 000000000..398791399 --- /dev/null +++ b/specs/fixtures/different_domains/pages/localized-route.vue @@ -0,0 +1,11 @@ + + + diff --git a/src/module.ts b/src/module.ts index e8571fbe5..cdd37238b 100644 --- a/src/module.ts +++ b/src/module.ts @@ -195,7 +195,7 @@ export default defineNuxtModule({ * setup nuxt/pages */ - if (options.strategy !== 'no_prefix' && localeCodes.length) { + if (localeCodes.length) { setupPages(options, nuxt) } diff --git a/src/pages.ts b/src/pages.ts index 8d0a212b6..a7bbe9f1b 100644 --- a/src/pages.ts +++ b/src/pages.ts @@ -70,8 +70,12 @@ export function setupPages(options: Required, nuxt: Nuxt) { localizedPages.unshift(indexPage) } - pages.splice(0, pages.length) - pages.unshift(...localizedPages) + // do not mutate pages if localization is skipped + if (pages !== localizedPages) { + pages.splice(0, pages.length) + pages.unshift(...localizedPages) + } + debug('... made pages', pages) }) } diff --git a/src/routing.ts b/src/routing.ts index ab0be694a..25e441d55 100644 --- a/src/routing.ts +++ b/src/routing.ts @@ -33,6 +33,7 @@ export function prefixLocalizedRoute( return ( !extra && !isChildWithRelativePath && + options.strategy !== 'no_prefix' && // skip default locale if strategy is 'prefix_except_default' !(isDefaultLocale && options.strategy === 'prefix_except_default') ) @@ -51,6 +52,31 @@ export type LocalizeRoutesParams = MarkRequired< optionsResolver?: RouteOptionsResolver } +export function shouldLocalizeRoutes(options: NuxtI18nOptions) { + if (options.strategy === 'no_prefix') { + // no_prefix is only supported when using a separate domain per locale + if (!options.differentDomains) return false + + // check if domains are used multiple times + const domains = new Set() + for (const locale of options.locales || []) { + if (typeof locale === 'string') continue + if (locale.domain) { + if (domains.has(locale.domain)) { + console.error( + `Cannot use \`strategy: no_prefix\` when using multiple locales on the same domain - found multiple entries with ${locale.domain}` + ) + return false + } + + domains.add(locale.domain) + } + } + } + + return true +} + type LocalizedRoute = NuxtPage & { locale: Locale; parent: NuxtPage | undefined } type LocalizeRouteParams = { /** @@ -82,9 +108,7 @@ type LocalizeRouteParams = { * @public */ export function localizeRoutes(routes: NuxtPage[], options: LocalizeRoutesParams): NuxtPage[] { - if (options.strategy === 'no_prefix') { - return routes - } + if (!shouldLocalizeRoutes(options)) return routes let defaultLocales = [options.defaultLocale ?? ''] if (options.differentDomains) { diff --git a/src/runtime/routing/compatibles/routing.ts b/src/runtime/routing/compatibles/routing.ts index d12ccafa0..5d1bdad3b 100644 --- a/src/runtime/routing/compatibles/routing.ts +++ b/src/runtime/routing/compatibles/routing.ts @@ -131,8 +131,7 @@ export function localeLocation( export function resolveRoute(common: CommonComposableOptions, route: RouteLocationRaw, locale: Locale | undefined) { const { router, i18n } = common const _locale = locale || getLocale(i18n) - const { routesNameSeparator, defaultLocale, defaultLocaleRouteNameSuffix, strategy, trailingSlash } = - common.runtimeConfig.public.i18n + const { defaultLocale, strategy, trailingSlash } = common.runtimeConfig.public.i18n const prefixable = extendPrefixable(common.runtimeConfig) // if route parameter is a string, check if it's a path or name of route. let _route: RouteLocationPathRaw | RouteLocationNamedRaw @@ -162,12 +161,7 @@ export function resolveRoute(common: CommonComposableOptions, route: RouteLocati const resolvedRouteName = getRouteBaseName(common, resolvedRoute) if (isString(resolvedRouteName)) { localizedRoute = { - name: getLocaleRouteName(resolvedRouteName, _locale, { - defaultLocale, - strategy, - routesNameSeparator, - defaultLocaleRouteNameSuffix - }), + name: getLocaleRouteName(resolvedRouteName, _locale, common.runtimeConfig.public.i18n), // @ts-ignore // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- FIXME params: resolvedRoute.params, @@ -193,12 +187,7 @@ export function resolveRoute(common: CommonComposableOptions, route: RouteLocati localizedRoute.name = getRouteBaseName(common, router.currentRoute.value) } - localizedRoute.name = getLocaleRouteName(localizedRoute.name, _locale, { - defaultLocale, - strategy, - routesNameSeparator, - defaultLocaleRouteNameSuffix - }) + localizedRoute.name = getLocaleRouteName(localizedRoute.name, _locale, common.runtimeConfig.public.i18n) } try { diff --git a/src/runtime/routing/utils.ts b/src/runtime/routing/utils.ts index cdcf3e9be..bd787f3a2 100644 --- a/src/runtime/routing/utils.ts +++ b/src/runtime/routing/utils.ts @@ -102,10 +102,18 @@ export function getLocaleRouteName( defaultLocale, strategy, routesNameSeparator, - defaultLocaleRouteNameSuffix - }: { defaultLocale: string; strategy: Strategies; routesNameSeparator: string; defaultLocaleRouteNameSuffix: string } + defaultLocaleRouteNameSuffix, + differentDomains + }: { + defaultLocale: string + strategy: Strategies + routesNameSeparator: string + defaultLocaleRouteNameSuffix: string + differentDomains: boolean + } ) { - let name = getRouteName(routeName) + (strategy === 'no_prefix' ? '' : routesNameSeparator + locale) + const localizedRoutes = strategy !== 'no_prefix' || differentDomains + let name = getRouteName(routeName) + (localizedRoutes ? routesNameSeparator + locale : '') if (locale === defaultLocale && strategy === 'prefix_and_default') { name += routesNameSeparator + defaultLocaleRouteNameSuffix } diff --git a/test/routing-utils.test.ts b/test/routing-utils.test.ts index 38ba30f96..651f6e891 100644 --- a/test/routing-utils.test.ts +++ b/test/routing-utils.test.ts @@ -53,7 +53,8 @@ describe('getLocaleRouteName', () => { defaultLocale: 'en', strategy: 'prefix_and_default', routesNameSeparator: '___', - defaultLocaleRouteNameSuffix: 'default' + defaultLocaleRouteNameSuffix: 'default', + differentDomains: false }), 'route1___en___default' ) @@ -67,7 +68,8 @@ describe('getLocaleRouteName', () => { defaultLocale: 'en', strategy: 'prefix_except_default', routesNameSeparator: '___', - defaultLocaleRouteNameSuffix: 'default' + defaultLocaleRouteNameSuffix: 'default', + differentDomains: false }), 'route1___en' ) @@ -81,7 +83,8 @@ describe('getLocaleRouteName', () => { defaultLocale: 'en', strategy: 'no_prefix', routesNameSeparator: '___', - defaultLocaleRouteNameSuffix: 'default' + defaultLocaleRouteNameSuffix: 'default', + differentDomains: false }), 'route1' ) @@ -96,7 +99,8 @@ describe('getLocaleRouteName', () => { defaultLocale: 'en', strategy: 'prefix_and_default', routesNameSeparator: '___', - defaultLocaleRouteNameSuffix: 'default' + defaultLocaleRouteNameSuffix: 'default', + differentDomains: false }), '(null)___en___default' )