Idiom.Locales (idiom v0.1.0)

Utility functions for handling locales.

Locale identifiers consist of a language code, an optional script code, and an optional region code, separated by a hyphen (-).

Summary

Functions

Constructs a hierarchical list of locale identifiers from the given locale.

Extracts the language code from the given locale identifier.

Extracts the language and script codes from the given locale identifier.

Functions

Link to this function

get_hierarchy(locale, opts \\ [])

Constructs a hierarchical list of locale identifiers from the given locale.

The fallback option allows you to specify a fallback locale that will be added to the end of the hierarchy if it's not already included. By default, no fallback language is added.

Examples

iex> Idiom.Locales.get_hierarchy("en-Latn-US")
["en-Latn-US", "en-Latn", "en"]

iex> Idiom.Locales.get_hierarchy("de-DE", fallback: "en")
["de-DE", "de", "en"]
Link to this function

get_language(locale)

@spec get_language(String.t()) :: String.t()

Extracts the language code from the given locale identifier.

Examples

iex> Idiom.Locales.get_language("en-Latn-US")
"en"

iex> Idiom.Locales.get_language("de-DE")
"de"
Link to this function

get_language_and_script(locale)

@spec get_language_and_script(String.t()) :: String.t() | nil

Extracts the language and script codes from the given locale identifier.

Returns nil if the given locale does not have a script code.

Examples

iex> Idiom.Locales.get_language_and_script("en-Latn-US")
"en-Latn"

iex> Idiom.Locales.get_language_and_script("de-DE")
nil