Hello,
The following doesn’t work for me:
# In the view
<button phx-click="set_en">English</button>
# In the handler
def handle_event("set_en", _, socket) do
Gettext.put_locale(MyApp.Gettext, "en")
socket = %{socket | fingerprints: Phoenix.LiveView.Diff.new_fingerprints()}
{:noreply, socket}
end
In fact I get {nil, %{}}
for the result of new_fingerprints()
.
Also I wasn’t able to find any documentation about The Diff module and that new_fingerprints()
function.
Do you have any documentation or even some code to share?
Thank you!
EDIT 1:
Anyway I tried the following which is working (very trivial though)
# The button to trigger the translation live update
<button phx-click="set_en">English</button>
# the gettext calls in the views
<%= @x && gettext "Hello" %>
# In the handler (x was set to 0 in mount())
def handle_event("set_en", _, socket) do
Gettext.put_locale(MyApp.Gettext, "en")
{:noreply, update(socket, x, &(&1 + 1))}
end
EDIT 2:
The following make it even a little cleaner:
# in the handler
def handle_event("set_en", _, socket) do
Gettext.put_locale(MyApp.Gettext, "en")
{:noreply, force_rerender(socket)}
end
defp force_rerender(socket, fingerprint \\ :x) do
update(socket, fingerprint, &(&1 + 1))
end
Default to :x
(as a single character to clutter a little less the view)…