Facility for correctly converting POSIX locale codes into Intl locale codes
Description
JavaScript's Intl
APIs take a locale code such as en-CA
, called a BCP 47 locale code. However, the GNOME platform works with locale codes such as en_CA
, called POSIX locale codes, from environment variables such as LC_ALL
and from APIs such as GLib.get_language_names()
.
I learned recently that it is not correct to convert from POSIX to BCP 47 simply by replacing the _
with a -
. This works in many cases, but the correct transformation is much more complicated.
To get the best adoption of Intl
in GJS, we should provide this transformation in the platform somehow. This issue should discuss what form that API should take.
Prior Art
- Browser environments have
navigator.language
to get the current BCP 47 locale code. (An API such as that would not be appropriate for GJS because anavigator
object doesn't make sense for our environment. Also, we probably want an API that converts any POSIX locale, not just the current one.) - From a quick search, Node.js, Deno, and Bun do not have such a facility.
- ICU's code to convert POSIX locale into ICU locale, which can then be converted to BCP 47.