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 %>
-
@@ -127,7 +127,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
<%%= @title %>
<%%= msg %>
- {gettext("close")}<% else %>"close"<% end %>>
+ >
<.icon name="hero-x-mark-solid" class="h-5 w-5 opacity-40 group-hover:opacity-70" />
@@ -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) %>
|