From 6e05e66d8b2872dc324dbd5250e65dcc8e43ab69 Mon Sep 17 00:00:00 2001 From: Rainer <57937814+raerkeer@users.noreply.github.com> Date: Wed, 20 Nov 2019 09:37:48 +0100 Subject: [PATCH 1/2] Put the locale into the session for LiveView To get set_locale working with Phoenix LiveView, we need to put the locale into the session --- lib/set_locale.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/set_locale.ex b/lib/set_locale.ex index d55b376..0db62e0 100644 --- a/lib/set_locale.ex +++ b/lib/set_locale.ex @@ -39,6 +39,7 @@ defmodule SetLocale do do: Gettext.put_locale(config.gettext, config.default_locale), else: Gettext.put_locale(config.gettext, requested_locale) assign(conn, :locale, requested_locale) + put_session(conn, :locale, requested_locale) else path = rewrite_path(conn, requested_locale, config) From 48da86ca3259d665010d0d72cee180f26836cdce Mon Sep 17 00:00:00 2001 From: Rainer <57937814+raerkeer@users.noreply.github.com> Date: Wed, 20 Nov 2019 09:56:22 +0100 Subject: [PATCH 2/2] Updated readme with instructions for Phoenix LiveView --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 0fe3522..0aec02e 100644 --- a/README.md +++ b/README.md @@ -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