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

Puts the CLDR locale in the session.

The session key is fixed to be cldr_locale in order that downstream functions like those in liveview don't have to be passed options.

Options

  • :as is an options that determines the format in which the locale is saved in the session. That valid settings for this options are:
    • :string in which the current locale is converted to a string before storing in the session. It will then be parsed back into a %Cldr.LanguageTag{} upon reading it from the session. This option has the benefit that it will minimise space used in the session at the expense of the CPU time required to serialized and parse. The average size of commonly used locales is under 10 bytes and often less than 5.

    • :language_tag in which the current locale is stored in the session in its native %Cldr.LanguageTag{} format. This has the benefit of minimizing CPU time serialising and parsing the locale at the expense of a larger payment size in the session. The average size of a language tag struct, when serialized, is around 500 bytes.

The default is as: :string.

Examples

# Define a router module that
# sets the locale for the current process
# and then also sets it in the session

defmodule MyAppWeb.Router do
  use MyAppWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug Cldr.Plug.PutLocale,
        apps: [:cldr, :gettext],
        from: [:path, :query],
        gettext: MyApp.Gettext,
        cldr: MyApp.Cldr
    plug Cldr.Plug.PutSession, as: :string
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end
end