Open trandaison opened 1 year ago
I found this making my previous comment useless: I think this does what we want
https://v8.i18n.nuxtjs.org/api/composables/#useroutebasename
Can you give an example of what you mean exactly? I may be misunderstanding but your description sounds similar to the useLocaleRoute composable.
for example, if the route object is something like this:
{
fullPath: '/es/about',
name: 'about___es',
path: '/es/about',
href: '/es/about',
...
}
then the getRouteBase
method should return the unlocalized version of the given route object, that is:
{
fullPath: '/about',
name: 'about',
path: '/about',
href: '/about',
...
}
@HassanMojab exactly!
A composable that return base route from localized route.
Could you give an example of a use case for this functionality? Technically there are many situations in which the unlocalized routes do not exist when using Nuxt I18n.
In my case, I use the ContectDoc
component from the nuxt-content
package and want to query the correct md file from the current path. Since I use locale-based routing, I need a way to get the base path from the locale path to pass the ContentDoc
:
<ContentDoc
:query="{ where: [{ _locale: locale }] }"
:path="routeBasePath"
/>
The use cases of this feature request is still unclear to me and the implementation gets difficult when routes are localized.
If an /about-us
page is localized to /nl/over-ons
in Dutch, would this composable return an object with the localized values without the prefix?
@BobbieGoede Will this work? Just an idea, I'm not sure if it works
function getRouteBase(route?: Route | RouteLocationNormalizedLoaded) {
const _route = route ?? useRoute();
const router = useRouter();
return router.resolve({
name: getRouteBaseName(_route),
query: _route.query,
params: _route.params,
hash: _route.hash,
})
}
@trandaison That approach won't work because route names without the i18n suffix don't exist when using this module, meaning the route won't resolve correctly, if at all.
Please test it on your side. Since the use case is still unclear to me, I can't determine if the outcome is what you need 🤔. As of now, I’m hesitant to implement this because it seems like a niche use case.
Describe the feature
We already have
getRouteBaseName()
to get the base name of current (or given) route. It would be useful if we have a similargetRouteBase(localizedRoute?: Route | RouteLocationNormalizedLoaded)
function to un-localize a route.Additional information
Final checks