diff --git a/lib/teslamate_web/cldr.ex b/lib/teslamate_web/cldr.ex index c3203540..67d1165a 100644 --- a/lib/teslamate_web/cldr.ex +++ b/lib/teslamate_web/cldr.ex @@ -5,5 +5,5 @@ defmodule TeslaMateWeb.Cldr do otp_app: :teslamate, providers: [], generate_docs: false, - force_locale_download: Mix.env() == :prod + force_locale_download: Mix.env() in [:prod, :test] end diff --git a/lib/teslamate_web/live/car_live/index.ex b/lib/teslamate_web/live/car_live/index.ex index 83587121..074095da 100644 --- a/lib/teslamate_web/live/car_live/index.ex +++ b/lib/teslamate_web/live/car_live/index.ex @@ -6,11 +6,13 @@ defmodule TeslaMateWeb.CarLive.Index do alias TeslaMate.{Settings, Vehicles} alias TeslaMate.Settings.GlobalSettings + on_mount {TeslaMateWeb.InitAssigns, :locale} + @impl true - def mount(_params, %{"settings" => settings, "locale" => locale}, socket) do + def mount(_params, %{"settings" => settings}, socket) do socket = socket - |> assign(locale: locale, page_title: gettext("Home")) + |> assign(page_title: gettext("Home")) |> assign_new(:summaries, fn -> Vehicles.list() end) |> assign_new(:settings, fn -> update_base_url(settings, socket) end) diff --git a/lib/teslamate_web/live/car_live/summary.ex b/lib/teslamate_web/live/car_live/summary.ex index 6a881f6f..098bf55c 100644 --- a/lib/teslamate_web/live/car_live/summary.ex +++ b/lib/teslamate_web/live/car_live/summary.ex @@ -7,11 +7,11 @@ defmodule TeslaMateWeb.CarLive.Summary do alias TeslaMate.Vehicles.Vehicle alias TeslaMate.{Vehicles, Convert} + on_mount {TeslaMateWeb.InitAssigns, :locale} + @impl true def mount(_params, %{"summary" => %Summary{car: car} = summary} = session, socket) do if connected?(socket) do - Gettext.put_locale(session["locale"]) - send(self(), :update_duration) send(self(), {:status, Vehicle.busy?(car.id)}) diff --git a/lib/teslamate_web/live/charge_live/cost.ex b/lib/teslamate_web/live/charge_live/cost.ex index 7e8706b8..c53a9b61 100644 --- a/lib/teslamate_web/live/charge_live/cost.ex +++ b/lib/teslamate_web/live/charge_live/cost.ex @@ -9,10 +9,10 @@ defmodule TeslaMateWeb.ChargeLive.Cost do import TeslaMateWeb.Gettext - @impl true - def mount(%{"id" => id}, %{"locale" => locale}, socket) do - if connected?(socket), do: Gettext.put_locale(locale) + on_mount {TeslaMateWeb.InitAssigns, :locale} + @impl true + def mount(%{"id" => id}, _session, socket) do charging_process = Log.get_charging_process!(id) socket = diff --git a/lib/teslamate_web/live/geofence_live/form.ex b/lib/teslamate_web/live/geofence_live/form.ex index dd2162b8..90c5f1db 100644 --- a/lib/teslamate_web/live/geofence_live/form.ex +++ b/lib/teslamate_web/live/geofence_live/form.ex @@ -10,26 +10,16 @@ defmodule TeslaMateWeb.GeoFenceLive.Form do alias TeslaMate.Locations.GeoFence alias TeslaMate.Log.Position + on_mount {TeslaMateWeb.InitAssigns, :locale} + @impl true - def mount(%{"id" => id}, session, socket) do - %{"settings" => settings, "locale" => locale} = session - - if connected?(socket) do - Gettext.put_locale(locale) - end - + def mount(%{"id" => id}, %{"settings" => settings}, socket) do geofence = Locations.get_geofence!(id) {:ok, base_assigns(socket, geofence, settings, :edit)} end - def mount(%{"lat" => lat, "lng" => lng}, session, socket) do - %{"settings" => settings, "locale" => locale} = session - - if connected?(socket) do - Gettext.put_locale(locale) - end - + def mount(%{"lat" => lat, "lng" => lng}, %{"settings" => settings}, socket) do {:ok, settings} = set_grafana_url(settings, socket) geofence = %GeoFence{ @@ -41,13 +31,7 @@ defmodule TeslaMateWeb.GeoFenceLive.Form do {:ok, base_assigns(socket, geofence, settings, :new)} end - def mount(_params, session, socket) do - %{"settings" => settings, "locale" => locale} = session - - if connected?(socket) do - Gettext.put_locale(locale) - end - + def mount(_params, %{"settings" => settings}, socket) do %{latitude: lat, longitude: lng} = case Log.get_latest_position() do %Position{latitude: lat, longitude: lng} -> %{latitude: lat, longitude: lng} diff --git a/lib/teslamate_web/live/geofence_live/index.ex b/lib/teslamate_web/live/geofence_live/index.ex index 41e4bf9f..27516d9d 100644 --- a/lib/teslamate_web/live/geofence_live/index.ex +++ b/lib/teslamate_web/live/geofence_live/index.ex @@ -6,10 +6,10 @@ defmodule TeslaMateWeb.GeoFenceLive.Index do alias TeslaMate.Convert - @impl true - def mount(_params, %{"settings" => settings, "locale" => locale}, socket) do - if connected?(socket), do: Gettext.put_locale(locale) + on_mount {TeslaMateWeb.InitAssigns, :locale} + @impl true + def mount(_params, %{"settings" => settings}, socket) do unit_of_length = case settings do %GlobalSettings{unit_of_length: :km} -> :m diff --git a/lib/teslamate_web/live/import_live/index.ex b/lib/teslamate_web/live/import_live/index.ex index 8d101653..281882a0 100644 --- a/lib/teslamate_web/live/import_live/index.ex +++ b/lib/teslamate_web/live/import_live/index.ex @@ -13,11 +13,12 @@ defmodule TeslaMateWeb.ImportLive.Index do alias TeslaMate.Import + on_mount {TeslaMateWeb.InitAssigns, :locale} + @impl true - def mount(_params, %{"settings" => _, "locale" => locale}, socket) do + def mount(_params, %{"settings" => _}, socket) do tz = if connected?(socket) do - Gettext.put_locale(locale) :ok = Import.subscribe() get_connect_params(socket)["tz"] end diff --git a/lib/teslamate_web/live/init_assigns.ex b/lib/teslamate_web/live/init_assigns.ex new file mode 100644 index 00000000..a28793e6 --- /dev/null +++ b/lib/teslamate_web/live/init_assigns.ex @@ -0,0 +1,12 @@ +defmodule TeslaMateWeb.InitAssigns do + @moduledoc """ + Ensures common `assigns` are applied to all LiveViews attaching this hook. + """ + + import Phoenix.LiveView + + def on_mount(:locale, _params, %{"cldr_locale" => locale}, socket) do + Gettext.put_locale(locale) + {:cont, assign(socket, :locale, locale)} + end +end diff --git a/lib/teslamate_web/live/settings_live/index.ex b/lib/teslamate_web/live/settings_live/index.ex index e3e01520..5fc81762 100644 --- a/lib/teslamate_web/live/settings_live/index.ex +++ b/lib/teslamate_web/live/settings_live/index.ex @@ -6,12 +6,11 @@ defmodule TeslaMateWeb.SettingsLive.Index do alias TeslaMate.Settings.{GlobalSettings, CarSettings} alias TeslaMate.{Settings, Updater, Api} - @impl true - def mount(_params, %{"settings" => settings, "locale" => locale}, socket) do - if connected?(socket), do: Gettext.put_locale(locale) + on_mount {TeslaMateWeb.InitAssigns, :locale} + @impl true + def mount(_params, %{"settings" => settings}, socket) do assigns = %{ - locale: locale, addresses_migrated?: addresses_migrated?(), car_settings: Settings.get_car_settings() |> prepare(), car: nil, diff --git a/lib/teslamate_web/live/signin_live/index.ex b/lib/teslamate_web/live/signin_live/index.ex index 4f3f2633..ea76f8e9 100644 --- a/lib/teslamate_web/live/signin_live/index.ex +++ b/lib/teslamate_web/live/signin_live/index.ex @@ -99,10 +99,10 @@ defmodule TeslaMateWeb.SignInLive.Index do end end - @impl true - def mount(_params, %{"locale" => locale}, socket) do - if connected?(socket), do: Gettext.put_locale(locale) + on_mount {TeslaMateWeb.InitAssigns, :locale} + @impl true + def mount(_params, _session, socket) do assigns = %{ api: get_api(socket), page_title: gettext("Sign in"), diff --git a/lib/teslamate_web/plugs/put_locle_in_session.ex b/lib/teslamate_web/plugs/put_locle_in_session.ex deleted file mode 100644 index 17015e8b..00000000 --- a/lib/teslamate_web/plugs/put_locle_in_session.ex +++ /dev/null @@ -1,27 +0,0 @@ -defmodule TeslaMateWeb.Plugs.PutLocaleInSession do - @moduledoc """ - Puts the CLDR and Gettext locale names in the session. - - (Based on Cldr.Plug.PutSession) - """ - - import Plug.Conn - alias Cldr.Plug.SetLocale - - @doc false - def init(_options), do: [] - - @doc false - def call(conn, _options) do - case SetLocale.get_cldr_locale(conn) do - %Cldr.LanguageTag{} = tag -> - conn - |> fetch_session() - |> put_session(SetLocale.session_key(), tag.cldr_locale_name) - |> put_session("locale", tag.gettext_locale_name) - - _other -> - conn - end - end -end diff --git a/lib/teslamate_web/router.ex b/lib/teslamate_web/router.ex index def8c0e2..1b63d381 100644 --- a/lib/teslamate_web/router.ex +++ b/lib/teslamate_web/router.ex @@ -1,7 +1,7 @@ defmodule TeslaMateWeb.Router do use TeslaMateWeb, :router - alias TeslaMateWeb.Plugs.{Donate, PutLocaleInSession} + alias TeslaMateWeb.Plugs.Donate alias TeslaMate.Settings pipeline :browser do @@ -19,7 +19,7 @@ defmodule TeslaMateWeb.Router do gettext: TeslaMateWeb.Gettext, cldr: TeslaMateWeb.Cldr - plug PutLocaleInSession + plug Cldr.Plug.PutSession plug :put_root_layout, {TeslaMateWeb.LayoutView, :root} plug :protect_from_forgery diff --git a/mix.exs b/mix.exs index 6e4733fb..43061f7b 100644 --- a/mix.exs +++ b/mix.exs @@ -37,7 +37,7 @@ defmodule TeslaMate.MixProject do [ {:castore, "~> 0.1"}, {:ecto_sql, "~> 3.0"}, - {:ex_cldr, "~> 2.23.0"}, + {:ex_cldr, "~> 2.24.1"}, {:excoveralls, "~> 0.10", only: :test}, {:finch, "~> 0.3"}, {:floki, "~> 0.23"}, diff --git a/mix.lock b/mix.lock index 959f329f..da571071 100644 --- a/mix.lock +++ b/mix.lock @@ -11,7 +11,7 @@ "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, "ecto": {:hex, :ecto, "3.7.1", "a20598862351b29f80f285b21ec5297da1181c0442687f9b8329f0445d228892", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d36e5b39fc479e654cffd4dbe1865d9716e4a9b6311faff799b6f90ab81b8638"}, "ecto_sql": {:hex, :ecto_sql, "3.7.1", "8de624ef50b2a8540252d8c60506379fbbc2707be1606853df371cf53df5d053", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.4.0 or ~> 0.5.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2b42a32e2ce92f64aba5c88617891ab3b0ba34f3f3a503fa20009eae1a401c81"}, - "ex_cldr": {:hex, :ex_cldr, "2.23.2", "76c51b722cefdcd1a13eb5e7c7f4da5b9acfd64ff054424a977ff6e2d6a78981", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:cldr_utils, "~> 2.15", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.13", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "d9ce03c8d3fdc7ab751bdb2be742b6972f94adc856d51dfe5bb06a51ac96b8f4"}, + "ex_cldr": {:hex, :ex_cldr, "2.24.1", "159b55b29b2f32897689c4edfcf699761c28d626455642514794a4848c7092d5", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:cldr_utils, "~> 2.17", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.13", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "82a44fab6a09f6481e8dd869c27f0cad783dfe5ff83cb7eef42d556a5134275e"}, "excoveralls": {:hex, :excoveralls, "0.14.4", "295498f1ae47bdc6dce59af9a585c381e1aefc63298d48172efaaa90c3d251db", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e3ab02f2df4c1c7a519728a6f0a747e71d7d6e846020aae338173619217931c1"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, "finch": {:hex, :finch, "0.9.0", "8b772324aebafcaba763f1dffaa3e7f52f8c4e52485f50f48bbb2f42219a2e87", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a93bfcad9ca50fa3cb2d459f27667d9a87cfbb7fecf9b29b2e78a50bc2ab445d"}, @@ -37,7 +37,7 @@ "phoenix_ecto": {:hex, :phoenix_ecto, "4.4.0", "0672ed4e4808b3fbed494dded89958e22fb882de47a97634c0b13e7b0b5f7720", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "09864e558ed31ee00bd48fcc1d4fc58ae9678c9e81649075431e69dbabb43cc1"}, "phoenix_html": {:hex, :phoenix_html, "3.1.0", "0b499df05aad27160d697a9362f0e89fa0e24d3c7a9065c2bd9d38b4d1416c09", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "0c0a98a2cefa63433657983a2a594c7dee5927e4391e0f1bfd3a151d1def33fc"}, "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.3.3", "3a53772a6118d5679bf50fc1670505a290e32a1d195df9e069d8c53ab040c054", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "766796676e5f558dbae5d1bdb066849673e956005e3730dfd5affd7a6da4abac"}, - "phoenix_live_view": {:hex, :phoenix_live_view, "0.17.3", "2691f3b7923c39029452c236c9a6137329fe9c41f7a0c142922cb5be274a4e11", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.5.9 or ~> 1.6.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.1", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "55b36cdb483e261d3b3213d24cc23ab9b3c7855e8b967ba8790a69fd3486db62"}, + "phoenix_live_view": {:hex, :phoenix_live_view, "0.17.5", "63f52a6f9f6983f04e424586ff897c016ecc5e4f8d1e2c22c2887af1c57215d8", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.5.9 or ~> 1.6.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.1", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c5586e6a3d4df71b8214c769d4f5eb8ece2b4001711a7ca0f97323c36958b0e3"}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"}, "phoenix_view": {:hex, :phoenix_view, "1.0.0", "fea71ecaaed71178b26dd65c401607de5ec22e2e9ef141389c721b3f3d4d8011", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "82be3e2516f5633220246e2e58181282c71640dab7afc04f70ad94253025db0c"}, "plug": {:hex, :plug, "1.12.1", "645678c800601d8d9f27ad1aebba1fdb9ce5b2623ddb961a074da0b96c35187d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d57e799a777bc20494b784966dc5fbda91eb4a09f571f76545b72a634ce0d30b"},