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

Auto-detect language #121

Closed
krzysztofmajewski opened this issue Aug 10, 2020 · 4 comments
Closed

Auto-detect language #121

krzysztofmajewski opened this issue Aug 10, 2020 · 4 comments
Labels

Comments

@krzysztofmajewski
Copy link

I'm using polyglot for a bilingual (French and English) site. Currently the default_lang is en. I would like the site to auto-detect the user's preferred language, probably via the Accept-Language header. Discussion in this Issue suggests that this can be achieved via webserver configuration, by redirecting to the appropriate localized version of the deployed site based on inspecting the header. I'm open to that. However, there is currently no convenient/elegant path for me to do this. That's because I'm running the nginx webserver, and the relevant module requires me to recompile nginx. I can do that, but it would imply missing out on nginx updates from the distribution vendor (Ubuntu in my case).

Opening a new issue as suggested by @untra .

@garraflavatra
Copy link

Good idea. But I don’t think you have to struggle with webserver configuration, as it can be done with JavaScript like here. Then we can just make a JS file redirecting us to the right language.

Something like this:

var browserlang = navigator.language || navigator.userLanguage;
if (browserlang.includes('en')) { window.location = '/en/'; }
if (browserlang.includes('fr')) { window.location = '/fr/'; }

Of course, you’ll have to execute this code ONLY on the site’s index.html — otherwise the user will keep being redirected to the homepage.
To avoid this, you could also let this script display a banner at the top of the page saying there’s a page in the user’s language (then execute on all pages).

There’s one disadvantage: the JS code will be executed in the user’s browser. When the user has disabled JavaScript execution, this script doesn’t work.

@victorbnl
Copy link

If we put this code on the default language's index to be on /, i.e. the english index, the user who wants the english index will fall in a redirect loop

@garraflavatra
Copy link

garraflavatra commented May 13, 2021

@victorbnl You’re right, otherwise you can make a site detect the user’s language and if the user hasn’t selected that language, show a friendly dialog.

E.g. as Apple does:

A49DBBB6-4F47-46F4-AE49-1503ADCA339E

@victorbnl
Copy link

victorbnl commented May 13, 2021

I'd say that's not a solution, that's an awful user-experience, the goal would be to just display directly the correct page to the user

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

No branches or pull requests

4 participants