Localize.LanguageTag.Ecto.Type (Localize SQL v2.0.0)

Copy Markdown View Source

An Ecto.Type storing a locale as its BCP 47 string in a text column.

A Localize.LanguageTag.t/0 is a rich struct — resolved likely subtags, -u- extension keywords, a canonical form — but all of it is derived from the language tag string, so the string is what is stored. The column is ordinary text holding a canonical tag such as "en-US" or "de-DE-u-co-phonebk".

Values load back into a fully resolved Localize.LanguageTag.t/0 via Localize.validate_locale/1, so a loaded locale is canonicalized, has its -u- extensions populated and its likely subtags resolved — the same value Localize.validate_locale/1 returns anywhere else, and the resolution is cached.

Schema

# in a migration
add :locale, :string

# in a schema
field :locale, Localize.LanguageTag.Ecto.Type

Casting

cast/1 accepts a Localize.LanguageTag.t/0, or any locale identifier Localize.validate_locale/1 accepts — a string ("en-US") or an atom (:"en-US"). An identifier that is not a valid locale returns an error rather than being stored.

Summary

Functions

Callback implementation for Ecto.Type.embed_as/1.

Returns whether two values are the same locale.

Functions

embed_as(_)

Callback implementation for Ecto.Type.embed_as/1.

equal?(term1, term2)

@spec equal?(term(), term()) :: boolean()

Returns whether two values are the same locale.

A language tag remembers the identifier it was parsed from, so "en-au" and "en-AU" cast to tags that are not ==. They are the same locale and store the same text, so a changeset that casts one over the other records no change.

Arguments

  • left is a language tag, a locale identifier or nil.

  • right is a language tag, a locale identifier or nil.

Returns

  • true when both dump to the same stored value, or are ==, or

  • false otherwise.

Examples

iex> {:ok, left} = Localize.LanguageTag.Ecto.Type.cast("en-au")
iex> Localize.LanguageTag.Ecto.Type.equal?(left, Localize.LanguageTag.new!("en-AU"))
true

iex> Localize.LanguageTag.Ecto.Type.equal?(:en, :fr)
false