View Source Cldr.HTML.Locale (Cldr HTML v1.6.0)
Implements an HTML Form select specifically localised locale display.
Summary
Functions
Generate a list of options for a locale list
that can be used with Phoenix.HTML.Form.options_for_select/2 or
to create a <datalist>.
Generate an HTML select tag for a locale list
that can be used with a Phoenix.HTML.Form.t.
Types
@type locale() :: %{ locale: String.t(), display_name: String.t(), language_tag: Cldr.LanguageTag.t() }
Functions
@spec locale_options(select_options()) :: [tuple()] | {:error, {Cldr.UnknownLocaleError, binary()}}
Generate a list of options for a locale list
that can be used with Phoenix.HTML.Form.options_for_select/2 or
to create a <datalist>.
Arguments
- A
Keyword.t/0list of options.
Options
See Cldr.HTML.Locale.select/3 for options.
@spec select( form :: Phoenix.HTML.Form.t(), field :: Phoenix.HTML.Form.field(), select_options() ) :: Phoenix.HTML.safe() | {:error, {Cldr.UnknownLocaleError, binary()}}
Generate an HTML select tag for a locale list
that can be used with a Phoenix.HTML.Form.t.
Arguments
A
Phoenix.HTML.Form.t/0formA
Phoenix.HTML.Form.field/0fieldA
Keyword.t/0list of options
Options
For select options see Phoenix.HTML.Form.options_for_select/2
:localesdefines the list of locales to be displayed in the theselecttag. The list defaults toCldr.known_locale_names/0. If:backendis specified then the list of locales known to that backend is returned. If no:backendis specified the locales known toCldr.default_backend!/0is returned.:localedefines the locale to be used to localise the description of the list of locales. The default is the locale returned byCldr.get_locale/1If set to:identitythen each locale in the:localeslist will be rendered in its own locale.:backendis any backend module. The default isCldr.default_backend!/0:collatoris a function used to sort the locales in the selection list. It is passed a list of maps where each map represents a locale. The default collator sorts bylocale_1.display_name < locale_2.display_name. As a result, default collation sorts by code point which will not return expected results for scripts other than Latin.:mapperis a function that creates the text to be displayed in the select tag for each locale. It is passed a map with three fields::display_name,:localeand:language_tag. The default mapper is&{&1.display_name, &1.locale}. Seet:locale.:selectedidentifies the locale that is to be selected by default in theselecttag. The default isnil. This is passed to the form generator.:promptis a prompt displayed at the top of the select box. This is passed unmodified to the form generator.
Notes
If :locale is set to :identity then each locale in
:locales will be used to render its own display name. In
this case each locale in :locales must also be configured
in the :backend or an error will be returned.
Examples
Cldr.HTML.Locale.select(:my_form, :locale_list, selected: "en")
Cldr.HTML.Locale.select(:my_form, :locale_list,
locales: ["zh-Hant", "ar", "fr"],
mapper: &({&1.display_name, &1.locale}))