Localize.ContextTransform (Localize v1.10.0)

Copy Markdown View Source

Cases a name for the context it stands in, as CLDR's context transforms say.

CLDR writes a name as it appears in the middle of a sentence. French writes language names in lower case, "français", and a menu of languages capitalizes them: "Français". CLDR records, for each locale, which kinds of name change case in which contexts, and transform/4 applies that record.

Usages

The kind of name, as CLDR names it: :languages, :script, :key_value, :calendar_field, :currency_name, :day_format_except_narrow, :day_standalone_except_narrow, :month_format_except_narrow, :month_standalone_except_narrow, :era_abbr, :era_name, :number_spellout, :relative and :typographic_names.

Contexts

  • :middle_of_sentence leaves the name as CLDR writes it.

  • :begin_sentence titlecases its first word, in every locale.

  • :ui_list_or_menu is a name in a menu or a list, such as a language picker. Its case is the locale's.

  • :stand_alone is a name that stands by itself, such as a month in a calendar header. Its case is the locale's.

Titlecasing changes the first letter of the first word and nothing else, so "español (México)" becomes "Español (México)". It follows the language's casing: Turkish and Azerbaijani titlecase "i" as "İ", and Dutch "ij" as "IJ".

Summary

Types

A context a name stands in.

A kind of name a context transform applies to.

Functions

Returns the contexts a name may stand in.

Cases a name for the context it stands in.

Returns the usages a context transform applies to.

Types

context()

@type context() ::
  :middle_of_sentence | :begin_sentence | :ui_list_or_menu | :stand_alone

A context a name stands in.

usage()

@type usage() ::
  :languages
  | :script
  | :key_value
  | :calendar_field
  | :currency_name
  | :day_format_except_narrow
  | :day_standalone_except_narrow
  | :month_format_except_narrow
  | :month_standalone_except_narrow
  | :era_abbr
  | :era_name
  | :number_spellout
  | :relative
  | :typographic_names

A kind of name a context transform applies to.

Functions

contexts()

@spec contexts() :: [context(), ...]

Returns the contexts a name may stand in.

Returns

  • A list of context atoms.

Examples

iex> Localize.ContextTransform.contexts()
[:middle_of_sentence, :begin_sentence, :ui_list_or_menu, :stand_alone]

transform(name, usage, context, options \\ [])

@spec transform(String.t(), usage(), context(), Keyword.t()) ::
  {:ok, String.t()} | {:error, Exception.t()}

Cases a name for the context it stands in.

Arguments

  • name is the name as CLDR writes it, a string.

  • usage is the kind of name; see "Usages" in the module documentation.

  • context is the context it stands in; see "Contexts" in the module documentation.

  • options is a keyword list of options.

Options

Returns

  • {:ok, name} cased for the context, or

  • {:error, exception} when name is not a string, usage or context is not one listed above, or :locale is not a valid locale.

Examples

iex> Localize.ContextTransform.transform("français", :languages, :ui_list_or_menu, locale: :fr)
{:ok, "Français"}

iex> Localize.ContextTransform.transform("français", :languages, :middle_of_sentence, locale: :fr)
{:ok, "français"}

iex> Localize.ContextTransform.transform("janvier", :month_standalone_except_narrow, :stand_alone, locale: :fr)
{:ok, "Janvier"}

usages()

@spec usages() :: [usage(), ...]

Returns the usages a context transform applies to.

Returns

  • A list of usage atoms.

Examples

iex> :languages in Localize.ContextTransform.usages()
true