Skip to content

Commit

Permalink
Add on_mount examples to Gettext docs (#2007)
Browse files Browse the repository at this point in the history
* add `on_mount` locale examples to docs

* replace `<%= web_namespace %>` with `MyAppWeb` in doc examples

* add fallback case for logged out routes
  • Loading branch information
oliviasculley committed May 6, 2022
1 parent 550cec2 commit 2af0014
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 3 additions & 3 deletions guides/introduction/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ The change is to define the `live_view` and `live_component` functions in your `
quote do
use Phoenix.View,
root: "lib/<%= lib_web_name %>/templates",
namespace: <%= web_namespace %>
namespace: MyAppWeb

# Import convenience functions from controllers
import Phoenix.Controller,
Expand All @@ -260,7 +260,7 @@ The change is to define the `live_view` and `live_component` functions in your `
def live_view do
quote do
use Phoenix.LiveView,
layout: {<%= web_namespace %>.LayoutView, "live.html"}
layout: {MyAppWeb.LayoutView, "live.html"}

unquote(view_helpers())
end
Expand Down Expand Up @@ -296,7 +296,7 @@ Note that LiveViews are automatically configured to use a "live.html.heex" layou

```elixir
use Phoenix.LiveView,
layout: {<%= web_namespace %>.LayoutView, "live.html"}
layout: {MyAppWeb.LayoutView, "live.html"}
```

"layouts/root.html.heex" is shared by regular and live views, "app.html.heex" is rendered inside the root layout for regular views, and "live.html.heex" is rendered inside the root layout for LiveViews. "live.html.heex" typically starts out as a copy of "app.html.heex", but using the `@socket` assign instead of `@conn`. Check the [Live Layouts](live-layouts.md) guide for more information.
Expand Down
2 changes: 1 addition & 1 deletion guides/server/security-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ to run it on all LiveViews by default:
def live_view do
quote do
use Phoenix.LiveView,
layout: {<%= web_namespace %>.LayoutView, "live.html"}
layout: {MyAppWeb.LayoutView, "live.html"}

on_mount MyAppWeb.UserLiveAuth
unquote(view_helpers())
Expand Down
28 changes: 28 additions & 0 deletions guides/server/using-gettext.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,31 @@ Then in your LiveView `mount/3`, you can restore the locale:
Gettext.put_locale(MyApp.Gettext, locale)
{:ok, socket}
end

You can also use the `on_mount` (`Phoenix.LiveView.on_mount/1`) hook to
automatically restore the locale for every LiveView in your application:

defmodule MyAppWeb.RestoreLocale do
import Phoenix.LiveView

def on_mount(:default, _params, %{"locale" => locale} = _session, socket) do
Gettext.put_locale(MyApp.Gettext, locale)
{:cont, socket}
end

# for any logged out routes
def on_mount(:default, _params, _session, socket), do: {:cont, socket}
end

Then, add this hook to `def live_view` under `MyAppWeb`, to run it on all
LiveViews by default:

def live_view do
quote do
use Phoenix.LiveView,
layout: {MyAppWeb.LayoutView, "live.html"}

on_mount MyAppWeb.RestoreLocale
unquote(view_helpers())
end
end

0 comments on commit 2af0014

Please sign in to comment.