An Elixir interface to internationalization functions modelled on the JavaScript Intl API.

This module delegates to the Localize library for locale-aware formatting and provides the top-level Intl static methods: get_canonical_locales/1 and supported_values_of/1.

The sub-modules mirror the JS Intl constructors adapted to idiomatic Elixir: options are passed as keyword lists, return values use {:ok, result} / {:error, reason} tuples, and bang variants are provided for convenience.

Sub-modules

Summary

Functions

Returns canonical locale name strings for the given locale identifiers.

Returns canonical locale name strings, raising on error.

Returns the supported values for a given internationalization key.

Functions

get_canonical_locales(locales)

@spec get_canonical_locales(String.t() | [String.t()]) ::
  {:ok, [String.t()]} | {:error, term()}

Returns canonical locale name strings for the given locale identifiers.

Parses and canonicalizes each locale string according to BCP 47 / RFC 5646 rules.

Arguments

  • locales is a locale identifier string or a list of locale identifier strings.

Returns

  • {:ok, list} where list is a list of canonical locale name strings.

  • {:error, reason} if any locale identifier cannot be parsed.

Examples

iex> Intl.get_canonical_locales("en-us")
{:ok, ["en-US"]}

iex> Intl.get_canonical_locales(["en-us", "fr-FR"])
{:ok, ["en-US", "fr-FR"]}

get_canonical_locales!(locales)

@spec get_canonical_locales!(String.t() | [String.t()]) :: [String.t()] | no_return()

Returns canonical locale name strings, raising on error.

Same as get_canonical_locales/1 but returns the list directly or raises an exception.

Arguments

  • locales is a locale identifier string or a list of locale identifier strings.

Returns

  • A list of canonical locale name strings.

Examples

iex> Intl.get_canonical_locales!("en-us")
["en-US"]

supported_values_of(key)

@spec supported_values_of(:calendar | :currency | :numbering_system | :unit) ::
  {:ok, list()} | {:error, term()}

Returns the supported values for a given internationalization key.

Arguments

  • key is one of :calendar, :currency, :numbering_system, or :unit.

Returns

  • {:ok, list} where list is a list of supported value atoms or strings.

  • {:error, reason} if the key is not recognized.

Examples

iex> {:ok, calendars} = Intl.supported_values_of(:calendar)
iex> :gregorian in calendars
true

iex> {:ok, systems} = Intl.supported_values_of(:numbering_system)
iex> :latn in systems
true