Replies: 2 comments 4 replies
-
By the way, huge thank you for making this module! I just realised first commit was in august. I'm impressed by the quality and completeness of what you've achieved on such a tight timeframe. |
Beta Was this translation helpful? Give feedback.
-
Hi, to implement something like this with multiple levels in your localized URLs, it's best to define two levels of parameters in the route, for example: Here’s an example of how you can do this: // in pages/news/[key]/[slug].vue
const { $switchLocaleRoute, $setI18nRouteParams, $defineI18nRoute } = useI18n();
// OR
const { $switchLocaleRoute, $setI18nRouteParams, $defineI18nRoute } = useNuxtApp();
$defineI18nRoute({
localeRoutes: {
en: '/news/:key/:slug()',
fr: '/nouvelles/:key/:slug()',
de: '/Nachricht/:key/:slug()',
},
});
const { data: news } = await useAsyncData(`news-${params.key}-${params.slug}`, async () => {
let response = await $fetch("/api/getNews", {
query: {
key: params.key,
slug: params.slug,
},
});
if (response?.localeSlugs) {
response.localeSlugs = {
en: {
key: 'world',
slug: '1-first-article',
},
fr: {
key: 'monde',
slug: '1-premier-article',
},
de: {
key: 'welt',
slug: '1-erster-Artikel',
},
};
$setI18nRouteParams(response?.localeSlugs);
}
return response;
});
$switchLocalePath('fr') // === 'fr/nouvelles/monde/1-premier-article'
$switchLocalePath('de') // === 'de/Nachricht/welt/1-erster-Artikel' In this example, we handle two dynamic segments ( This approach will allow you to manage URLs like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
For declaring custom localised URLs, we have this exemple from the docs:
Now what if we need to match several levels, like
fr/nouvelles/monde/1-premier-article
? It's a very common case (news subcategories, e-commerce…).Nuxt handles this out of the box with
[...slug]
, but I couldn't find the matching way withdefineI18nRoute
/setI18nRouteParams
.Beta Was this translation helpful? Give feedback.
All reactions