Next-translate: Support redirectToDefaultLang in static mode?

Created on 13 Mar 2020  ยท  13Comments  ยท  Source: vinissimus/next-translate

It seems like we could support redirectToDefaultLang in static mode by using a catch-all route at the top level. That page could detect the language and perform the redirect accordingly.

enhancement

All 13 comments

Here's an example of what I mean, as borrowed from the README:

โ”œโ”€โ”€ ca
โ”‚   โ”œโ”€โ”€ about.js
โ”‚   โ”œโ”€โ”€ index.js
โ”‚   โ””โ”€โ”€ nested
โ”‚       โ””โ”€โ”€ index.js
โ”œโ”€โ”€ en
โ”‚   โ”œโ”€โ”€ about.js
โ”‚   โ”œโ”€โ”€ index.js
โ”‚   โ””โ”€โ”€ nested
โ”‚       โ””โ”€โ”€ index.js
โ”œโ”€โ”€ es
โ”‚   โ”œโ”€โ”€ about.js
โ”‚   โ”œโ”€โ”€ index.js
โ”‚   โ””โ”€โ”€ nested
โ”‚       โ””โ”€โ”€ index.js
โ””โ”€โ”€ [...path].js

@justincy thank you a lot for your proposal!! ๐Ÿš€

With "redirects" in static mode, do you mean that the browser does these redirects, doesn't it? (Router.replace)

I guess that it will be interesting to add the new config prop redirectToDefaultLang for both (static mode and for a custom server middleware).

  • redirectToDefaultLang=false as default, for backwards compatibility.
  • redirectToDefaultLang=true (if en is the defaultLanguage)->

    • Static mode: It redirects on the browser level, routes as /some/route to /en/some/route.

    • Custom server middleware: It redirects with a status 301 on the server, routes as /some/route to /en/some/route.

I will investigate the catch-all route, I never used before! ๐Ÿ˜„

I remember than @BjoernRave did a similar proposal here: https://github.com/vinissimus/next-translate/issues/51, and we decided at that moment to don't bring this responsibility to the lib. However, if that feature is commonly used and it doesn't increase a lot the size of the lib, we can rethink it in order to implement it.

Maybe we can provide more than one redirect mode:

  • redirectMode:

    • 'none' (by default) - No redirects

    • 'browserLang' - Redirect to the lang of the browser when the language is not present on the URL. If the browser language is not a provided language, we redirect to defaultLanguage.

    • 'defaultLang' - Redirect directly to the defaultLanguage when the language is not present on the URL.

Maybe we can provide more than one redirect mode:

  • redirectMode:

    • 'none' (by default) - No redirects
    • 'browserLang' - Redirect to the lang of the browser when the language is not present on the URL. If the browser language is not a provided language, we redirect to defaultLanguage.
    • 'defaultLang' - Redirect directly to the defaultLanguage when the language is not present on the URL.

That's a good idea. But if you don't want to bother with language detection, you could just leave the current functionality of redirecting to the default lang and add language detection later.

Great!! ๐ŸŽ‰ In order to finish this issue:

  • [ ] Add docs on README explain it how it works
  • [ย ] Add tests if is possible

@justincy I did a prerelease under 0.11.0-canary.1 with the support of redirectToDefaultLang in static mode.

@justincy I realized that it works using some path, but when the path is just the homepage (localhost:3000), it displays a 404:

image

Thanks. I'll look into that bug.

@justincy I did a PR fixing some issues that I found (they are described in the PR). If you can take a look!

Great!! ๐ŸŽ‰ In order to finish this issue:

  • [ ] Add docs on README explain it how it works
  • [ ] Implement redirectToDefaultLang for no static mode (custom server with a middleware)
  • [ ] Add tests if is possible

Updating the README and adding tests makes sense to me. But the bit about custom server and middleware doesn't make sense to me. Doesn't it already exist here? Or is there something else you're asking for?

Wow @justincy you are right! This is already existing!! hah! Ok, perfect!

This will be available on 0.11.0 release

Was this page helpful?
0 / 5 - 0 ratings