rio517
Testing LiveView with different Locales
Hi,
I am trying to test a registration form where I’d like to capture the user’s locale. I can test my code manually and this works fine, but I cannot figure out how to change the locale in a test.
Gettext.with_locale(MyApp.Gettext, "de", fn ->
{:ok, lv, _html} = live(conn, ~p"/register")
[INSERT OTHER TEST CODE HERE]
end)
I also tried Gettext.put_locale(MyApp.Gettext, "de") and setting the locale on conn via conn = Plug.Conn.put_session(conn, :locale, "de").
What am I missing? Any other ways to get this done?
Background on capturing locale: I put MyApp.get_locale(PetalProWeb.Gettext) in a @locale and capture it via a hidden field in the registration form.
Thanks in advance!
Marked As Solved
LostKobrakai
The LV is a separate process. Gettext.with_locale (and basically all other ways of setting and retaining a locale) is per process. So you’re setting the locale for the test process, not the LV handling client.
You’ll need to run Gettext.put_locale within the LV process (e.g. as an on_mount callback) and at best you control that from the test the same way you pass the locale to LV in production.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








