Idiom.Locales (idiom v0.6.8)

Functionality for interacting with locales.

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

Summary

Functions

Returns the writing direction of the script belonging to the locale.

Formats a locale string.

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.

Types

Link to this type

get_hierarchy_opts()

@type get_hierarchy_opts() :: [{:fallback, String.t() | [String.t()]}]

Functions

Link to this function

direction(locale)

@spec direction(String.t()) :: :ltr | :rtl

Returns the writing direction of the script belonging to the locale.

Examples

iex> Idiom.Locales.direction("en-US")
:ltr

iex> Idiom.Locales.direction("ar")
:rtl
Link to this function

format_locale(locale)

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

Formats a locale string.

Idiom internally supports separating different parts of the locale code by either hyphen or underscore, which is then normalised by this function.

Examples

iex> Idiom.Locales.format_locale("de_de")
"de-DE"

iex> Idiom.Locales.format_locale("zh-hant-hk")
"zh-Hant-HK"
Link to this function

get_hierarchy(locale, opts \\ [])

@spec get_hierarchy(String.t(), get_hierarchy_opts()) :: [String.t()]

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

The fallback option allows you to specify a fallback locale or a list of fallback locales 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"]

iex> Idiom.Locales.get_hierarchy("de-DE", fallback: ["en-US", "fr"])
["de-DE", "de", "en-US", "fr"]
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

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