Localize.HTML.Month (Localize Web v0.7.0)

Copy Markdown View Source

Generates HTML <select> tags and option lists for localized month name display.

Month names are sourced from CLDR calendar data and localized according to the current or specified locale. The display style (wide, abbreviated, narrow), calendar system, and sort order are all configurable.

Summary

Functions

Generates a list of options for a month list that can be used with Phoenix.HTML.Form.options_for_select/2 or to create a <datalist>.

Generates an HTML select tag for a month name list that can be used with a Phoenix.HTML.Form.t.

Types

select_options()

@type select_options() :: [
  months: [pos_integer(), ...],
  locale: Localize.locale() | Localize.LanguageTag.t(),
  calendar: Calendar.calendar(),
  year: Calendar.year(),
  style: :wide | :abbreviated | :narrow,
  collator: function(),
  mapper: function(),
  selected: pos_integer()
]

Functions

month_options(options \\ [])

@spec month_options(select_options()) :: [tuple()]

Generates a list of options for a month list that can be used with Phoenix.HTML.Form.options_for_select/2 or to create a <datalist>.

Arguments

Options

See Localize.HTML.Month.select/3 for options.

Returns

  • A list of {month_name, month_number} tuples.

select(form, field, options \\ [])

@spec select(
  form :: Phoenix.HTML.Form.t(),
  field :: Phoenix.HTML.Form.field(),
  select_options()
) :: Phoenix.HTML.safe()

Generates an HTML select tag for a month name list that can be used with a Phoenix.HTML.Form.t.

Arguments

Options

  • :months defines the list of month numbers to be displayed. The default is 1..12.

  • :calendar is the calendar module from which the month names are derived. The default is Calendar.ISO, which renders Gregorian labels. If the calendar module exports cldr_calendar_type/0, the returned atom selects the CLDR calendar used for the labels (for example, Cldr.Calendar.Hebrew returns :hebrew). Calendars without that function fall back to Gregorian labels.

  • :year is the year from which the number of months is derived. The default is the current year.

  • :locale defines the locale used to localise the month names. The default is the locale returned by Localize.get_locale/0.

  • :style is the format of the month name. The options are :wide (the default), :abbreviated and :narrow.

  • :collator is a function used to sort the months. The default collator preserves month order.

  • :mapper is a function that creates the text to be displayed in the select tag for each month. It receives a tuple {month_name, month_number}. The default is the identity function.

  • :selected identifies the month to be selected by default in the select tag. The default is nil.

  • :prompt is a prompt displayed at the top of the select box.

Returns

Examples

iex> Localize.HTML.Month.select(:my_form, :month, selected: 1)