elixir-cldr / cldr

Elixir implementation of CLDR/ICU
Other
447 stars 33 forks source link

Using cldr with liveview #134

Closed ghost closed 4 years ago

ghost commented 4 years ago

What's the recommended way to use cldr with a liveview app? Right now I'm passign the locale into the session like this:

# in the router

plug Cldr.Plug.SetLocale,
  apps: [:cldr, :gettext],
  cldr: App.Cldr,
  from: [:query, :accept_language]

live "/some_live_path", AppLive,
  session: {__MODULE__, :localized_session, []}

@doc false
def localized_session(conn) do
  %Cldr.LanguageTag{language: language} =
    Cldr.Plug.SetLocale.get_cldr_locale(conn) || App.Cldr.default_locale()

    %{"locale" => language}
end

but it doesn't seem to work correctly since the locales are different in the ssr'ed page and when the live view mounts: in ssr the locale is correct (e.g. en), but then in live view it becomes en-001.

So I wonder what exactly should I pass into the live view session to then restore the same exact locale as the one set by SetLocale plug.

ghost commented 4 years ago

It's probably not really an issue, so I moved the discussion to elixirforum.