Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing gettext calls in core_components.ex #5658

Merged
merged 3 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions installer/lib/phx_new/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,50 @@ 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) = &maybe_heex_attr_gettext/2
_ = var!(maybe_heex_attr_gettext)
var!(maybe_eex_gettext) = &maybe_eex_gettext/2
_ = var!(maybe_eex_gettext)
unquote(compiled)
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|<tag attr=#{maybe_heex_attr_gettext("Hello", true)} />|
# ~S|<tag attr={gettext("Hello")} />|
#
# iex> ~s|<tag attr=#{maybe_heex_attr_gettext("Hello", false)} />|
# ~S|<tag attr="Hello" />|
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|<tag>#{maybe_eex_gettext("Hello", true)}</tag>|
# ~S|<tag><%= gettext("Hello") %></tag>|
#
# iex> ~s|<tag>#{maybe_eex_gettext("Hello", false)}</tag>|
# ~S|<tag>Hello</tag>|
defp maybe_eex_gettext(message, gettext?) do
if gettext? do
~s|<%= gettext(#{inspect(message)}) %>|
else
message
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of defining it inside the quote, define them as regular functions inside PhxNew.Generator which will be imported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

end
else
quote do
Expand Down
19 changes: 10 additions & 9 deletions installer/templates/phx_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
</button>
Expand Down Expand Up @@ -127,7 +127,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
<%%= @title %>
</p>
<p class="mt-2 text-sm leading-5"><%%= msg %></p>
<button type="button" class="group absolute top-1 right-1 p-2" aria-label=<%= if @gettext do %>{gettext("close")}<% else %>"close"<% end %>>
<button type="button" class="group absolute top-1 right-1 p-2" aria-label=<%= maybe_heex_attr_gettext.("close", @gettext) %>>
<.icon name="hero-x-mark-solid" class="h-5 w-5 opacity-40 group-hover:opacity-70" />
</button>
</div>
Expand All @@ -147,28 +147,29 @@ defmodule <%= @web_namespace %>.CoreComponents do
def flash_group(assigns) do
~H"""
<div id={@id}>
<.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>

<.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" />
</.flash>
</div>
Expand Down Expand Up @@ -479,7 +480,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
<tr>
<th :for={col <- @col} class="p-0 pb-4 pr-6 font-normal"><%%= col[:label] %></th>
<th :if={@action != []} class="relative p-0 pb-4">
<span class="sr-only"><%= if @gettext do %><%%= gettext("Actions") %><% else %>Actions<% end %></span>
<span class="sr-only"><%= maybe_eex_gettext.("Actions", @gettext) %></span>
</th>
</tr>
</thead>
Expand Down
13 changes: 13 additions & 0 deletions installer/test/phx_new_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down Expand Up @@ -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"])
Expand Down
43 changes: 43 additions & 0 deletions lib/mix/phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down Expand Up @@ -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|<tag attr=#{maybe_heex_attr_gettext("Hello", true)} />|
# ~S|<tag attr={gettext("Hello")} />|
#
# iex> ~s|<tag attr=#{maybe_heex_attr_gettext("Hello", false)} />|
# ~S|<tag attr="Hello" />|
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|<tag>#{maybe_eex_gettext("Hello", true)}</tag>|
# ~S|<tag><%= gettext("Hello") %></tag>|
#
# iex> ~s|<tag>#{maybe_eex_gettext("Hello", false)}</tag>|
# ~S|<tag>Hello</tag>|
defp maybe_eex_gettext(message, gettext?) do
if gettext? do
~s|<%= gettext(#{inspect(message)}) %>|
else
message
end
end
end
19 changes: 10 additions & 9 deletions priv/templates/phx.gen.live/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
</button>
Expand Down Expand Up @@ -127,7 +127,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
<%%= @title %>
</p>
<p class="mt-2 text-sm leading-5"><%%= msg %></p>
<button type="button" class="group absolute top-1 right-1 p-2" aria-label=<%= if @gettext do %>{gettext("close")}<% else %>"close"<% end %>>
<button type="button" class="group absolute top-1 right-1 p-2" aria-label=<%= maybe_heex_attr_gettext.("close", @gettext) %>>
<.icon name="hero-x-mark-solid" class="h-5 w-5 opacity-40 group-hover:opacity-70" />
</button>
</div>
Expand All @@ -147,28 +147,29 @@ defmodule <%= @web_namespace %>.CoreComponents do
def flash_group(assigns) do
~H"""
<div id={@id}>
<.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>

<.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" />
</.flash>
</div>
Expand Down Expand Up @@ -479,7 +480,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
<tr>
<th :for={col <- @col} class="p-0 pb-4 pr-6 font-normal"><%%= col[:label] %></th>
<th :if={@action != []} class="relative p-0 pb-4">
<span class="sr-only"><%= if @gettext do %><%%= gettext("Actions") %><% else %>Actions<% end %></span>
<span class="sr-only"><%= maybe_eex_gettext.("Actions", @gettext) %></span>
</th>
</tr>
</thead>
Expand Down