From e114b08af44fc1b6b9ec3b53a526eb688bd05ec6 Mon Sep 17 00:00:00 2001 From: Dominic Rubas <1042243+rubas@users.noreply.github.com> Date: Sat, 26 Sep 2026 21:19:49 +0200 Subject: [PATCH] Translate nested resources under a parent with options --- lib/localize/routes.ex | 16 ++++++++++++++++ test/routes_test.exs | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/localize/routes.ex b/lib/localize/routes.ex index eeb71a9..5fed1e4 100644 --- a/lib/localize/routes.ex +++ b/lib/localize/routes.ex @@ -278,6 +278,22 @@ defmodule Localize.Routes do end end + defmacro localize( + locale, + {:resources, _, [path, controller, options, [do: {fun, _, _}] = nested]} + ) + when fun != :localize do + nested = localize_nested_resources(locale, nested) + + quote location: :keep do + localize unquote(locale) do + resources unquote(path), unquote(controller), unquote(options) do + unquote(nested) + end + end + end + end + # Do the actual translations - locale is {%LanguageTag{}, gettext_locale_string} defmacro localize({locale, gettext_locale}, {verb, meta, [path | args]}) when verb in @localizable_verbs do diff --git a/test/routes_test.exs b/test/routes_test.exs index ad387b0..775e587 100644 --- a/test/routes_test.exs +++ b/test/routes_test.exs @@ -1,3 +1,14 @@ +defmodule Localize.Routes.Test.NestedWithOptionsRouter do + use MyAppWeb, :router + use Localize.Routes, gettext: MyApp.Gettext + + localize [:fr] do + resources "/users", UserController, only: [:index] do + resources("/faces", FaceController, only: [:index]) + end + end +end + defmodule Localize.Routes.Test do use ExUnit.Case import Plug.Test @@ -175,4 +186,14 @@ defmodule Localize.Routes.Test do assert header == "" end end + + describe "Nested resources" do + test "a parent with options translates the nested path" do + paths = + Localize.Routes.Test.NestedWithOptionsRouter.__routes__() + |> Enum.map(& &1.path) + + assert paths == ["/users_fr", "/users_fr/:user_id/faces_fr"] + end + end end