Skip to content

Commit

Permalink
docs: polish it
Browse files Browse the repository at this point in the history
  • Loading branch information
c4710n committed Dec 31, 2023
1 parent 8cda0d4 commit fddbfc2
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/plug_locale/browser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,47 @@ defmodule PlugLocale.Browser do
@doc """
Puts a response cookie for locale in the connection.
See `Plug.Conn.put_resp_cookie/4` for more details.
This is a simple wrapper around `Plug.Conn.put_resp_cookie/4`. See its docs
for more details.
## Examples
iex> put_locale_resp_cookie(conn, "en")
iex> put_locale_resp_cookie(conn, "zh", max_age: 365 * 24 * 60 * 60)
## Use cases
Use this function to persistent current locale into cookie, then subsequent
requests can directly read the locale from the cookie.
defmodule DemoWeb.PlugBrowserLocalization do
use Plug.Builder
plug PlugLocale.Browser,
default_locale: "en",
locales: ["en", "zh"],
route_identifier: :locale,
assign_key: :locale
plug :put_locale
def put_locale(conn, _opts) do
if locale = conn.assigns[:locale] do
# integrate with gettext
Gettext.put_locale(locale)
# persistent current locale into cookie
PlugLocale.Browser.put_locale_resp_cookie(
conn,
locale,
max_age: 365 * 24 * 60 * 60
)
else
conn
end
end
end
"""
@spec put_locale_resp_cookie(Plug.Conn.t(), String.t()) :: Plug.Conn.t()
def put_locale_resp_cookie(%Plug.Conn{} = conn, locale, opts \\ []) do
Expand Down

0 comments on commit fddbfc2

Please sign in to comment.