Skip to content

Conversation

illfygli
Copy link
Contributor

@illfygli illfygli commented Aug 5, 2024

I wanted to try Phanpy, but only got a blank page, because Intl.DisplayNames can't handle some values that might appear in navigator.languages.

As MDN says:

The Accept-Language HTTP header in every HTTP request from the user's browser generally lists the same locales as the navigator.languages property, with decreasing q values (quality values).

My config includes *;q=0.5 as the last value, which gives a navigator.languages like ['xx', 'xx', 'xx', 'xx', '*'].

new Intl.DisplayNames([..., '*'], {
    type: 'language'
})

results in RangeError: Incorrect locale information provided.

So this proposed fix filters out any language tags that don't look like RFC 5646/BCP 47 format, which is what Intl.DisplayNames accepts.

It's not a full validator, but it does not exclude any valid tags.

@cheeaun cheeaun added bug Something isn't working need-research labels Aug 5, 2024
@cheeaun
Copy link
Owner

cheeaun commented Aug 5, 2024

Seems like it's better to leave the first param blank.

The runtime's default locale is used when undefined is passed or when none of the specified locale identifiers is supported.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames#locales

So, should looks like this:

const IntlDN = new Intl.DisplayNames(undefined, {
  type: 'language',
});

@illfygli is it possible for you to try this instead if it works?

@illfygli
Copy link
Contributor Author

illfygli commented Aug 5, 2024

@cheeaun Good catch, that works and seems to give the same result!

Updated the PR accordingly.

@cheeaun cheeaun merged commit 082409a into cheeaun:main Aug 6, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working need-research
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants