Localize.LanguageTag.U (Localize v0.44.0)

Copy Markdown View Source

Defines the struct for the BCP 47 u extension and provides a standalone parser for -u- extension strings.

The u extension carries Unicode locale keywords such as the calendar (ca), hour cycle (hc), numbering system (nu) and collation options (co, ks, …). It is normally populated on a Localize.LanguageTag.t/0 by Localize.validate_locale/1; parse/1 parses an extension string on its own, for consumers that receive the -u- subtags without a full locale identifier.

Summary

Types

t()

Defines the BCP 47 u extension of a t:Localize.LanguageTag.

Functions

Encodes a t/0 struct as canonical BCP 47 key/value pairs.

Parses a standalone BCP 47 u extension string.

Same as parse/1 but raises on error.

Types

t()

@type t() :: %Localize.LanguageTag.U{
  ca: atom(),
  cf: atom(),
  co: atom(),
  cu: atom(),
  dx: atom(),
  em: atom(),
  fw: atom(),
  hc: atom(),
  ka: atom(),
  kb: atom(),
  kc: atom(),
  kf: atom(),
  kh: atom(),
  kk: atom(),
  kn: atom(),
  kr: atom(),
  ks: atom(),
  kv: atom(),
  lb: atom(),
  lw: atom(),
  ms: atom(),
  mu: atom(),
  nu: atom(),
  rg: atom(),
  sd: atom(),
  ss: atom(),
  tz: atom(),
  va: atom(),
  vt: atom()
}

Defines the BCP 47 u extension of a t:Localize.LanguageTag.

Functions

encode(u_extension)

@spec encode(t()) :: [{String.t(), String.t()}]

Encodes a t/0 struct as canonical BCP 47 key/value pairs.

Arguments

  • u_extension is a t/0 struct.

Returns

  • A list of {key, value} string tuples sorted by key, using the canonical BCP 47 forms (e.g. :gregorian encodes as {"ca", "gregory"}).

Examples

iex> {:ok, u} = Localize.LanguageTag.U.parse("hc-h23-ca-gregory")
iex> Localize.LanguageTag.U.encode(u)
[{"ca", "gregory"}, {"hc", "h23"}]

parse(u_extension)

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

Parses a standalone BCP 47 u extension string.

The keywords are validated and canonicalized exactly as they would be by Localize.validate_locale/1 on a full locale identifier: keys are checked against the CLDR validity data, aliases are resolved, and values are decoded to their canonical atoms (e.g. ca-gregory decodes to :gregorian).

Arguments

  • u_extension is the extension subtags as a string, with or without the leading u- singleton (e.g. "ca-gregory-hc-h23" or "u-ca-gregory-hc-h23").

Returns

  • {:ok, u} where u is a t/0 struct with the decoded keywords set.

  • {:error, exception} if the extension string cannot be parsed or a keyword fails validation.

Examples

iex> {:ok, u} = Localize.LanguageTag.U.parse("ca-gregory-hc-h23")
iex> {u.ca, u.hc}
{:gregorian, :h23}

iex> {:ok, u} = Localize.LanguageTag.U.parse("u-nu-thai")
iex> u.nu
:thai

iex> {:error, _} = Localize.LanguageTag.U.parse("not a u extension")

parse!(u_extension)

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

Same as parse/1 but raises on error.

Examples

iex> u = Localize.LanguageTag.U.parse!("ca-iso8601")
iex> u.ca
:iso8601