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

Add localePath and getRouteBaseName to Nuxt's app #25

Merged

Conversation

RomainLK
Copy link

Fix #24 . Vue Mixin through plugins are already too far in the lifecycle to be registered on Nuxt's app. The app is in fact, already created when the plugins are registered.

This PR extracts localePath and getRouteBaseName into factories so as to configure where to get $router, $route, $i18n and registers these 2 methods both in the mixin and on app.

( I don't believe anyone will use switchLocalePath from app... )

@paulgv paulgv merged commit 3080a04 into nuxt-modules:develop Feb 20, 2018
@paulgv
Copy link
Collaborator

paulgv commented Feb 20, 2018

Awesome thank you @RomainLK !

@felixdenoix
Copy link

felixdenoix commented Mar 9, 2018

Well in fact the switchLocalePath could be quite useful when trying to detect the user locale in the store nuxtServerInit hook in order to redirect him directly to the page with the correct user locale !
That could work like so

user goes to ==> foo.com
website default locale set to 'en'
user locale set to 'fr'
user redirected to ==> foo.com/fr   (if 'fr' is configured)
(user can switch to an other language afterwards if he wishes)

This way you could avoid a redirection on app loading

  async nuxtServerInit({ commit }, { req, app, redirect }) {
    let reqLocale = req.headers['accept-language']
      ? req.headers['accept-language'].substring(0, 2)
      : 'fr'

    let currentLocale = app.i18n.locale

    console.log('currentLocale', currentLocale)
    console.log('reqLocale', reqLocale)

    if (currentLocale !== reqLocale) {
      app.i18n.locale = reqLocale
      return redirect('/' + reqLocale)  <=== [avoid this redirection]
    }

    (........)
  }

Regarding of this use case don't you think, @RomainLK, that it could be interesting to add switchLocalePathto the app ?

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

Successfully merging this pull request may close these issues.

3 participants