-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
2196ab5
commit f8da08b
Showing
9 changed files
with
173 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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|<tag attr=#{maybe_gettext("Hello", :heex_attr, true)} />| | ||
~S|<tag attr={gettext("Hello")} />| | ||
iex> ~s|<tag attr=#{maybe_gettext("Hello", :heex_attr, false)} />| | ||
~S|<tag attr="Hello" />| | ||
iex> ~s|<tag>#{maybe_gettext("Hello", :eex, true)}</tag>| | ||
~S|<tag><%= gettext("Hello") %></tag>| | ||
iex> ~s|<tag>#{maybe_gettext("Hello", :eex, false)}</tag>| | ||
~S|<tag>Hello</tag>| | ||
""" | ||
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Phx.New.GettextSupportTest do | ||
use ExUnit.Case, async: true | ||
|
||
doctest Phx.New.GettextSupport, import: true | ||
|
||
test "gettext_support.ex in sync with phoenix" do | ||
in_phoenix = read_split!("../lib/mix/phoenix/gettext_support.ex") | ||
in_installer = read_split!("lib/phx_new/gettext_support.ex") | ||
|
||
assert in_phoenix.first_line == "defmodule Mix.Phoenix.GettextSupport do" | ||
assert in_installer.first_line == "defmodule Phx.New.GettextSupport do" | ||
assert in_phoenix.rest == in_installer.rest | ||
assert in_phoenix.rest =~ "gettext(" | ||
end | ||
|
||
defp read_split!(path) do | ||
File.read!(path) | ||
|> String.split("\n", parts: 2) | ||
|> then(fn [first_line, rest] -> %{first_line: first_line, rest: rest} end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule Mix.Phoenix.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|<tag attr=#{maybe_gettext("Hello", :heex_attr, true)} />| | ||
~S|<tag attr={gettext("Hello")} />| | ||
iex> ~s|<tag attr=#{maybe_gettext("Hello", :heex_attr, false)} />| | ||
~S|<tag attr="Hello" />| | ||
iex> ~s|<tag>#{maybe_gettext("Hello", :eex, true)}</tag>| | ||
~S|<tag><%= gettext("Hello") %></tag>| | ||
iex> ~s|<tag>#{maybe_gettext("Hello", :eex, false)}</tag>| | ||
~S|<tag>Hello</tag>| | ||
""" | ||
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Mix.Phoenix.GettextSupportTest do | ||
use ExUnit.Case, async: true | ||
|
||
doctest Mix.Phoenix.GettextSupport, import: true | ||
|
||
test "gettext_support.ex in sync with installer" do | ||
in_phoenix = read_split!("lib/mix/phoenix/gettext_support.ex") | ||
in_installer = read_split!("installer/lib/phx_new/gettext_support.ex") | ||
|
||
assert in_phoenix.first_line == "defmodule Mix.Phoenix.GettextSupport do" | ||
assert in_installer.first_line == "defmodule Phx.New.GettextSupport do" | ||
assert in_phoenix.rest == in_installer.rest | ||
assert in_phoenix.rest =~ "gettext(" | ||
end | ||
|
||
defp read_split!(path) do | ||
File.read!(path) | ||
|> String.split("\n", parts: 2) | ||
|> then(fn [first_line, rest] -> %{first_line: first_line, rest: rest} end) | ||
end | ||
end |