I18n-module: detectBrowserLanguage Redirect Alternate URL Name

Created on 15 Jun 2020  ·  9Comments  ·  Source: nuxt-community/i18n-module

Hey,

I build my setup in the way that every page also has its path in its language.
So its not simply changing /en/[page-name] to /de/[page-name]/,
but also /en/[page-name-en] to /de/[page-name-de]

Is it somehow possible to let the module know which is the alternate url?
I already added meta and link language tags, but this does not seem to get honored in this decision.

Best,
F

question❓

All 9 comments

Yes it is, this feature is documented in the documentation Routing - Custom paths 😃

That actually does not work for me.
_All_ my routes are dynamic, so at the point in the lifecycle where I have to add the alternate page paths I do not know about them yet. I would need the browserRedirect to fire after the asyncData method, because only then I have all the information I need to build the alternate page paths. Best would be to populate the paths from within the asyncData method, is that possible?
Additionally I cannot find the browserRedirect to fire beforeLanguageSwitch and onLanguageSwitched — I tried to log it but it does not show up in the console, is that correct?

Custom paths in nuxt-i18n are build-time based. So you have to decide the paths at build time already. This affects the routes that are generated.

If you only know your routes dynamically at runtime then you have to create your own, custom solution for that.

Something like having a "catch-all" route (_.vue) that will decide the locale to use. nuxt-i18n will do the initial redirect to /en/foo or /de/foo but then you have to have your own logic to redirect to the locale-specific path.

Yes, I already use a catch-all route.
So what you suggest is that I should disable the nuxt-i18n browser redirection and build my own?

At the moment my solution is to let nuxt-i18n do the redirect, but if no page data can be loaded from my asyncData method I search for language siblings and will redirect the user if found.

So my question was aiming if there is a more easy way, eg tell nuxt-i18n the paths async and delay the redirect until they are set.

So what you suggest is that I should disable the nuxt-i18n browser redirection and build my own?

No, unless it gets in the way.
The way you are doing it now (utilizing nuxt-i18n redirect + adjusting manually) is what I had in mind and it probably makes the most sense.

Okay, thanks for your feedback!
Unless anybody else want to add something here or make a suggestion I will close this thread.

BTW. I've just recalled that there exists this functionality: https://nuxt-community.github.io/nuxt-i18n/lang-switcher.html#dynamic-route-parameters

BTW. I've just recalled that there exists this functionality: https://nuxt-community.github.io/nuxt-i18n/lang-switcher.html#dynamic-route-parameters

Link doesn't work

Documentation has moved.
https://i18n.nuxtjs.org/lang-switcher.html#dynamic-route-parameters

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johnboylesingfield picture johnboylesingfield  ·  17Comments

albanm picture albanm  ·  20Comments

maxmckenzie picture maxmckenzie  ·  15Comments

jonalxh picture jonalxh  ·  15Comments

ebeloded picture ebeloded  ·  15Comments