glintl/locales

Pick a locale from a tag such as "en-GB", for example from an Accept-Language header or the browser’s language.

This module refers to every locale, which has a cost:

If you know which locales you need, import them from glintl/locale/ instead.

Generated by codegen/generate.ts. Do not edit.

Values

pub fn all() -> List(glintl.Locale)

Every locale glintl has.

On JavaScript this is cheap, as each locale is just its tag.

On Erlang it’s expensive:

  • It loads every locale’s data: about 350 modules and 11 MB of memory. The BEAM doesn’t unload modules by itself, so that memory stays in use until the app stops.
  • The first call takes about 0.3 seconds while the modules load. In a request handler, that’s one very slow request.
  • Each call builds a new list of every locale, so searching it on every request allocates every time.

To find one locale, use from_tag, which only loads that locale’s data. Use all when you really need every locale, such as in a test that checks them all.

Other calendars’ data is only loaded when a formatter uses it, so all doesn’t load it.

pub fn from_tag(tag: String) -> Result(glintl.Locale, Nil)

Find a locale by its BCP 47 tag. Case and _ or - don’t matter.

If there is no exact match, the last part of the tag is dropped and it tries again, so "fr-CA-x-mine" gives Canadian French and "en-XX" gives English.

from_tag("en_GB")  // -> Ok(en_gb.locale())
from_tag("zh-TW")  // -> Ok(zh_hant.locale())
from_tag("xx")     // -> Error(Nil)
✨ Search Document