From f8da08bd5318837079ccef5bf29773b49eeb650f Mon Sep 17 00:00:00 2001
From: Rodolfo Carvalho
Date: Mon, 4 Dec 2023 15:52:32 +0100
Subject: [PATCH] Add missing gettext calls in core_components.ex
Depending on the context of the template file, we need to output code
with different syntaxes. To make maintenance easier and keep the
with/without Gettext strings consistent, we add a helper
`maybe_gettext/3` to contextually call gettext or skip it, while
outputting correct syntax. The helper is internal and is never exposed
to the generated code.
Both installer and Phoenix (through phx.gen.live) have a
core_components.ex template file, and those must stay in sync. We don't
want to create a dependency between Phoenix and the installer, so
`maybe_gettext/3` is implemented in both places and, similar to the
core_components.ex template itself, we ensure that `maybe_gettext/3`
implementations stay in sync with automated tests.
---
installer/lib/phx_new/generator.ex | 7 ++-
installer/lib/phx_new/gettext_support.ex | 44 +++++++++++++++++++
.../phx_web/components/core_components.ex | 19 ++++----
.../test/phx_new/gettext_support_test.exs | 21 +++++++++
installer/test/phx_new_test.exs | 13 ++++++
lib/mix/phoenix.ex | 5 +++
lib/mix/phoenix/gettext_support.ex | 44 +++++++++++++++++++
.../templates/phx.gen.live/core_components.ex | 19 ++++----
test/mix/phoenix/gettext_support_test.exs | 21 +++++++++
9 files changed, 173 insertions(+), 20 deletions(-)
create mode 100644 installer/lib/phx_new/gettext_support.ex
create mode 100644 installer/test/phx_new/gettext_support_test.exs
create mode 100644 lib/mix/phoenix/gettext_support.ex
create mode 100644 test/mix/phoenix/gettext_support_test.exs
diff --git a/installer/lib/phx_new/generator.ex b/installer/lib/phx_new/generator.ex
index 1a44ffbe87..cf9ad70785 100644
--- a/installer/lib/phx_new/generator.ex
+++ b/installer/lib/phx_new/generator.ex
@@ -37,8 +37,11 @@ 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_gettext) = &Phx.New.GettextSupport.maybe_gettext/3
+ _ = var!(maybe_gettext)
+ unquote(compiled)
+ end
end
else
quote do
diff --git a/installer/lib/phx_new/gettext_support.ex b/installer/lib/phx_new/gettext_support.ex
new file mode 100644
index 0000000000..c16a43cfcf
--- /dev/null
+++ b/installer/lib/phx_new/gettext_support.ex
@@ -0,0 +1,44 @@
+defmodule Phx.New.GettextSupport do
+ @moduledoc false
+
+ @doc ~S"""
+ Translates a message using Gettext if `gettext?` is true.
+
+ The role provides context and determines which syntax should be used:
+
+ * `:heex_attr` - Used in a HEEx attribute value.
+ * `:eex` - Used in an EEx template.
+
+ ## Examples
+
+ iex> ~s||
+ ~S||
+
+ iex> ~s||
+ ~S||
+
+ iex> ~s|#{maybe_gettext("Hello", :eex, true)}|
+ ~S|<%= gettext("Hello") %>|
+
+ iex> ~s|#{maybe_gettext("Hello", :eex, false)}|
+ ~S|Hello|
+ """
+ @spec maybe_gettext(binary(), :heex_attr | :eex | :ex, boolean()) :: binary()
+ def maybe_gettext(message, role, gettext?)
+
+ def maybe_gettext(message, :heex_attr, gettext?) do
+ if gettext? do
+ ~s|{gettext(#{inspect(message)})}|
+ else
+ inspect(message)
+ end
+ end
+
+ def maybe_gettext(message, :eex, 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..a9d6a38cc6 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_gettext.("close", :heex_attr, @gettext) %>
>
<.icon name="hero-x-mark-solid" class="h-5 w-5" />
@@ -127,7 +127,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
<%%= @title %>