Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translatable route param slugs #3409

Open
MickL opened this issue Mar 13, 2025 · 0 comments
Open

Translatable route param slugs #3409

MickL opened this issue Mar 13, 2025 · 0 comments

Comments

@MickL
Copy link
Contributor

MickL commented Mar 13, 2025

Describe the feature

Exemplary lets imagine the route /products/[type] should generate the following routes:

- en: '/products/shoes'
- de: '/produkte/schuhe'
- fr: '/produits/chaussures'

But all localized slugs need to be mapped to a fixed value (most likely an enum), In this example this could be ProductType.Shoe.

To archive this we need three things:

1. Map localized param (slug) to enum

Locale + Slug -> Fixed value. Basically this is a reverse translation. E.g.:

- en: '/products/shoes' -> type is ProductType.Shoe,
- de: '/produkte/schuhe' -> type is ProductType.Shoe,
- fr: '/produits/chaussures' -> type is ProductType.Shoe,
- fr: '/produits/shoes' -> type is undefined (or throw error),

This is needed e.g. for making a fetch request to the API.

2. For the current route params, tell @nuxtjs/i18n about each other localized slug

This is needed for language switching. With the example above, the following needs to be generated:

const setI18nParams = useSetI18nParams();
setI18nParams({
   en: { type: 'shoes' },
   de: { type: 'schuhe' },
   fr: { type: 'chaussures' },
});

3. Generate explicit links
When wanting to place a link, we need to get the localized slug for the current locale. E.g. with the example above, if the current locale is de then the following should be generated:

<NuxtLinkLocale :to="{ name: 'products-type', params: { type: 'schuhe' } }">
   Schuhe
</NuxtLinkLocale>

Additional information

I dont have the time to get into @nuxtjs/i18n to implement this BUT I was thinking about how to solve this for some time now and I made a sandbox where I have a fully working example with the three needed functions. I dont know if this is the best possible way (if not PLEASE make suggestions!) but maybe this could be a good starting point for @nuxtjs/i18n contributors to implement this feature.

Sandbox Repo: https://github.com/MickL/nuxti18n-localized-slugs
Sandbox Stackblitz: https://stackblitz.com/~/github.com/MickL/nuxti18n-localized-slugs?file=app/pages/products/%5Btype%5D.vue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant