Skip to content

Instantly share code, notes, and snippets.

@mcrumm
Created July 11, 2022 21:41
Show Gist options
  • Save mcrumm/f801a2f2d4fc10608f59af850fafbe83 to your computer and use it in GitHub Desktop.
Save mcrumm/f801a2f2d4fc10608f59af850fafbe83 to your computer and use it in GitHub Desktop.
Intl data via LiveView connect params
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
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),
},
})
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