From 7c386ecb6611326a5353a0486ced0698677956e7 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Wed, 27 Dec 2023 21:15:43 +0100 Subject: [PATCH] Add missing gettext calls in core_components.ex (#5658) --- installer/lib/phx_new/generator.ex | 46 ++++++++++++++++++- .../phx_web/components/core_components.ex | 19 ++++---- installer/test/phx_new_test.exs | 13 ++++++ lib/mix/phoenix.ex | 43 +++++++++++++++++ .../templates/phx.gen.live/core_components.ex | 19 ++++---- 5 files changed, 120 insertions(+), 20 deletions(-) diff --git a/installer/lib/phx_new/generator.ex b/installer/lib/phx_new/generator.ex index 1a44ffbe87..de8284e2e5 100644 --- a/installer/lib/phx_new/generator.ex +++ b/installer/lib/phx_new/generator.ex @@ -37,8 +37,13 @@ defmodule Phx.New.Generator do @external_resource unquote(path) @file unquote(path) def render(unquote(name), unquote(source), var!(assigns)) - when is_list(var!(assigns)), - do: unquote(compiled) + when is_list(var!(assigns)) do + var!(maybe_heex_attr_gettext) = &unquote(__MODULE__).maybe_heex_attr_gettext/2 + _ = var!(maybe_heex_attr_gettext) + var!(maybe_eex_gettext) = &unquote(__MODULE__).maybe_eex_gettext/2 + _ = var!(maybe_eex_gettext) + unquote(compiled) + end end else quote do @@ -434,4 +439,41 @@ defmodule Phx.New.Generator do defp random_string(length), do: :crypto.strong_rand_bytes(length) |> Base.encode64() |> binary_part(0, length) + + # In the context of a HEEx attribute value, transforms a given message into a + # dynamic `gettext` call or a fixed-value string attribute, depending on the + # `gettext?` parameter. + # + # ## Examples + # + # iex> ~s|| + # ~S|| + # + # iex> ~s|| + # ~S|| + def maybe_heex_attr_gettext(message, gettext?) do + if gettext? do + ~s|{gettext(#{inspect(message)})}| + else + inspect(message) + end + end + + # In the context of an EEx template, transforms a given message into a dynamic + # `gettext` call or the message as is, depending on the `gettext?` parameter. + # + # ## Examples + # + # iex> ~s|#{maybe_eex_gettext("Hello", true)}| + # ~S|<%= gettext("Hello") %>| + # + # iex> ~s|#{maybe_eex_gettext("Hello", false)}| + # ~S|Hello| + def maybe_eex_gettext(message, gettext?) do + if gettext? do + ~s|<%= gettext(#{inspect(message)}) %>| + else + message + end + end end diff --git a/installer/templates/phx_web/components/core_components.ex b/installer/templates/phx_web/components/core_components.ex index 6d5841ea48..1a095dd8be 100644 --- a/installer/templates/phx_web/components/core_components.ex +++ b/installer/templates/phx_web/components/core_components.ex @@ -73,7 +73,7 @@ defmodule <%= @web_namespace %>.CoreComponents do phx-click={JS.exec("data-cancel", to: "##{@id}")} type="button" class="-m-3 flex-none p-3 opacity-20 hover:opacity-40" - aria-label=<%= if @gettext do %>{gettext("close")}<% else %>"close"<% end %> + aria-label=<%= maybe_heex_attr_gettext.("close", @gettext) %> > <.icon name="hero-x-mark-solid" class="h-5 w-5" /> @@ -127,7 +127,7 @@ defmodule <%= @web_namespace %>.CoreComponents do <%%= @title %>

<%%= msg %>

- @@ -147,28 +147,29 @@ defmodule <%= @web_namespace %>.CoreComponents do def flash_group(assigns) do ~H"""
- <.flash kind={:info} title="Success!" flash={@flash} /> - <.flash kind={:error} title="Error!" flash={@flash} /> + <.flash kind={:info} title=<%= maybe_heex_attr_gettext.("Success!", @gettext) %> flash={@flash} /> + <.flash kind={:error} title=<%= maybe_heex_attr_gettext.("Error!", @gettext) %> flash={@flash} /> <.flash id="client-error" kind={:error} - title="We can't find the internet" + title=<%= maybe_heex_attr_gettext.("We can't find the internet", @gettext) %> phx-disconnected={show(".phx-client-error #client-error")} phx-connected={hide("#client-error")} hidden > - Attempting to reconnect <.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" /> + <%= maybe_eex_gettext.("Attempting to reconnect", @gettext) %> + <.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" /> <.flash id="server-error" kind={:error} - title="Something went wrong!" + title=<%= maybe_heex_attr_gettext.("Something went wrong!", @gettext) %> phx-disconnected={show(".phx-server-error #server-error")} phx-connected={hide("#server-error")} hidden > - Hang in there while we get back on track + <%= maybe_eex_gettext.("Hang in there while we get back on track", @gettext) %> <.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" />
@@ -479,7 +480,7 @@ defmodule <%= @web_namespace %>.CoreComponents do <%%= col[:label] %> - <%= if @gettext do %><%%= gettext("Actions") %><% else %>Actions<% end %> + <%= maybe_eex_gettext.("Actions", @gettext) %> diff --git a/installer/test/phx_new_test.exs b/installer/test/phx_new_test.exs index ae1af81647..a09572c77f 100644 --- a/installer/test/phx_new_test.exs +++ b/installer/test/phx_new_test.exs @@ -110,6 +110,8 @@ defmodule Mix.Tasks.Phx.NewTest do assert_file("phx_blog/lib/phx_blog_web/components/core_components.ex", fn file -> assert file =~ "defmodule PhxBlogWeb.CoreComponents" + assert file =~ ~S|aria-label={gettext("close")}| + assert file =~ ~S|<.flash kind={:info} title={gettext("Success!")} flash={@flash} />| end) assert_file("phx_blog/lib/phx_blog_web/components/layouts.ex", fn file -> @@ -550,6 +552,17 @@ defmodule Mix.Tasks.Phx.NewTest do end) end + test "new with --no-gettext" do + in_tmp("new with no_gettext", fn -> + Mix.Tasks.Phx.New.run([@app_name, "--no-gettext"]) + + assert_file("phx_blog/lib/phx_blog_web/components/core_components.ex", fn file -> + assert file =~ ~S|aria-label="close"| + assert file =~ ~S|<.flash kind={:info} title="Success!" flash={@flash} />| + end) + end) + end + test "new with binary_id" do in_tmp("new with binary_id", fn -> Mix.Tasks.Phx.New.run([@app_name, "--binary-id"]) diff --git a/lib/mix/phoenix.ex b/lib/mix/phoenix.ex index 39d3b000a6..7c6b8e6d53 100644 --- a/lib/mix/phoenix.ex +++ b/lib/mix/phoenix.ex @@ -29,6 +29,12 @@ defmodule Mix.Phoenix do def copy_from(apps, source_dir, binding, mapping) when is_list(mapping) do roots = Enum.map(apps, &to_app_source(&1, source_dir)) + binding = + Keyword.merge(binding, + maybe_heex_attr_gettext: &maybe_heex_attr_gettext/2, + maybe_eex_gettext: &maybe_eex_gettext/2 + ) + for {format, source_file_path, target} <- mapping do source = Enum.find_value(roots, fn root -> @@ -359,4 +365,41 @@ defmodule Mix.Phoenix do def prepend_newline(string) do "\n" <> string end + + # In the context of a HEEx attribute value, transforms a given message into a + # dynamic `gettext` call or a fixed-value string attribute, depending on the + # `gettext?` parameter. + # + # ## Examples + # + # iex> ~s|| + # ~S|| + # + # iex> ~s|| + # ~S|| + defp maybe_heex_attr_gettext(message, gettext?) do + if gettext? do + ~s|{gettext(#{inspect(message)})}| + else + inspect(message) + end + end + + # In the context of an EEx template, transforms a given message into a dynamic + # `gettext` call or the message as is, depending on the `gettext?` parameter. + # + # ## Examples + # + # iex> ~s|#{maybe_eex_gettext("Hello", true)}| + # ~S|<%= gettext("Hello") %>| + # + # iex> ~s|#{maybe_eex_gettext("Hello", false)}| + # ~S|Hello| + defp maybe_eex_gettext(message, gettext?) do + if gettext? do + ~s|<%= gettext(#{inspect(message)}) %>| + else + message + end + end end diff --git a/priv/templates/phx.gen.live/core_components.ex b/priv/templates/phx.gen.live/core_components.ex index 6d5841ea48..1a095dd8be 100644 --- a/priv/templates/phx.gen.live/core_components.ex +++ b/priv/templates/phx.gen.live/core_components.ex @@ -73,7 +73,7 @@ defmodule <%= @web_namespace %>.CoreComponents do phx-click={JS.exec("data-cancel", to: "##{@id}")} type="button" class="-m-3 flex-none p-3 opacity-20 hover:opacity-40" - aria-label=<%= if @gettext do %>{gettext("close")}<% else %>"close"<% end %> + aria-label=<%= maybe_heex_attr_gettext.("close", @gettext) %> > <.icon name="hero-x-mark-solid" class="h-5 w-5" /> @@ -127,7 +127,7 @@ defmodule <%= @web_namespace %>.CoreComponents do <%%= @title %>

<%%= msg %>

- @@ -147,28 +147,29 @@ defmodule <%= @web_namespace %>.CoreComponents do def flash_group(assigns) do ~H"""
- <.flash kind={:info} title="Success!" flash={@flash} /> - <.flash kind={:error} title="Error!" flash={@flash} /> + <.flash kind={:info} title=<%= maybe_heex_attr_gettext.("Success!", @gettext) %> flash={@flash} /> + <.flash kind={:error} title=<%= maybe_heex_attr_gettext.("Error!", @gettext) %> flash={@flash} /> <.flash id="client-error" kind={:error} - title="We can't find the internet" + title=<%= maybe_heex_attr_gettext.("We can't find the internet", @gettext) %> phx-disconnected={show(".phx-client-error #client-error")} phx-connected={hide("#client-error")} hidden > - Attempting to reconnect <.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" /> + <%= maybe_eex_gettext.("Attempting to reconnect", @gettext) %> + <.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" /> <.flash id="server-error" kind={:error} - title="Something went wrong!" + title=<%= maybe_heex_attr_gettext.("Something went wrong!", @gettext) %> phx-disconnected={show(".phx-server-error #server-error")} phx-connected={hide("#server-error")} hidden > - Hang in there while we get back on track + <%= maybe_eex_gettext.("Hang in there while we get back on track", @gettext) %> <.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" />
@@ -479,7 +480,7 @@ defmodule <%= @web_namespace %>.CoreComponents do <%%= col[:label] %> - <%= if @gettext do %><%%= gettext("Actions") %><% else %>Actions<% end %> + <%= maybe_eex_gettext.("Actions", @gettext) %>