Skip to content

Commit

Permalink
Store the local in :private for all routes
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jul 21, 2022
1 parent abbd845 commit 2c54881
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 2 additions & 9 deletions lib/cldr/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))]
Expand Down
2 changes: 1 addition & 1 deletion lib/cldr/routes/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule CldrRoutes.MixProject do
use Mix.Project

@version "0.4.0"
@version "0.5.0"

def project do
[
Expand Down
10 changes: 5 additions & 5 deletions test/cldr_routes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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

0 comments on commit 2c54881

Please sign in to comment.