Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,21 @@ defmodule MyApp.Router do
pipe_through :browser
get "/", PageController, :index
...
# If you are using Phoenix LiveView, use the following:
live "/livepage", PageLive, session: [:locale]
end
end
```

### Phoenix LiveView
You need to set the locale in the mount/2 function of your page
```elixir
def mount(session, socket) do
Gettext.put_locale(session.locale)
...
end
```

### Options
- gettext: mandatory
- default_locale: mandatory, used as last step in fallback chain
Expand Down
4 changes: 3 additions & 1 deletion lib/set_locale.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ defmodule SetLocale do
if Enum.member?(config.additional_locales, requested_locale),
do: Gettext.put_locale(config.gettext, config.default_locale),
else: Gettext.put_locale(config.gettext, requested_locale)
assign(conn, :locale, requested_locale)
conn
|> assign(:locale, requested_locale)
|> put_session(:locale, requested_locale)
else
path = rewrite_path(conn, requested_locale, config)

Expand Down
12 changes: 12 additions & 0 deletions test/set_locale_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ defmodule SetLocaleTest do
default_locale: @default_locale,
additional_locales: ["fr", "es"]
}
@session Plug.Session.init(
store: :cookie,
key: "_set_locale",
encryption_salt: "set_locale",
signing_salt: "set_locale"
)

describe "init" do
test "it supports a legacy config" do
Expand Down Expand Up @@ -338,22 +344,26 @@ defmodule SetLocaleTest do
test "with sibling: it should only assign it" do
conn =
Phoenix.ConnTest.build_conn(:get, "/en-gb/foo/bar/baz", %{"locale" => "en-gb"})
|> Plug.Test.init_test_session(@session)
|> Plug.Conn.fetch_cookies()
|> SetLocale.call(@default_options)

assert conn.status == nil
assert conn.assigns == %{locale: "en-gb"}
assert get_session(conn, :locale) == "en-gb"
assert Gettext.get_locale(MyGettext) == "en-gb"
end

test "without sibling: it should only assign it" do
conn =
Phoenix.ConnTest.build_conn(:get, "/nl/foo/bar/baz", %{"locale" => "nl"})
|> Plug.Conn.fetch_cookies()
|> Plug.Test.init_test_session(@session)
|> SetLocale.call(@default_options)

assert conn.status == nil
assert conn.assigns == %{locale: "nl"}
assert get_session(conn, :locale) == "nl"
assert Gettext.get_locale(MyGettext) == "nl"
end

Expand Down Expand Up @@ -382,10 +392,12 @@ defmodule SetLocaleTest do
conn =
Phoenix.ConnTest.build_conn(:get, "/fr/foo/bar/baz", %{"locale" => "fr"})
|> Plug.Conn.fetch_cookies()
|> Plug.Test.init_test_session(@session)
|> SetLocale.call(@default_options_with_additional_locales)

assert conn.status == nil
assert conn.assigns == %{locale: "fr"}
assert get_session(conn, :locale) == "fr"
assert Gettext.get_locale(MyGettext) == @default_locale
end
end
Expand Down