Locale utility functions for resolution, validation, and per-locale data access.
Locale data
Per-locale CLDR data (number formats, calendar patterns,
territory names, etc.) is loaded on demand via a configurable
data provider and cached in :persistent_term. The default
provider is Localize.Locale.Provider.PersistentTerm.
load/2— loads raw locale data from the provider.get/2— retrieves a specific data key for a locale.
Locale resolution
parent/1— returns the CLDR parent locale (e.g.,:"en-AU"→:en,:en→:und).to_locale_id/1— coerces a language tag, atom, or string to a canonical locale identifier atom.gettext_locale_id/2— finds the best matching locale among a Gettext backend's known locales.
Summary
Types
A BCP 47 language subtag as an atom.
A locale identifier as an atom.
A BCP 47 script subtag as an atom.
A BCP 47 region/territory subtag as an atom.
Functions
Returns the default locale data provider module.
Returns the localized display name for a locale identifier.
Same as display_name/2 but raises on error.
Expands a list of locale identifiers (atoms and wildcard strings) into a list of known CLDR locale ID atoms.
Retrieves a value from locale data by following a list of access keys.
Returns the best-matching Gettext locale for a given locale identifier.
Loads locale data for the given locale.
Loads and stores locale data if it has not already been loaded.
Returns whether locale data has been loaded and is available.
Build a locale identifier string from its component parts.
Convert a POSIX locale identifier to a BCP 47 locale identifier by replacing underscores with hyphens.
Return the parent locale of the given language tag.
Stores locale data in the provider's backing store.
Coerces a locale identifier to an atom.
Types
Functions
@spec default_provider() :: module()
Returns the default locale data provider module.
Returns
- The module implementing
Localize.Locale.Provider.
@spec display_name(Localize.LanguageTag.t() | String.t() | atom(), Keyword.t()) :: {:ok, String.t()} | {:error, Exception.t()}
Returns the localized display name for a locale identifier.
Formats a locale identifier or language tag into a
human-readable display name using the CLDR locale display
name algorithm (e.g., "en-AU" → "English (Australia)").
Arguments
localeis a locale identifier string, atom, or aLocalize.LanguageTag.t/0.optionsis a keyword list of options.
Options
:localeis the locale to use for formatting. The default isLocalize.get_locale().:preferis:standardor:short. The default is:standard.
Returns
{:ok, name}wherenameis the localized display name.{:error, exception}if the locale cannot be resolved.
Examples
iex> Localize.Locale.display_name("en-AU")
{:ok, "English (Australia)"}
iex> Localize.Locale.display_name("zh-Hant")
{:ok, "Chinese (Traditional)"}
@spec display_name!(Localize.LanguageTag.t() | String.t() | atom(), Keyword.t()) :: String.t()
Same as display_name/2 but raises on error.
Examples
iex> Localize.Locale.display_name!("en-AU")
"English (Australia)"
Expands a list of locale identifiers (atoms and wildcard strings) into a list of known CLDR locale ID atoms.
Each entry is either an atom matching a known CLDR locale
(e.g. :en, :"fr-CA") or a string with a trailing *
wildcard (e.g. "en-*") that expands to all matching locales.
Invalid entries log a warning and are skipped.
Arguments
entriesis a list of atoms and/or strings.contextis an atom or string used in warning messages to identify where the entry came from (e.g.:supported_locales).
Returns
- A deduplicated list of locale ID atoms.
@spec get(Localize.Locale.Provider.locale(), list(), Keyword.t()) :: {:ok, term()} | {:error, term()}
Retrieves a value from locale data by following a list of access keys.
Delegates to the configured provider module to navigate the locale data map.
Arguments
localeis a locale identifier atom or aLocalize.LanguageTag.t/0.keysis a list of keys to traverse in the locale data map.optionsis a keyword list of options. The default is[].
Options
:provideris the module implementingLocalize.Locale.Providerto use. The default isdefault_provider/0.:fallbackis a boolean. Whentrue, if the requested key path is not found in the given locale, parent locales are searched according to the CLDR locale inheritance chain. The default isfalse.
Returns
{:ok, value}if the key path resolves to a value.{:error, reason}if the key path cannot be resolved.
@spec gettext_locale_id(Localize.LanguageTag.t() | atom() | String.t(), module()) :: {:ok, String.t()} | {:error, Exception.t()}
Returns the best-matching Gettext locale for a given locale identifier.
Compares the given locale against the locales known to a Gettext
backend using Localize.LanguageTag.best_match/3. This allows
a CLDR locale like :"en-AU" to match a Gettext locale like
"en" when no exact match exists.
Arguments
localeis a locale identifier atom, string, or aLocalize.LanguageTag.t/0.gettext_backendis a module that usesGettext(e.g.,MyApp.Gettext). It must respond toGettext.known_locales/1.
Returns
{:ok, gettext_locale}wheregettext_localeis a string from the Gettext backend's known locales.{:error, exception}if no match is found among the backend's known locales.
Examples
iex> Localize.Locale.gettext_locale_id(:en, Localize.Gettext)
{:error,
%Localize.UnknownLocaleError{
locale_id: "en"
}}
@spec load(Localize.Locale.Provider.locale(), Keyword.t()) :: {:ok, map()} | {:error, Exception.t()}
Loads locale data for the given locale.
Delegates to the configured provider module to find and retrieve locale data.
Arguments
localeis a locale identifier atom or aLocalize.LanguageTag.t/0.optionsis a keyword list of options. The default is[].
Options
:provideris the module implementingLocalize.Locale.Providerto use. The default isdefault_provider/0.
Returns
{:ok, locale_data}wherelocale_datais a map of the locale's CLDR data.{:error, Localize.UnknownLocaleError.t()}if the locale is not recognized.
@spec load_and_store(Localize.Locale.Provider.locale(), Keyword.t()) :: :ok | {:error, Exception.t()}
Loads and stores locale data if it has not already been loaded.
This is a convenience function that checks whether the locale data is already available and, if not, delegates to the configured provider to load and store it. Subsequent calls for the same locale are no-ops.
Arguments
localeis a locale identifier atom or aLocalize.LanguageTag.t/0.optionsis a keyword list of options. The default is[].
Options
:provideris the module implementingLocalize.Locale.Providerto use. The default isdefault_provider/0.
Returns
:okif the locale data is already loaded or was successfully loaded and stored.{:error, Localize.UnknownLocaleError.t()}if the locale data could not be loaded.
@spec loaded?(Localize.Locale.Provider.locale(), Keyword.t()) :: boolean()
Returns whether locale data has been loaded and is available.
Delegates to the configured provider module to check availability.
Arguments
localeis a locale identifier atom or aLocalize.LanguageTag.t/0.optionsis a keyword list of options. The default is[].
Options
:provideris the module implementingLocalize.Locale.Providerto use. The default isdefault_provider/0.
Returns
trueif the locale data has been loaded and stored.falseotherwise.
Build a locale identifier string from its component parts.
Assembles a BCP 47 locale identifier from language, script, territory, and variant subtags, omitting nil components.
Arguments
languageis a language subtag (string or atom).scriptis an optional script subtag (string, atom, or nil).territoryis an optional territory subtag (string, atom, or nil).variantsis a list of variant subtag strings.
Returns
- A BCP 47 locale identifier string.
Examples
iex> Localize.Locale.locale_id_from(:en, nil, :US, [])
"en-US"
iex> Localize.Locale.locale_id_from(:zh, :Hant, :TW, [])
"zh-Hant-TW"
iex> Localize.Locale.locale_id_from(:en, nil, nil, [])
"en"
Convert a POSIX locale identifier to a BCP 47 locale identifier by replacing underscores with hyphens.
Arguments
locale_idis a string locale identifier, potentially in POSIX format using underscores.
Returns
- A string with underscores replaced by hyphens.
Examples
iex> Localize.Locale.locale_id_from_posix("en_US")
"en-US"
iex> Localize.Locale.locale_id_from_posix("zh_Hant_TW")
"zh-Hant-TW"
iex> Localize.Locale.locale_id_from_posix("en")
"en"
@spec parent(Localize.LanguageTag.t() | String.t()) :: {:ok, Localize.LanguageTag.t()} | {:error, Exception.t()}
Return the parent locale of the given language tag.
Implements the CLDR locale ID inheritance algorithm (bundle
inheritance) from
Unicode TR35.
The parent locale is determined by first checking the CLDR
parentLocales supplemental data for an explicit override,
then falling back to progressive subtag removal.
Extensions (-u- and -t-) are transferred from the child
to the parent so that calendar, numbering system, and other
preferences are preserved across the inheritance chain.
Arguments
localeis a%Localize.LanguageTag{}struct or a BCP 47 locale identifier string.
Returns
{:ok, parent_tag}whereparent_tagis a%Localize.LanguageTag{}struct representing the parent locale.{:error, Localize.NoParentError.exception(locale: locale)}if the locale is the root locale (und) which has no parent.
Examples
iex> {:ok, parent} = Localize.Locale.parent("en-AU")
iex> parent.language
:en
iex> parent.territory
:"001"
iex> {:ok, parent} = Localize.Locale.parent("en")
iex> parent.language
:und
Stores locale data in the provider's backing store.
Delegates to the configured provider module to persist locale data.
Arguments
locale_idis a locale identifier atom.locale_datais a map of locale data to store.optionsis a keyword list of options. The default is[].
Options
:provideris the module implementingLocalize.Locale.Providerto use. The default isdefault_provider/0.
Returns
:okon success.{:error, reason}on failure.
@spec to_locale_id(Localize.LanguageTag.t() | atom() | String.t()) :: atom()
Coerces a locale identifier to an atom.
Accepts a Localize.LanguageTag.t/0, an atom, or a binary
string and returns the corresponding locale identifier atom.
When given a Localize.LanguageTag with a populated
:cldr_locale_id, that value is returned directly. When the
:cldr_locale_id is nil, the tag is serialised to a string
and converted to an atom.
Arguments
localeis aLocalize.LanguageTag.t/0, an atom, or a binary string.
Returns
- A locale identifier atom.
Examples
iex> Localize.Locale.to_locale_id(:en)
:en
iex> Localize.Locale.to_locale_id("en")
:en