Skip to content

Commit

Permalink
add routesNameSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
s00d committed Apr 28, 2024
1 parent 202bd25 commit ca666c8
Show file tree
Hide file tree
Showing 12 changed files with 1,280 additions and 49 deletions.
8 changes: 8 additions & 0 deletions docs/content/docs/3.options/2.routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ Internal suffix added to generated route names for default locale, if strategy i

Internal separator used for generated route names for each locale. You shouldn't need to change this.

## `routesNameSuffix`

- type: `string`
- default: `'locale'`

Internal suffix used for generated route names for each locale. You shouldn't need to change this.


## `rootRedirect`

- type: `string` or `object` or `null`
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const DEFAULT_OPTIONS = {
defaultLocale: '',
defaultDirection: 'ltr',
routesNameSeparator: '___',
routesNameSuffix: 'locale',
trailingSlash: false,
defaultLocaleRouteNameSuffix: 'default',
strategy: STRATEGY_PREFIX_EXCEPT_DEFAULT,
Expand Down
7 changes: 7 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export default defineNuxtModule<NuxtI18nOptions>({
lazy: options.lazy,
rootRedirect: options.rootRedirect,
routesNameSeparator: options.routesNameSeparator,
routesNameSuffix: options.routesNameSuffix,
defaultLocaleRouteNameSuffix: options.defaultLocaleRouteNameSuffix,
skipSettingLocaleOnNavigate: options.skipSettingLocaleOnNavigate,
differentDomains: options.differentDomains,
Expand Down Expand Up @@ -441,6 +442,12 @@ export interface ModulePublicRuntimeConfig {
* @internal
*/
routesNameSeparator: Required<NuxtI18nOptions>['routesNameSeparator']
/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
routesNameSuffix: Required<NuxtI18nOptions>['routesNameSuffix']
/**
* Overwritten at build time, used to pass generated options to runtime
*
Expand Down
11 changes: 7 additions & 4 deletions src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function adjustRoutePathForTrailingSlash(localized: LocalizedRoute, trailingSlas

export type LocalizeRoutesParams = MarkRequired<
NuxtI18nOptions,
'strategy' | 'locales' | 'routesNameSeparator' | 'trailingSlash' | 'defaultLocaleRouteNameSuffix'
'strategy' | 'locales' | 'routesNameSeparator' | 'routesNameSuffix' | 'trailingSlash' | 'defaultLocaleRouteNameSuffix'
> & {
includeUnprefixedFallback?: boolean
optionsResolver?: RouteOptionsResolver
Expand Down Expand Up @@ -171,8 +171,11 @@ export function localizeRoutes(routes: NuxtPage[], options: LocalizeRoutesParams
}

if (combinedLocalized.name) {
combinedLocalized.name = combinedLocalized.name.replace(`${options.routesNameSeparator}locale`, '')
combinedLocalized.name += `${options.routesNameSeparator}locale`
combinedLocalized.name = combinedLocalized.name.replace(
`${options.routesNameSeparator}${options.routesNameSuffix}`,
''
)
combinedLocalized.name += `${options.routesNameSeparator}${options.routesNameSuffix}`
}
combinedLocalized.meta = { ...combinedLocalized.meta, ...{ locale: true } }

Expand All @@ -199,7 +202,7 @@ export function localizeRoutes(routes: NuxtPage[], options: LocalizeRoutesParams
prefix = ''
subLocalized.name = `${route.name}${options.routesNameSeparator}${locale}`
} else {
subLocalized.name = `${route.name}${options.routesNameSeparator}locale${options.routesNameSeparator}${locale}`
subLocalized.name = `${route.name}${options.routesNameSeparator}${options.routesNameSuffix}${options.routesNameSeparator}${locale}`
}
subLocalized.path = `${prefix}${componentOptions.paths[locale]}`
subLocalized.meta = { ...subLocalized.meta, ...{ locale: true } }
Expand Down
23 changes: 19 additions & 4 deletions src/runtime/routing/compatibles/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export function resolveRoute(common: CommonComposableOptions, route: RouteLocati
if (!_locale || _locale === 'undefined') {
return null
}
const { strategy, routesNameSeparator, trailingSlash, customPages } = common.runtimeConfig.public.i18n
const { strategy, routesNameSeparator, routesNameSuffix, trailingSlash, customPages } =
common.runtimeConfig.public.i18n
let { defaultLocale } = common.runtimeConfig.public.i18n

const lang = [...normalizedLocales].find(locale => locale.code === _locale)
Expand Down Expand Up @@ -184,7 +185,14 @@ export function resolveRoute(common: CommonComposableOptions, route: RouteLocati
const resolvedRouteName = getRouteBaseName(common, resolvedRoute)
if (isString(resolvedRouteName)) {
localizedRoute = {
name: getLocaleRouteName(resolvedRouteName, _locale, defaultLocale, routesNameSeparator, strategy),
name: getLocaleRouteName(
resolvedRouteName,
_locale,
defaultLocale,
routesNameSeparator,
routesNameSuffix,
strategy
),
// @ts-ignore
params: resolvedRoute.params,
query: resolvedRoute.query,
Expand Down Expand Up @@ -218,7 +226,14 @@ export function resolveRoute(common: CommonComposableOptions, route: RouteLocati
localizedRoute.name = getRouteBaseName(common, router.currentRoute.value)
}

localizedRoute.name = getLocaleRouteName(localizedRoute.name, _locale, defaultLocale, routesNameSeparator, strategy)
localizedRoute.name = getLocaleRouteName(
localizedRoute.name,
_locale,
defaultLocale,
routesNameSeparator,
routesNameSuffix,
strategy
)
if ((defaultLocale !== _locale && strategy !== 'no_prefix') || strategy === 'prefix') {
localizedRoute.params = { ...localizedRoute.params, ...{ locale: _locale } }
}
Expand Down Expand Up @@ -261,7 +276,7 @@ export function resolveRoute(common: CommonComposableOptions, route: RouteLocati
}
}
if (!checker && localizedRoute.name) {
localizedRoute.name = localizedRoute.name.toString().replace(`${routesNameSeparator}locale`, '')
localizedRoute.name = localizedRoute.name.toString().replace(`${routesNameSeparator}${routesNameSuffix}`, '')
resolvedRoute = router.resolve(localizedRoute)
}

Expand Down
5 changes: 3 additions & 2 deletions src/runtime/routing/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ export function getLocaleRouteName(
locale: Locale,
defaultLocale: string,
routesNameSeparator: string,
routesNameSuffix: string,
strategy: string
) {
let name = getRouteName(routeName)
.replace(`${routesNameSeparator}${locale}`, '')
.replace(`${routesNameSeparator}locale`, '')
.replace(`${routesNameSeparator}${routesNameSuffix}`, '')
if ((locale !== defaultLocale && strategy !== 'no_prefix') || strategy === 'prefix') {
name += `${routesNameSeparator}locale`
name += `${routesNameSeparator}${routesNameSuffix}`
}
return name
}
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ export type NuxtI18nOptions<
* @defaultValue '___'
*/
routesNameSeparator?: string
/**
* Suffix added to the generated route names for each locale to distinguish them based on language or regional settings.
* This suffix is appended to the base route name to create a unique name for each localized version of a route.
*
* @defaultValue 'locale'
*/
routesNameSuffix?: string
/**
* Internal suffix added to generated route names for default locale
*
Expand Down

0 comments on commit ca666c8

Please sign in to comment.