Cldr.Plug (Cldr Plug v1.3.4)
View SourceFunctions to support setting the locale for Cldr and/or Gettext from the session.
Summary
Types
@type application() :: :cldr | :gettext
      @type applications() :: [application()]
Functions
@spec put_locale_from_session(map(), applications()) :: {:ok, Cldr.LanguageTag.t()} | {:error, {module(), String.t()}}
Puts the locale from the session into the current
process for Cldr and/or Gettext.
This function is useful to place in the on_mount
callback for a LiveView.
Arguments
sessionis any map, typically the map returned as part of theconnof a Phoenix or Plug request, Asessionis passed as the third parameter to theon_mountcallback of a LiveView request.applicationsis a list of applications for which the locale may be set. The valid options are:cldrand:gettext. The default is[:cldr, :gettext]
Returns
{:ok, locale}or{:error, {exception, reason}}
Example
=> Cldr.Plug.put_locale_from_session(session)
=> Cldr.Plug.put_locale_from_session(session, [:cldr])
=> Cldr.Plug.put_locale_from_session(session, [:cldr, :gettext])
# In a LiveView
def on_mount(:default, _params, session, socket) do
  {:ok, locale} = Cldr.Plug.put_locale_from_session(session)
end