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

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

Link to this section Summary

Functions

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

Link to this section Types

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

Link to this section Functions

Link to this function

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

View Source
@spec put_locale_from_session(Cldr.LanguageTag.t(), 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

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

Returns

  • {:ok, locale} or

  • {:error, {exception, reason}}

example

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