Automatically redirect users based on locale #5839
Replies: 1 comment 3 replies
-
Note that Vercel recently released Edge Fucntions (built on top on Cloudflare Workers), and i18n redirect/rewrites is one of the main use-case they showcased in their conf. https://vercel.com/features/edge-functions https://github.com/vercel/examples/tree/main/edge-functions/i18n https://youtu.be/WlP2TB2ORL4?t=338 If we document this, I don't want to clutter our doc with hosting provider specific solutions, but rather link to the official doc of the various hosting providers.
I don't think we'll handle such a cookie automatically. Internally there are Facebook needs to comply better with GDPR and present a consent banner, and I'd rather figure this out before introducing some new cookie-based system. We could add a |
Beta Was this translation helpful? Give feedback.
-
Hi all!
I was looking for a way to automatically redirect users to the documentation in their language, based on their locale. The docs suggest to use a server-side redirect for that, so I came up with a Cloudflare Workers script that does the trick for me.
Right now, I only have one additional language (Brazilian Portuguese), so the script isn't super generic, but it should be a good starting point for other Docusaurus users who can adapt it to their needs.
↣ auto-locale CloudFlare Workers script
The script checks the "Accept-Language" header, and if Portuguese has a higher priority than English, it'd prepend
/pt-BR
to the request path. It also tries to avoid overriding user-chosen language by looking at theSec-Fetch-Site
andReferer
headers. So if the user explicitly choses a different language (e.g. English), it will skip the redirect.Ideally, I'd love if Docusaurus stored the user chosen language in a cookie (or at least, would append some query string to the URL so that the script could detect it and set such cookie). This would allow user choice to persist, so the user won't have to set the language every time when visiting the documentation.
Beta Was this translation helpful? Give feedback.
All reactions