Copy of https://github.com/elixir-cldr/cldr/issues/134
What’s the recommended way to use cldr / gettext with a liveview app? Right now I’m passign the locale into the session with Cldr.Plug.SetLocale
plug and then set the locale again in the liveview’s mount/3
callback:
# in the router
plug Cldr.Plug.SetLocale,
apps: [:cldr, :gettext],
cldr: App.Cldr,
from: [:query, :accept_language]
plug :put_locale_into_session
@doc false
def put_locale_into_session(conn, _opts) do
%Cldr.LanguageTag{cldr_locale_name: locale} = conn.private.cldr_locale
put_session(conn, "locale", locale)
end
live "/some_live_path", AppLive
# in the live view
def mount(_params, %{"locale" => locale}, socket) do
App.Cldr.put_locale(locale)
# ...
end
because otherwise it doesn’t seem to work correctly with the locales being different in the ssr’ed page and when the live view mounts: in ssr the locale is correct (e.g. en
):
%Cldr.LanguageTag{
canonical_locale_name: "en-Latn-US",
cldr_locale_name: "en",
extensions: %{},
gettext_locale_name: "en",
language: "en",
language_subtags: [],
language_variant: nil,
locale: %{},
private_use: [],
rbnf_locale_name: "en",
requested_locale_name: "en",
script: "Latn",
territory: "US",
transform: %{}
}
but then in live view it becomes en-001
(the default):
%Cldr.LanguageTag{
canonical_locale_name: "en-Latn-001",
cldr_locale_name: "en-001",
extensions: %{},
gettext_locale_name: nil,
language: "en",
language_subtags: [],
language_variant: nil,
locale: %{},
private_use: [],
rbnf_locale_name: "en",
requested_locale_name: "en-001",
script: "Latn",
territory: "001",
transform: %{}
}
So I wonder if anyone has used liveview cldr and how they resolved this problem and if there’s a less repetitive approach (something like plug
macro would be great).