diff --git a/installer/templates/phx_gettext/gettext.ex b/installer/templates/phx_gettext/gettext.ex index 78d902515c..edc195b275 100644 --- a/installer/templates/phx_gettext/gettext.ex +++ b/installer/templates/phx_gettext/gettext.ex @@ -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") @@ -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 diff --git a/installer/templates/phx_single/lib/app_name_web.ex b/installer/templates/phx_single/lib/app_name_web.ex index 10a1894040..b64696d3fa 100644 --- a/installer/templates/phx_single/lib/app_name_web.ex +++ b/installer/templates/phx_single/lib/app_name_web.ex @@ -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 @@ -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 diff --git a/installer/templates/phx_single/mix.exs b/installer/templates/phx_single/mix.exs index af6407569d..60fe5b2677 100644 --- a/installer/templates/phx_single/mix.exs +++ b/installer/templates/phx_single/mix.exs @@ -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 %>"} diff --git a/installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex b/installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex index a2d52e1d3d..985dee1c05 100644 --- a/installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex +++ b/installer/templates/phx_umbrella/apps/app_name_web/lib/app_name.ex @@ -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 @@ -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 diff --git a/installer/templates/phx_umbrella/apps/app_name_web/mix.exs b/installer/templates/phx_umbrella/apps/app_name_web/mix.exs index 593ea2bedd..12f347b4df 100644 --- a/installer/templates/phx_umbrella/apps/app_name_web/mix.exs +++ b/installer/templates/phx_umbrella/apps/app_name_web/mix.exs @@ -55,7 +55,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 %>"} diff --git a/installer/templates/phx_web/components/core_components.ex b/installer/templates/phx_web/components/core_components.ex index 1f382c5fbb..d69dffb048 100644 --- a/installer/templates/phx_web/components/core_components.ex +++ b/installer/templates/phx_web/components/core_components.ex @@ -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 flash notices. diff --git a/installer/test/phx_new_test.exs b/installer/test/phx_new_test.exs index 366e952843..be3cad20c3 100644 --- a/installer/test/phx_new_test.exs +++ b/installer/test/phx_new_test.exs @@ -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) @@ -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 diff --git a/installer/test/phx_new_umbrella_test.exs b/installer/test/phx_new_umbrella_test.exs index 5b8ff009a6..31f6d05040 100644 --- a/installer/test/phx_new_umbrella_test.exs +++ b/installer/test/phx_new_umbrella_test.exs @@ -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) diff --git a/integration_test/mix.exs b/integration_test/mix.exs index 8d0f1474c7..81012c0c80 100644 --- a/integration_test/mix.exs +++ b/integration_test/mix.exs @@ -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.16"}, {:bandit, "~> 1.0"}, diff --git a/mix.exs b/mix.exs index 9dfcc2f9b6..c32c49d035 100644 --- a/mix.exs +++ b/mix.exs @@ -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}, diff --git a/mix.lock b/mix.lock index 71240a24a0..c6e39398bb 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, diff --git a/priv/templates/phx.gen.live/core_components.ex b/priv/templates/phx.gen.live/core_components.ex index 1f382c5fbb..d69dffb048 100644 --- a/priv/templates/phx.gen.live/core_components.ex +++ b/priv/templates/phx.gen.live/core_components.ex @@ -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 flash notices.