nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.74k stars 480 forks source link

Expose a way to get a route base #1897

Open trandaison opened 1 year ago

trandaison commented 1 year ago

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 similar getRouteBase(localizedRoute?: Route | RouteLocationNormalizedLoaded) function to un-localize a route.

Additional information

Final checks

CaptainFalcon92 commented 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

BobbieGoede commented 1 year ago

Can you give an example of what you mean exactly? I may be misunderstanding but your description sounds similar to the useLocaleRoute composable.

mojabyte commented 1 year ago

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',
  ...
}
trandaison commented 1 year ago

@HassanMojab exactly!

A composable that return base route from localized route.

BobbieGoede commented 1 year ago

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.

mojabyte commented 1 year ago

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"
/>
BobbieGoede commented 1 month ago

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?

trandaison commented 1 month ago

@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,
  })
}
BobbieGoede commented 1 month ago

@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.