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

Support passing path to localePath for easier resolving of dynamic routes #215

Closed
rchl opened this issue Feb 8, 2019 · 6 comments
Closed

Comments

@rchl
Copy link
Collaborator

rchl commented Feb 8, 2019

What problem does this feature solve?

localePath takes either string or object.
String is expected to be route's internal name.
Object, with current implementation of localePath, will be passed to VueRouter's resolve function but the name property will get ___[locale] appended to it.

The problem is that the object variant expects name to be set, otherwise it will create name value like undefined___[locale] and route will not match.

Given that it won't work to pass an object with just {path: '/about'}, for dynamic routes like pages/_.vue, we must instead do an awkward localePath call like localePath({name: 'all', params: {0: 'about'}}) (haven't really tested but I think it should work). This is IMO too cumbersome and what's more, it requires knowledge of internal route's name. It's basically not user friendly.

What does the proposed changes look like?

It should be possible to call code below to get localized path to dynamic route

localePath({path: '/about'});

and let router figure out the path.

Note that with PREFIX_AND_DEFAULT mode, it should ideally only return path with locale prefix added, if resolving to non-default language OR when current route includes locale prefix.

So (with en being default locale):

  • /about >> localePath({path: '/about'}) >> /about
  • /en/about >> localePath({path: '/about'}) >> /en/about
  • /fr/about >> localePath({path: '/about'}) >> /fr/about
This feature request is available on Nuxt community (#c201)
@ghost ghost added the cmty:feature-request label Feb 8, 2019
@smesterheide
Copy link

smesterheide commented Mar 13, 2019

I found this information to be very useful for decoupling my CMS and serving content with Nuxt. I'm using the slightly less awkward syntax {name: 'all', params: ['about']}, but support for the path property is definitely preferable.

I'd like to add that you must add your default locale last to the locales definition when using the prefix_except_default strategy, eg:

{
  modules: [
    ['nuxt-i18n', {
      locales: [
        { code: 'en', name: 'English', iso: 'en-US' },
        { code: 'de', name: 'Deutsch', iso: 'de-DE' } // <-- Add this last
      ],
      defaultLocale: 'de'
    }]
}

So you get the following routes generated for your _.vue page in this particular order:

[{
  path: '/en/*',
  name: 'all___en'
},
{
  path: '/*',
  name: 'all___de'
}]

Else your localized dynamic route never matches.

@rchl
Copy link
Collaborator Author

rchl commented Mar 14, 2019

I'd like to add that you must add your default locale last...

Indeed. It's #152

@mornir
Copy link

mornir commented Mar 31, 2019

Yes, that would be really nice to have!

@stale
Copy link

stale bot commented Jun 13, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 13, 2019
@rchl
Copy link
Collaborator Author

rchl commented Jun 13, 2019

Makes sense to support it IMO.

@stale stale bot removed the wontfix label Jun 13, 2019
@stale
Copy link

stale bot commented Aug 12, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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

3 participants