Turned on :error_handling, :underspecs, and :unmatched_returns in mix.exs dialyzer config. The 97 warnings this surfaced were fixed in place rather than suppressed: - unmatched_return (79): explicit discard with `_ = ...` for fire-and-forget side effects (Process.cancel_timer, :ets.new, send/2), and pattern-matched `:ok = ...` for control-plane Phoenix.PubSub subscribe/unsubscribe/broadcast calls so a future return-shape change fails loud. - contract_supertype (18): tightened @spec arg and return types on data_builder, historical_loader, url_params, packet_utils, encoding_utils, aprs_symbol, weather_controller, packet_replay to match each function's actual success typing. No behavioural change. mix compile clean, 1008 tests pass, dialyzer count is now 0.
46 lines
1.2 KiB
Elixir
46 lines
1.2 KiB
Elixir
defmodule AprsmeWeb.LocaleHook do
|
|
@moduledoc """
|
|
LiveView hook for setting locale based on Accept-Language header.
|
|
This ensures that LiveView updates maintain the correct locale.
|
|
"""
|
|
import Phoenix.Component
|
|
|
|
# Cache supported locales at compile time
|
|
@supported_locales ~w(en es de fr)
|
|
|
|
def on_mount(:set_locale, params, session, socket) do
|
|
# Only set locale in production to allow seeing "xx" placeholders in dev
|
|
locale =
|
|
if Application.get_env(:aprsme, :env) == :prod do
|
|
get_locale_from_session(session) || "en"
|
|
end
|
|
|
|
_ =
|
|
if locale do
|
|
Gettext.put_locale(AprsmeWeb.Gettext, locale)
|
|
end
|
|
|
|
# Set map_page assign based on the view module
|
|
map_page = map_page?(socket, params)
|
|
|
|
{:cont, assign(socket, locale: locale, map_page: map_page)}
|
|
end
|
|
|
|
defp get_locale_from_session(session) do
|
|
case session do
|
|
%{"locale" => locale} when locale in @supported_locales ->
|
|
locale
|
|
|
|
_ ->
|
|
nil
|
|
end
|
|
end
|
|
|
|
defp map_page?(socket, _params) do
|
|
# Only check the view module from socket private data
|
|
case socket.private[:phoenix_live_view] do
|
|
%{view: AprsmeWeb.MapLive.Index} -> true
|
|
_ -> false
|
|
end
|
|
end
|
|
end
|