-
Notifications
You must be signed in to change notification settings - Fork 438
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
Handle browsers with no navigator.language better (#521) #674
Conversation
Good job on the PR! I've tested with Could you rebase on latest master please? The |
25dd401
to
36ad436
Compare
@ix5 Rebased and made both of the requested changes. |
Great, thanks! @jelmer @blatinier please review when you find the time. |
@zackw I've rebased your branch on top of current master, please see https://github.com/ix5/isso/tree/pull-674-rebased After that, I'll merge this PR. |
The code for setting `config["lang"]`, in `isso/app/config.js`, assumes that browsers will always provide a value for `navigator.language` and/or `navigator.userLanguage`. Per bug isso-comments#521, this is not a safe assumption. While I was attempting to fix this, I noticed that regional variants of a language (`zh-TW`, `pt-BR`) were being handled in an ad-hoc, unreliable manner. I also noticed a new user-agent language property [`navigator.languages`][] which more closely matches the semantics of [`Accept-Language`][]—it would be good to support that. This patch addresses all the above problems, as follows: 1. Add a new configuration property `data-isso-default-lang` that specifies the language to use (instead of English) when the browser *doesn’t* have a preference. 2. Document that we expect the value of `data-isso-lang` and `data-isso-default-lang` to be a [BCP 47][] language tag, because this is what `navigator.language` etc contain. (The practical upshot is that tags like `zh-TW` are officially allowed now.) 3. In `config.js`, compile an array of candidate language tags, in descending order of preference, from all available sources: `data-isso-lang`, `navigator.languages`, `navigator.language`, `navigator.userLanguage`, `data-isso-default-lang`, and finally `"en"` as a backstop. Handle any or all of the above being null/undefined/empty. This array goes into `config["langs"]`. `config["lang"]` is removed. 4. In `i18n.js`, select the first entry in `config["langs"]` for which we have both pluralforms and a translation, and make that the value of `i18n.lang`. An English backstop is supplied here too for extra defensiveness. Also, if we don’t have a translation for say `zh-HK`, try `zh` before moving on to the next entry in the array. 5. New function `utils.normalize_bcp47` ensures that we process language tags, whereever they came from, case-insensitively and treating `_` as equivalent to `-`. 6. Move `utils.ago` to `i18n.ago` to avoid a circular dependency between utils and i18n. [`navigator.languages`]: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/languages [`Accept-Language`]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language [BCP 47]: https://tools.ietf.org/html/bcp47
eeae07e
to
5502445
Compare
@ix5 Done. |
LGTM, I think proper handling of the two-part language identifiers is also worth merging this. |
Thank you for reviewing, @stefangehn! |
The code for setting
config["lang"]
, inisso/app/config.js
,assumes that browsers will always provide a value for
navigator.language
and/ornavigator.userLanguage
. Per bug #521,this is not a safe assumption.
While I was attempting to fix this, I noticed that regional variants
of a language (
zh-TW
,pt-BR
) were being handled in an ad-hoc,unreliable manner. I also noticed a new user-agent language property
navigator.languages
which more closely matches the semantics ofAccept-Language
—it would be good to support that.This patch addresses all the above problems, as follows:
Add a new configuration property
data-isso-default-lang
thatspecifies the language to use (instead of English) when the browser
doesn’t have a preference.
Document that we expect the value of
data-isso-lang
anddata-isso-default-lang
to be a BCP 47 language tag, becausethis is what
navigator.language
etc contain. (The practicalupshot is that tags like
zh-TW
are officially allowed now.)In
config.js
, compile an array of candidate language tags, indescending order of preference, from all available sources:
data-isso-lang
,navigator.languages
,navigator.language
,navigator.userLanguage
,data-isso-default-lang
, and finally"en"
as a backstop. Handle any or all of the above beingnull/undefined/empty. This array goes into
config["langs"]
.config["lang"]
is removed.In
i18n.js
, select the first entry inconfig["langs"]
for whichwe have both pluralforms and a translation, and make that the value
of
i18n.lang
. An English backstop is supplied here too for extradefensiveness. Also, if we don’t have a translation for say
zh-HK
, tryzh
before moving on to the next entry in the array.New function
utils.normalize_bcp47
ensures that we processlanguage tags, whereever they came from, case-insensitively and
treating
_
as equivalent to-
.Move
utils.ago
toi18n.ago
to avoid a circular dependencybetween utils and i18n.