Skip to content

Commit

Permalink
Update generators with new Gettext API (#5902)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide authored and SteffenDE committed Dec 9, 2024
1 parent 6858b4f commit a7c93ec
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 31 deletions.
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
17 changes: 10 additions & 7 deletions installer/templates/phx_single/lib/app_name_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ defmodule <%= @web_namespace %> do
quote do
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: <%= @web_namespace %>.Layouts]
layouts: [html: <%= @web_namespace %>.Layouts]<%= if @gettext do %>

import Plug.Conn<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
use Gettext, backend: <%= @web_namespace %>.Gettext<% end %>

import Plug.Conn

unquote(verified_routes())
end
Expand Down Expand Up @@ -80,12 +81,14 @@ defmodule <%= @web_namespace %> do
end

defp html_helpers do
quote do
quote do<%= if @gettext do %>
# Translation
use Gettext, backend: <%= @web_namespace %>.Gettext
<% end %>
# 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 @@ -58,7 +58,7 @@ defmodule <%= @app_module %>.MixProject do
{:finch, "~> 0.13"},<% 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
17 changes: 10 additions & 7 deletions installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ defmodule <%= @web_namespace %> do
quote do
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: <%= @web_namespace %>.Layouts]
layouts: [html: <%= @web_namespace %>.Layouts]<%= if @gettext do %>

import Plug.Conn<%= if @gettext do %>
import <%= @web_namespace %>.Gettext<% end %>
use Gettext, backend: <%= @web_namespace %>.Gettext<% end %>

import Plug.Conn

unquote(verified_routes())
end
Expand Down Expand Up @@ -80,12 +81,14 @@ defmodule <%= @web_namespace %> do
end

defp html_helpers do
quote do
quote do<%= if @gettext do %>
# Translation
use Gettext, backend: <%= @web_namespace %>.Gettext
<% end %>
# 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_umbrella/apps/app_name_web/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule <%= @web_namespace %>.MixProject do
depth: 1},<% end %>
{:telemetry_metrics, "~> 1.0"},
{:telemetry_poller, "~> 1.0"},<%= if @gettext do %>
{:gettext, "~> 0.20"},<% end %><%= if @app_name != @web_app_name do %>
{:gettext, "~> 0.26"},<% end %><%= if @app_name != @web_app_name do %>
{:<%= @app_name %>, in_umbrella: true},<% end %>
{:jason, "~> 1.2"},
{<%= inspect @web_adapter_app %>, "<%= @web_adapter_vsn %>"}
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 %>

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

@doc """
Renders a modal.
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 @@ -300,7 +300,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 @@ -373,7 +377,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 @@ -319,7 +319,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
2 changes: 1 addition & 1 deletion integration_test/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule Phoenix.Integration.MixProject do
{:phoenix_live_dashboard, "~> 0.8.3"},
{:telemetry_metrics, "~> 1.0"},
{:telemetry_poller, "~> 1.0"},
{:gettext, "~> 0.20"},
{:gettext, "~> 0.26"},
{:jason, "~> 1.2"},
{:swoosh, "~> 1.3"},
{:bandit, "~> 1.0"},
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule Phoenix.MixProject do
{:ex_doc, "~> 0.24", only: :docs},
{:ecto, "~> 3.0", only: :docs},
{:ecto_sql, "~> 3.10", only: :docs},
{:gettext, "~> 0.20", only: :docs},
{:gettext, "~> 0.26", only: :docs},
{:telemetry_poller, "~> 1.0", only: :docs},
{:telemetry_metrics, "~> 1.0", only: :docs},
{:makeup_eex, ">= 0.1.1", only: :docs},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ecto_sql": {:hex, :ecto_sql, "3.10.1", "6ea6b3036a0b0ca94c2a02613fd9f742614b5cfe494c41af2e6571bb034dd94c", [:mix], [{:db_connection, "~> 2.5 or ~> 2.4.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f6a25bdbbd695f12c8171eaff0851fa4c8e72eec1e98c7364402dda9ce11c56b"},
"esbuild": {:hex, :esbuild, "0.8.1", "0cbf919f0eccb136d2eeef0df49c4acf55336de864e63594adcea3814f3edf41", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "25fc876a67c13cb0a776e7b5d7974851556baeda2085296c14ab48555ea7560f"},
"ex_doc": {:hex, :ex_doc, "0.34.0", "ab95e0775db3df71d30cf8d78728dd9261c355c81382bcd4cefdc74610bef13e", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "60734fb4c1353f270c3286df4a0d51e65a2c1d9fba66af3940847cc65a8066d7"},
"expo": {:hex, :expo, "1.0.0", "647639267e088717232f4d4451526e7a9de31a3402af7fcbda09b27e9a10395a", [:mix], [], "hexpm", "18d2093d344d97678e8a331ca0391e85d29816f9664a25653fd7e6166827827c"},
"gettext": {:hex, :gettext, "0.20.0", "75ad71de05f2ef56991dbae224d35c68b098dd0e26918def5bb45591d5c8d429", [:mix], [], "hexpm", "1c03b177435e93a47441d7f681a7040bd2a816ece9e2666d1c9001035121eb3d"},
"hpax": {:hex, :hpax, "0.1.1", "2396c313683ada39e98c20a75a82911592b47e5c24391363343bde74f82396ca", [:mix], [], "hexpm", "0ae7d5a0b04a8a60caf7a39fcf3ec476f35cc2cc16c05abea730d3ce6ac6c826"},
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
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 %>

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

@doc """
Renders a modal.
Expand Down

0 comments on commit a7c93ec

Please sign in to comment.