JavaScript ES6/Harmony now has language-specific sorting/collation.
Now browsers differ in their support so I'm finding with Chrome some languages I use work and other don't.
But what I'm having trouble with is for languages such as Finnish, Japanese, Spanish, and Swedish, which have more than one way of ordering text, finding which ways are supported by the browser?
Intl.Collator.supportedLocalesOf
accepts a list of locales and returns the ones it supports, but it removes Unicode extensions from each locale before testing for support. So if you were to do
Intl.Collator.supportedLocalesOf("de-u-co-phonebk")
and the de
locale were supported but phonebook sorting were not supported, you'd get back ["de-u-co-phonebk"]
. Likely worse (because I suspect support for the de
locale will pretty much always imply support for phonebook sorting), if you mistyped that as
Intl.Collator.supportedLocalesOf("de-u-co-phonebm")
you'd get ["de-u-co-phonebm"]
which is potentially really bad.
Given that's out, I think what you have to do is individually attempt to create collators for all the locales, specifying all the possible Unicode locale extensions you care about that could affect sort order, then examine resolvedOptions().locale
on the collator to see if the Unicode extension transferred over. For example,
Intl.Collator("de-u-co-phonebk").resolvedOptions().locale
will return "de-u-co-phonebk"
, while
Intl.Collator("de-u-co-phonebm").resolvedOptions().locale
will return "de"
.