View Source Cldr.Plug (Cldr Plug v1.3.2)

Functions to support setting the locale for Cldr and/or Gettext from the session.

Summary

Functions

Puts the locale from the session into the current process for Cldr and/or Gettext.

Types

@type application() :: :cldr | :gettext
@type applications() :: [application()]

Functions

Link to this function

put_locale_from_session(locale, applications \\ [:cldr, :gettext])

View Source
@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

  • session is any map, typically the map returned as part of the conn of a Phoenix or Plug request, A session is passed as the third parameter to the on_mount callback of a LiveView request.

  • applications is a list of applications for which the locale may be set. The valid options are :cldr and :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