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
Defines the BCP 47 u extension
of a t:Localize.LanguageTag.
Types
@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
Encodes a t/0 struct as canonical BCP 47 key/value pairs.
Arguments
u_extensionis at/0struct.
Returns
- A list of
{key, value}string tuples sorted by key, using the canonical BCP 47 forms (e.g.:gregorianencodes 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"}]
@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_extensionis the extension subtags as a string, with or without the leadingu-singleton (e.g."ca-gregory-hc-h23"or"u-ca-gregory-hc-h23").
Returns
{:ok, u}whereuis at/0struct 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")
Same as parse/1 but raises on error.
Examples
iex> u = Localize.LanguageTag.U.parse!("ca-iso8601")
iex> u.ca
:iso8601