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

Update generators with new Gettext API #5902

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions installer/templates/phx_gettext/gettext.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ defmodule <%= @web_namespace %>.Gettext do
@moduledoc """
A module providing Internationalization with a gettext-based API.

By using [Gettext](https://hexdocs.pm/gettext),
your module gains a set of macros for translations, for example:
By using [Gettext](https://hexdocs.pm/gettext), your module compiles translations
that you can use in your application. To use this Gettext backend module,
call `use Gettext` and pass it as an option:

import <%= @web_namespace %>.Gettext
use Gettext, backend: <%= @web_namespace %>.Gettext

# Simple translation
gettext("Here is the string to translate")
Expand All @@ -20,5 +21,5 @@ defmodule <%= @web_namespace %>.Gettext do

See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
"""
use Gettext, otp_app: :<%= @web_app_name %>
use Gettext.Backend, otp_app: :<%= @web_app_name %>
end
13 changes: 8 additions & 5 deletions installer/templates/phx_single/lib/app_name_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ defmodule <%= @web_namespace %> do
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: <%= @web_namespace %>.Layouts]
<%= if @gettext do %>
Copy link
Member

Choose a reason for hiding this comment

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

The new lines are tricky to get right, but I am almost sure this is what is needed. You have to replicate this below.

Suggested change
layouts: [html: <%= @web_namespace %>.Layouts]
<%= if @gettext do %>
layouts: [html: <%= @web_namespace %>.Layouts]<%= if @gettext do %>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With --gettext:

    quote do
      # Translation
      use Gettext, backend: GtestWeb.Gettext
      
      # HTML escaping functionality
      import Phoenix.HTML
      # Core UI components
      import GtestWeb.CoreComponents

      # Shortcut for generating JS commands
      alias Phoenix.LiveView.JS

      # Routes generation with the ~p sigil
      unquote(verified_routes())
    end

With --no-gettext:

    quote do
      # HTML escaping functionality
      import Phoenix.HTML
      # Core UI components
      import GtestWeb.CoreComponents

      # Shortcut for generating JS commands
      alias Phoenix.LiveView.JS

      # Routes generation with the ~p sigil
      unquote(verified_routes())
    end

I think we're good? I would love to keep the use above import as it seems like it's a pretty common ordering. 🙃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(this is with changes I’m about to push to be clear!)

Copy link
Member

Choose a reason for hiding this comment

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

This will leave trailing whitespace. I think the <%= end %> needs to be at the end of the previous line.

use Gettext, backend: <%= @web_namespace %>.Gettext<% end %>

import Plug.Conn<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
import Plug.Conn

unquote(verified_routes())
end
Expand Down Expand Up @@ -81,11 +82,13 @@ defmodule <%= @web_namespace %> do

defp html_helpers do
quote do
# Translation
josevalim marked this conversation as resolved.
Show resolved Hide resolved
<%= if @gettext do %>use Gettext, backend: <%= @web_namespace %>.Gettext
<% end %>
Copy link
Member

Choose a reason for hiding this comment

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

This will leave trailing whitespace. I think the <%= end %> needs to be at the end of the previous line.

# HTML escaping functionality
import Phoenix.HTML
# Core UI components and translation
import <%= @web_namespace %>.CoreComponents<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
# Core UI components
import <%= @web_namespace %>.CoreComponents

# Shortcut for generating JS commands
alias Phoenix.LiveView.JS
Expand Down
2 changes: 1 addition & 1 deletion installer/templates/phx_single/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ defmodule <%= @app_module %>.MixProject do
{:req, "~> 0.5.4"},<% end %>
{:telemetry_metrics, "~> 1.0"},
{:telemetry_poller, "~> 1.0"},<%= if @gettext do %>
{:gettext, "~> 0.20"},<% end %>
{:gettext, "~> 0.26"},<% end %>
{:jason, "~> 1.2"},
{:dns_cluster, "~> 0.1.1"},
{<%= inspect @web_adapter_app %>, "<%= @web_adapter_vsn %>"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ defmodule <%= @web_namespace %> do
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: <%= @web_namespace %>.Layouts]

import Plug.Conn<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>

<%= if @gettext do %>use Gettext, backend: <%= @web_namespace %>.Gettext
<% end %>
josevalim marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

This will leave trailing whitespace before and after. I think the if and <%= end %> needs to be at the end of the previous line.

import Plug.Conn

unquote(verified_routes())
end
Expand Down Expand Up @@ -81,11 +82,13 @@ defmodule <%= @web_namespace %> do

defp html_helpers do
quote do
# Translation
<%= if @gettext do %>use Gettext, backend: <%= @web_namespace %>.Gettext
<% end %>
josevalim marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

This will leave trailing whitespace. I think the <%= end %> needs to be at the end of the previous line.

# HTML escaping functionality
import Phoenix.HTML
# Core UI components and translation
import <%= @web_namespace %>.CoreComponents<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
# Core UI components
import <%= @web_namespace %>.CoreComponents

# Shortcut for generating JS commands
alias Phoenix.LiveView.JS
Expand Down
6 changes: 3 additions & 3 deletions installer/templates/phx_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ defmodule <%= @web_namespace %>.CoreComponents do

Icons are provided by [heroicons](https://heroicons.com). See `icon/1` for usage.
"""
use Phoenix.Component
use Phoenix.Component<%= if @gettext do %>
use Gettext, backend: <%= @web_namespace %>.Gettext<% end %>
Copy link
Member

Choose a reason for hiding this comment

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

This is perfect!


alias Phoenix.LiveView.JS<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
alias Phoenix.LiveView.JS

@doc """
Renders flash notices.
Expand Down
13 changes: 11 additions & 2 deletions installer/test/phx_new_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ defmodule Mix.Tasks.Phx.NewTest do
assert_received {:mix_shell, :info, ["Start your Phoenix app" <> _]}

# Gettext
assert_file("phx_blog/lib/phx_blog_web/gettext.ex", ~r"defmodule PhxBlogWeb.Gettext")
assert_file("phx_blog/lib/phx_blog_web/gettext.ex", [
~r"defmodule PhxBlogWeb.Gettext",
~r"use Gettext\.Backend, otp_app: :phx_blog"
])

assert File.exists?("phx_blog/priv/gettext/errors.pot")
assert File.exists?("phx_blog/priv/gettext/en/LC_MESSAGES/errors.po")
end)
Expand Down Expand Up @@ -369,7 +373,12 @@ defmodule Mix.Tasks.Phx.NewTest do
refute_file("phx_blog/priv/gettext/en/LC_MESSAGES/errors.po")
refute_file("phx_blog/priv/gettext/errors.pot")
assert_file("phx_blog/mix.exs", &refute(&1 =~ ~r":gettext"))
assert_file("phx_blog/lib/phx_blog_web.ex", &refute(&1 =~ ~r"import AmsMockWeb.Gettext"))

assert_file(
"phx_blog/lib/phx_blog_web.ex",
&refute(&1 =~ ~r"use Gettext, backend: AmsMockWeb.Gettext")
)

assert_file("phx_blog/config/dev.exs", &refute(&1 =~ ~r"gettext"))

# No HTML
Expand Down
6 changes: 5 additions & 1 deletion installer/test/phx_new_umbrella_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
assert_received {:mix_shell, :info, ["Start your Phoenix app" <> _]}

# Gettext
assert_file(web_path(@app, "lib/#{@app}_web/gettext.ex"), ~r"defmodule PhxUmbWeb.Gettext")
assert_file(web_path(@app, "lib/#{@app}_web/gettext.ex"), [
~r"defmodule PhxUmbWeb.Gettext",
~r"use Gettext\.Backend, otp_app: :phx_umb_web"
])

assert File.exists?(web_path(@app, "priv/gettext/errors.pot"))
assert File.exists?(web_path(@app, "priv/gettext/en/LC_MESSAGES/errors.po"))
end)
Expand Down
6 changes: 3 additions & 3 deletions priv/templates/phx.gen.live/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ defmodule <%= @web_namespace %>.CoreComponents do

Icons are provided by [heroicons](https://heroicons.com). See `icon/1` for usage.
"""
use Phoenix.Component
use Phoenix.Component<%= if @gettext do %>
use Gettext, backend: <%= @web_namespace %>.Gettext<% end %>
Copy link
Member

Choose a reason for hiding this comment

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

This is perfect!


alias Phoenix.LiveView.JS<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
alias Phoenix.LiveView.JS

@doc """
Renders flash notices.
Expand Down
Loading