Set user language from HTTP Accept-Language - #8751
Conversation
| ENV['LANG'] = 'C.UTF-8'; | ||
| if (navigator.languages != 'undefined' && navigator.languages.length > 0) { | ||
| ENV['LANG'] = navigator.languages[0].replace('-', '_') + '.UTF-8'; | ||
| } |
There was a problem hiding this comment.
This change seems a little risky to me - maybe some language has - in other places? Also, the app may behave differently for different users, which is the point I guess :) but it may be surprising to start doing so now.
There was a problem hiding this comment.
To improve Closure minifiability, one could do:
ENV['LANG'] = (typeof navigator === 'object' && navigator.languages && navigator.languages[0] || 'C').replace('-', '_') + '.UTF-8';(I would assume that if navigator.languages exists, it is an array)
Here, I wish to pass the language information from the browser to the application, which is currently lacking, so that i18n can be initiated.
According to the list of languages in Firefox > Preferences > Languages, no (only |
|
Interesting, thanks. Overall I feel I don't know enough about this to say what's right. I especially worry since the MDN page on this feature says it is experimental. So, yeah, maybe asking on the mailing list is a good idea. If there are no strong feelings against it then this seems ok to me, the risk is probably low enough. Btw, why would this change only be noticeable by calling setlocale? It seems like anything that looks at that environment value could be affected? (which I agree is probably very few things though) |
| ENV['HOME'] = '/home/web_user'; | ||
| ENV['LANG'] = 'C.UTF-8'; | ||
| if (typeof navigator === 'object' && typeof navigator.languages === 'object' && navigator.languages.length > 0) { | ||
| ENV['LANG'] = navigator.languages[0].replace('-', '_') + '.UTF-8'; |
There was a problem hiding this comment.
(should be 2-space indentation)
I meant that standard C library functions like scanf(3) are affected only when calling setlocale(3). |
|
Following the discussion on the mailing list:
Surprisingly, nobody suggested we did something for nodejs, where I believe it'd make more sense to inherit LANG from the system environment - but since all environment variables are reset that sorta makes sense too. What do we do with this PR ? :) |
|
This PR looks good to me - it does bridge POSIX language feature with browser API in a straightforward expected way, and developers do have a way to enforce no locale from both C ( |
|
I agree, after the discussion I think this is a good approach. Please just add a test before we land. |
|
I got some time at last and implemented the test case! |
| ENV['HOME'] = '/home/web_user'; | ||
| ENV['LANG'] = 'C.UTF-8'; | ||
| // Browser language detection #8751 | ||
| ENV['LANG'] = (typeof navigator === 'object' && navigator.languages && navigator.languages[0] || 'C').replace('-', '_') + '.UTF-8'; |
There was a problem hiding this comment.
How does the order of evaluation work here - is && before || or the opposite? Might be clearer to add parens, I think.
| // Expected results: | ||
| // LANG=C: 1 / 3.4 | ||
| // LANG=fr_FR.UTF-8: 1,2 / 3 | ||
| // Note: locale needs to be installed (cf. `locale -a`) |
There was a problem hiding this comment.
I'm not sure what this means - it needs to be installed locally? What error happens if it isn't?
|
Thanks, looks good aside from those minor comments! CI errors look unrelated too. |
|
I added clarifications. |
|
Great, thanks @Beuc! |
This allows users to start the app/game with LANG set to their preferred language, avoiding the need to explicitly change it in the app's GUI, POSIX-style. e.g. Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3 > navigator.languages = Array(4) [ "fr", "fr-FR", "en-US", "en" ] > LANG=fr.UTF-8
This allows users to start the app/game with LANG set to their preferred language, avoiding the need to explicitly change it in the app's GUI, POSIX-style.
e.g.