Created
July 11, 2022 21:41
-
-
Save mcrumm/f801a2f2d4fc10608f59af850fafbe83 to your computer and use it in GitHub Desktop.
Intl data via LiveView connect params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyAppWeb.Intl do | |
@moduledoc """ | |
Loads internationalization info from socket. | |
""" | |
import Phoenix.LiveView | |
def on_mount(:default, _params, _session, socket) do | |
cparams = get_connect_params(socket) | |
{:cont, | |
socket | |
|> assign(:locale, cparams["_intl_locale"]) | |
|> assign(:timezone, cparams["_intl_timezone"]) | |
|> assign(:timezone_offset, cparams["_intl_timezone_offset"])} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let liveSocket = new LiveSocket("/live", Socket, { | |
hooks: Hooks, | |
params: { | |
_csrf_token: csrfToken, | |
_intl_locale: Intl.NumberFormat().resolvedOptions().locale, | |
_intl_timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, | |
_intl_timezone_offset: -(new Date().getTimezoneOffset() / 60), | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scope "/", MyAppWeb do | |
pipe_through :browser | |
live_session :default, on_mount: [MyAppWeb.Intl] do | |
live "/", PageLive.Index, :index | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment