Skip to content

Commit 7c386ec

Browse files
rhcarvalhostudzien
authored andcommitted
Add missing gettext calls in core_components.ex (phoenixframework#5658)
1 parent 4349cee commit 7c386ec

File tree

5 files changed

+120
-20
lines changed

5 files changed

+120
-20
lines changed

installer/lib/phx_new/generator.ex

+44-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ defmodule Phx.New.Generator do
3737
@external_resource unquote(path)
3838
@file unquote(path)
3939
def render(unquote(name), unquote(source), var!(assigns))
40-
when is_list(var!(assigns)),
41-
do: unquote(compiled)
40+
when is_list(var!(assigns)) do
41+
var!(maybe_heex_attr_gettext) = &unquote(__MODULE__).maybe_heex_attr_gettext/2
42+
_ = var!(maybe_heex_attr_gettext)
43+
var!(maybe_eex_gettext) = &unquote(__MODULE__).maybe_eex_gettext/2
44+
_ = var!(maybe_eex_gettext)
45+
unquote(compiled)
46+
end
4247
end
4348
else
4449
quote do
@@ -434,4 +439,41 @@ defmodule Phx.New.Generator do
434439

435440
defp random_string(length),
436441
do: :crypto.strong_rand_bytes(length) |> Base.encode64() |> binary_part(0, length)
442+
443+
# In the context of a HEEx attribute value, transforms a given message into a
444+
# dynamic `gettext` call or a fixed-value string attribute, depending on the
445+
# `gettext?` parameter.
446+
#
447+
# ## Examples
448+
#
449+
# iex> ~s|<tag attr=#{maybe_heex_attr_gettext("Hello", true)} />|
450+
# ~S|<tag attr={gettext("Hello")} />|
451+
#
452+
# iex> ~s|<tag attr=#{maybe_heex_attr_gettext("Hello", false)} />|
453+
# ~S|<tag attr="Hello" />|
454+
def maybe_heex_attr_gettext(message, gettext?) do
455+
if gettext? do
456+
~s|{gettext(#{inspect(message)})}|
457+
else
458+
inspect(message)
459+
end
460+
end
461+
462+
# In the context of an EEx template, transforms a given message into a dynamic
463+
# `gettext` call or the message as is, depending on the `gettext?` parameter.
464+
#
465+
# ## Examples
466+
#
467+
# iex> ~s|<tag>#{maybe_eex_gettext("Hello", true)}</tag>|
468+
# ~S|<tag><%= gettext("Hello") %></tag>|
469+
#
470+
# iex> ~s|<tag>#{maybe_eex_gettext("Hello", false)}</tag>|
471+
# ~S|<tag>Hello</tag>|
472+
def maybe_eex_gettext(message, gettext?) do
473+
if gettext? do
474+
~s|<%= gettext(#{inspect(message)}) %>|
475+
else
476+
message
477+
end
478+
end
437479
end

installer/templates/phx_web/components/core_components.ex

+10-9
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
7373
phx-click={JS.exec("data-cancel", to: "##{@id}")}
7474
type="button"
7575
class="-m-3 flex-none p-3 opacity-20 hover:opacity-40"
76-
aria-label=<%= if @gettext do %>{gettext("close")}<% else %>"close"<% end %>
76+
aria-label=<%= maybe_heex_attr_gettext.("close", @gettext) %>
7777
>
7878
<.icon name="hero-x-mark-solid" class="h-5 w-5" />
7979
</button>
@@ -127,7 +127,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
127127
<%%= @title %>
128128
</p>
129129
<p class="mt-2 text-sm leading-5"><%%= msg %></p>
130-
<button type="button" class="group absolute top-1 right-1 p-2" aria-label=<%= if @gettext do %>{gettext("close")}<% else %>"close"<% end %>>
130+
<button type="button" class="group absolute top-1 right-1 p-2" aria-label=<%= maybe_heex_attr_gettext.("close", @gettext) %>>
131131
<.icon name="hero-x-mark-solid" class="h-5 w-5 opacity-40 group-hover:opacity-70" />
132132
</button>
133133
</div>
@@ -147,28 +147,29 @@ defmodule <%= @web_namespace %>.CoreComponents do
147147
def flash_group(assigns) do
148148
~H"""
149149
<div id={@id}>
150-
<.flash kind={:info} title="Success!" flash={@flash} />
151-
<.flash kind={:error} title="Error!" flash={@flash} />
150+
<.flash kind={:info} title=<%= maybe_heex_attr_gettext.("Success!", @gettext) %> flash={@flash} />
151+
<.flash kind={:error} title=<%= maybe_heex_attr_gettext.("Error!", @gettext) %> flash={@flash} />
152152
<.flash
153153
id="client-error"
154154
kind={:error}
155-
title="We can't find the internet"
155+
title=<%= maybe_heex_attr_gettext.("We can't find the internet", @gettext) %>
156156
phx-disconnected={show(".phx-client-error #client-error")}
157157
phx-connected={hide("#client-error")}
158158
hidden
159159
>
160-
Attempting to reconnect <.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" />
160+
<%= maybe_eex_gettext.("Attempting to reconnect", @gettext) %>
161+
<.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" />
161162
</.flash>
162163
163164
<.flash
164165
id="server-error"
165166
kind={:error}
166-
title="Something went wrong!"
167+
title=<%= maybe_heex_attr_gettext.("Something went wrong!", @gettext) %>
167168
phx-disconnected={show(".phx-server-error #server-error")}
168169
phx-connected={hide("#server-error")}
169170
hidden
170171
>
171-
Hang in there while we get back on track
172+
<%= maybe_eex_gettext.("Hang in there while we get back on track", @gettext) %>
172173
<.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" />
173174
</.flash>
174175
</div>
@@ -479,7 +480,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
479480
<tr>
480481
<th :for={col <- @col} class="p-0 pb-4 pr-6 font-normal"><%%= col[:label] %></th>
481482
<th :if={@action != []} class="relative p-0 pb-4">
482-
<span class="sr-only"><%= if @gettext do %><%%= gettext("Actions") %><% else %>Actions<% end %></span>
483+
<span class="sr-only"><%= maybe_eex_gettext.("Actions", @gettext) %></span>
483484
</th>
484485
</tr>
485486
</thead>

installer/test/phx_new_test.exs

+13
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ defmodule Mix.Tasks.Phx.NewTest do
110110

111111
assert_file("phx_blog/lib/phx_blog_web/components/core_components.ex", fn file ->
112112
assert file =~ "defmodule PhxBlogWeb.CoreComponents"
113+
assert file =~ ~S|aria-label={gettext("close")}|
114+
assert file =~ ~S|<.flash kind={:info} title={gettext("Success!")} flash={@flash} />|
113115
end)
114116

115117
assert_file("phx_blog/lib/phx_blog_web/components/layouts.ex", fn file ->
@@ -550,6 +552,17 @@ defmodule Mix.Tasks.Phx.NewTest do
550552
end)
551553
end
552554

555+
test "new with --no-gettext" do
556+
in_tmp("new with no_gettext", fn ->
557+
Mix.Tasks.Phx.New.run([@app_name, "--no-gettext"])
558+
559+
assert_file("phx_blog/lib/phx_blog_web/components/core_components.ex", fn file ->
560+
assert file =~ ~S|aria-label="close"|
561+
assert file =~ ~S|<.flash kind={:info} title="Success!" flash={@flash} />|
562+
end)
563+
end)
564+
end
565+
553566
test "new with binary_id" do
554567
in_tmp("new with binary_id", fn ->
555568
Mix.Tasks.Phx.New.run([@app_name, "--binary-id"])

lib/mix/phoenix.ex

+43
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ defmodule Mix.Phoenix do
2929
def copy_from(apps, source_dir, binding, mapping) when is_list(mapping) do
3030
roots = Enum.map(apps, &to_app_source(&1, source_dir))
3131

32+
binding =
33+
Keyword.merge(binding,
34+
maybe_heex_attr_gettext: &maybe_heex_attr_gettext/2,
35+
maybe_eex_gettext: &maybe_eex_gettext/2
36+
)
37+
3238
for {format, source_file_path, target} <- mapping do
3339
source =
3440
Enum.find_value(roots, fn root ->
@@ -359,4 +365,41 @@ defmodule Mix.Phoenix do
359365
def prepend_newline(string) do
360366
"\n" <> string
361367
end
368+
369+
# In the context of a HEEx attribute value, transforms a given message into a
370+
# dynamic `gettext` call or a fixed-value string attribute, depending on the
371+
# `gettext?` parameter.
372+
#
373+
# ## Examples
374+
#
375+
# iex> ~s|<tag attr=#{maybe_heex_attr_gettext("Hello", true)} />|
376+
# ~S|<tag attr={gettext("Hello")} />|
377+
#
378+
# iex> ~s|<tag attr=#{maybe_heex_attr_gettext("Hello", false)} />|
379+
# ~S|<tag attr="Hello" />|
380+
defp maybe_heex_attr_gettext(message, gettext?) do
381+
if gettext? do
382+
~s|{gettext(#{inspect(message)})}|
383+
else
384+
inspect(message)
385+
end
386+
end
387+
388+
# In the context of an EEx template, transforms a given message into a dynamic
389+
# `gettext` call or the message as is, depending on the `gettext?` parameter.
390+
#
391+
# ## Examples
392+
#
393+
# iex> ~s|<tag>#{maybe_eex_gettext("Hello", true)}</tag>|
394+
# ~S|<tag><%= gettext("Hello") %></tag>|
395+
#
396+
# iex> ~s|<tag>#{maybe_eex_gettext("Hello", false)}</tag>|
397+
# ~S|<tag>Hello</tag>|
398+
defp maybe_eex_gettext(message, gettext?) do
399+
if gettext? do
400+
~s|<%= gettext(#{inspect(message)}) %>|
401+
else
402+
message
403+
end
404+
end
362405
end

priv/templates/phx.gen.live/core_components.ex

+10-9
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
7373
phx-click={JS.exec("data-cancel", to: "##{@id}")}
7474
type="button"
7575
class="-m-3 flex-none p-3 opacity-20 hover:opacity-40"
76-
aria-label=<%= if @gettext do %>{gettext("close")}<% else %>"close"<% end %>
76+
aria-label=<%= maybe_heex_attr_gettext.("close", @gettext) %>
7777
>
7878
<.icon name="hero-x-mark-solid" class="h-5 w-5" />
7979
</button>
@@ -127,7 +127,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
127127
<%%= @title %>
128128
</p>
129129
<p class="mt-2 text-sm leading-5"><%%= msg %></p>
130-
<button type="button" class="group absolute top-1 right-1 p-2" aria-label=<%= if @gettext do %>{gettext("close")}<% else %>"close"<% end %>>
130+
<button type="button" class="group absolute top-1 right-1 p-2" aria-label=<%= maybe_heex_attr_gettext.("close", @gettext) %>>
131131
<.icon name="hero-x-mark-solid" class="h-5 w-5 opacity-40 group-hover:opacity-70" />
132132
</button>
133133
</div>
@@ -147,28 +147,29 @@ defmodule <%= @web_namespace %>.CoreComponents do
147147
def flash_group(assigns) do
148148
~H"""
149149
<div id={@id}>
150-
<.flash kind={:info} title="Success!" flash={@flash} />
151-
<.flash kind={:error} title="Error!" flash={@flash} />
150+
<.flash kind={:info} title=<%= maybe_heex_attr_gettext.("Success!", @gettext) %> flash={@flash} />
151+
<.flash kind={:error} title=<%= maybe_heex_attr_gettext.("Error!", @gettext) %> flash={@flash} />
152152
<.flash
153153
id="client-error"
154154
kind={:error}
155-
title="We can't find the internet"
155+
title=<%= maybe_heex_attr_gettext.("We can't find the internet", @gettext) %>
156156
phx-disconnected={show(".phx-client-error #client-error")}
157157
phx-connected={hide("#client-error")}
158158
hidden
159159
>
160-
Attempting to reconnect <.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" />
160+
<%= maybe_eex_gettext.("Attempting to reconnect", @gettext) %>
161+
<.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" />
161162
</.flash>
162163
163164
<.flash
164165
id="server-error"
165166
kind={:error}
166-
title="Something went wrong!"
167+
title=<%= maybe_heex_attr_gettext.("Something went wrong!", @gettext) %>
167168
phx-disconnected={show(".phx-server-error #server-error")}
168169
phx-connected={hide("#server-error")}
169170
hidden
170171
>
171-
Hang in there while we get back on track
172+
<%= maybe_eex_gettext.("Hang in there while we get back on track", @gettext) %>
172173
<.icon name="hero-arrow-path" class="ml-1 h-3 w-3 animate-spin" />
173174
</.flash>
174175
</div>
@@ -479,7 +480,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
479480
<tr>
480481
<th :for={col <- @col} class="p-0 pb-4 pr-6 font-normal"><%%= col[:label] %></th>
481482
<th :if={@action != []} class="relative p-0 pb-4">
482-
<span class="sr-only"><%= if @gettext do %><%%= gettext("Actions") %><% else %>Actions<% end %></span>
483+
<span class="sr-only"><%= maybe_eex_gettext.("Actions", @gettext) %></span>
483484
</th>
484485
</tr>
485486
</thead>

0 commit comments

Comments
 (0)