Idiom.Locales (idiom v0.6.5)
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
get_hierarchy_opts()
Functions
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
format_locale(locale)
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"
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"]
get_language(locale)
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"
get_language_and_script(locale)
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