Icu.LanguageTag (icu v0.4.0)

Represents a locale parsed via ICU4X.

Language tags wrap a NIF resource. Use the provided parsing functions to construct values and avoid manipulating the resource directly.

Summary

Types

Hour cycle preference for time formatting.

t()

Functions

Returns the parsed components of a language tag.

Returns the parsed components and raises on error.

Returns the full list of fallback locales for the given locale. "lookup" according to RFC4647.

Gets the hour cycle preference from a language tag.

Gets the hour cycle preference and raises on error.

Attempts to match the given LanguageTag.t() against a list of gettext locales.

The maximize method potentially updates a passed in locale in place depending up the results of running the ‘Add Likely Subtags’ algorithm from https://www.unicode.org/reports/tr35/#Likely_Subtags.

This returns a new Locale that is the result of running the ‘Remove Likely Subtags’ algorithm from https://www.unicode.org/reports/tr35/#Likely_Subtags.

This returns a new Locale that is the result of running the 'Remove Likely Subtags, favoring script' algorithm from https://www.unicode.org/reports/tr35/#Likely_Subtags.

Parses a locale string and returns a language tag resource.

Parses a locale string and raises on error.

Sets the hour cycle preference on a language tag.

Sets the hour cycle preference and raises on error.

Converts a language tag resource back to its canonical string representation.

Converts a language tag resource to string and raises on error.

Types

hour_cycle()

@type hour_cycle() :: :h11 | :h12 | :h23

Hour cycle preference for time formatting.

parsable()

@type parsable() :: t() | String.t()

parse_error()

@type parse_error() :: {:error, :invalid_locale}

t()

@opaque t()

Functions

components(language_tag)

@spec components(t()) ::
  {:ok, Icu.LanguageTag.Components.t()} | {:error, :invalid_resource}

Returns the parsed components of a language tag.

components!(locale)

@spec components!(t()) :: Icu.LanguageTag.Components.t()

Returns the parsed components and raises on error.

fallbacks(language_tag)

@spec fallbacks(t()) :: {:ok, [t()]}

Returns the full list of fallback locales for the given locale. "lookup" according to RFC4647.

get_hour_cycle(language_tag)

@spec get_hour_cycle(t()) :: {:ok, hour_cycle() | nil} | {:error, :invalid_resource}

Gets the hour cycle preference from a language tag.

Returns {:ok, hour_cycle} if the tag has an hour cycle extension set, or {:ok, nil} if no hour cycle is specified.

Examples

iex> {:ok, tag} = Icu.LanguageTag.parse("en-US-u-hc-h23")
iex> Icu.LanguageTag.get_hour_cycle(tag)
{:ok, :h23}

iex> {:ok, tag} = Icu.LanguageTag.parse("en-US")
iex> Icu.LanguageTag.get_hour_cycle(tag)
{:ok, nil}

get_hour_cycle!(tag)

@spec get_hour_cycle!(t()) :: hour_cycle() | nil

Gets the hour cycle preference and raises on error.

Examples

iex> tag = Icu.LanguageTag.parse!("en-US-u-hc-h12")
iex> Icu.LanguageTag.get_hour_cycle!(tag)
:h12

match_gettext(language_tag, gettext_locales)

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

Attempts to match the given LanguageTag.t() against a list of gettext locales.

Uses "lookup" according to RFC4647.

Accepts both _ and - as separators in gettext_locales.

maximize(language_tag)

@spec maximize(t()) :: {:modified, t()} | {:unmodified, t()}

The maximize method potentially updates a passed in locale in place depending up the results of running the ‘Add Likely Subtags’ algorithm from https://www.unicode.org/reports/tr35/#Likely_Subtags.

This function does not guarantee that any particular set of subtags will be present in the resulting locale.

minimize(language_tag)

@spec minimize(t()) :: {:modified, t()} | {:unmodified, t()}

This returns a new Locale that is the result of running the ‘Remove Likely Subtags’ algorithm from https://www.unicode.org/reports/tr35/#Likely_Subtags.

minimize_favor_script(language_tag)

@spec minimize_favor_script(t()) :: {:modified, t()} | {:unmodified, t()}

This returns a new Locale that is the result of running the 'Remove Likely Subtags, favoring script' algorithm from https://www.unicode.org/reports/tr35/#Likely_Subtags.

parse(locale_string)

@spec parse(String.t() | t()) :: {:ok, t()} | parse_error()

Parses a locale string and returns a language tag resource.

parse!(locale_string)

@spec parse!(String.t() | t()) :: t()

Parses a locale string and raises on error.

set_hour_cycle(language_tag, hour_cycle)

@spec set_hour_cycle(t(), hour_cycle()) :: {:ok, t()} | {:error, :invalid_options}

Sets the hour cycle preference on a language tag.

Returns a new language tag with the hour cycle Unicode extension (-u-hc-) set.

Hour Cycles

  • :h11 – Hour system using 0-11 (noon = 0, midnight = 0)
  • :h12 – Hour system using 1-12 (noon = 12, midnight = 12)
  • :h23 – Hour system using 0-23 (noon = 12, midnight = 0)

Examples

iex> {:ok, tag} = Icu.LanguageTag.parse("en-US")
iex> {:ok, tag_with_hc} = Icu.LanguageTag.set_hour_cycle(tag, :h23)
iex> Icu.LanguageTag.to_string!(tag_with_hc)
"en-US-u-hc-h23"

set_hour_cycle!(tag, hour_cycle)

@spec set_hour_cycle!(t(), hour_cycle()) :: t()

Sets the hour cycle preference and raises on error.

Examples

iex> tag = Icu.LanguageTag.parse!("en-US")
iex> tag_with_hc = Icu.LanguageTag.set_hour_cycle!(tag, :h23)
iex> Icu.LanguageTag.to_string!(tag_with_hc)
"en-US-u-hc-h23"

to_string(language_tag)

@spec to_string(t()) :: {:ok, String.t()} | {:error, :invalid_resource}

Converts a language tag resource back to its canonical string representation.

to_string!(locale)

@spec to_string!(t()) :: String.t()

Converts a language tag resource to string and raises on error.