From 2c54881b0f65447aa3100dfc81498bc01295cef0 Mon Sep 17 00:00:00 2001 From: Kip Cole Date: Thu, 21 Jul 2022 17:07:20 +0800 Subject: [PATCH] Store the local in :private for all routes --- CHANGELOG.md | 12 ++++++++++++ lib/cldr/route.ex | 11 ++--------- lib/cldr/routes/helpers.ex | 2 +- mix.exs | 2 +- test/cldr_routes_test.exs | 10 +++++----- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc9a9ba..c378f84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## Cldr Routes v0.5.0 + +This is the changelog for Cldr Routes version 0.5.0 released on July 21st, 2022. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_routes/tags) + +### Bug Fixes + +* Propogate locales on the `localize` macro to nested resources + +### Incompatible change + +* The locale is now stored in the `:private` field of the `conn` for both live routes and other routes. + ## Cldr Routes v0.4.0 This is the changelog for Cldr Routes version 0.4.0 released on July 19th, 2022. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_routes/tags) diff --git a/lib/cldr/route.ex b/lib/cldr/route.ex index 4e5bafa..d25a3e3 100644 --- a/lib/cldr/route.ex +++ b/lib/cldr/route.ex @@ -441,14 +441,7 @@ defmodule Cldr.Route do end # Do the actual translations - @template_verbs @localizable_verbs -- [:live] - - defmacro localize(cldr_locale_name, {verb, meta, [path | args]}) when verb in @template_verbs do - cldr_backend = Module.get_attribute(__CALLER__.module, :_cldr_backend) - do_localize(:assigns, cldr_locale_name, cldr_backend, {verb, meta, [path | args]}) - end - - defmacro localize(cldr_locale_name, {:live = verb, meta, [path | args]}) do + defmacro localize(cldr_locale_name, {verb, meta, [path | args]}) when verb in @localizable_verbs do cldr_backend = Module.get_attribute(__CALLER__.module, :_cldr_backend) do_localize(:private, cldr_locale_name, cldr_backend, {verb, meta, [path | args]}) end @@ -726,7 +719,7 @@ defmodule Cldr.Route do defp combine(first, rest, block), do: [block, first | rest] - # Keyword list of options - update or add :assigns + # Keyword list of options - update or add :private defp put_route([{first, _value} | _rest] = options, field, key, value) when is_atom(first) do {field_content, options} = Keyword.pop(options, field) options = [Keyword.put(options, field, put_value(field_content, key, value))] diff --git a/lib/cldr/routes/helpers.ex b/lib/cldr/routes/helpers.ex index a14468a..2e84a26 100644 --- a/lib/cldr/routes/helpers.ex +++ b/lib/cldr/routes/helpers.ex @@ -136,7 +136,7 @@ defmodule Cldr.Route.LocalizedHelpers do end defp localized_route?(route) do - Map.has_key?(route.assigns, :cldr_locale) or Map.has_key?(route.private, :cldr_locale) + Map.has_key?(route.private, :cldr_locale) end defp proxy_helpers(groups, helper_module, cldr_backend) do diff --git a/mix.exs b/mix.exs index 58b3a58..67c1247 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule CldrRoutes.MixProject do use Mix.Project - @version "0.4.0" + @version "0.5.0" def project do [ diff --git a/test/cldr_routes_test.exs b/test/cldr_routes_test.exs index b9c0fc9..8d6941c 100644 --- a/test/cldr_routes_test.exs +++ b/test/cldr_routes_test.exs @@ -64,7 +64,7 @@ defmodule Cldr.Route.Test do assert Map.get(conn.private, :phoenix_action) == :show assert Map.get(conn.private, :phoenix_controller) == PageController assert conn.path_info == ["pages_fr", "1"] - assert %{cldr_locale: %Cldr.LanguageTag{cldr_locale_name: :fr}} = conn.assigns + assert %{cldr_locale: %Cldr.LanguageTag{cldr_locale_name: :fr}} = conn.private end end @@ -123,7 +123,7 @@ defmodule Cldr.Route.Test do # Nested routes to an arbitrary level (testing with 3) localize do - get "/pages/:page", PageController, :show, assigns: %{key: :value} + get "/pages/:page", PageController, :show, private: %{key: :value} end end end) =~ "No known gettext locale for :es" @@ -178,14 +178,14 @@ defmodule Cldr.Route.Test do @endpoint MyApp.Router - test "That assigns propogate to the connection" do + test "That :private propogate to the connection" do {:ok, locale} = MyApp.Cldr.validate_locale(:en) conn = get(build_conn(), "/users/1") - assert conn.assigns.cldr_locale == locale + assert conn.private.cldr_locale == locale {:ok, locale} = MyApp.Cldr.validate_locale(:de) conn = get(build_conn(), "/users_de/1") - assert conn.assigns.cldr_locale == locale + assert conn.private.cldr_locale == locale end end end