From 3675d0b4f4f7c5817bff67f164e8749a2fa90827 Mon Sep 17 00:00:00 2001 From: shibao Date: Wed, 9 Feb 2022 00:13:01 -0500 Subject: [PATCH] gettext controllers --- CONTRIBUTING.md | 8 +-- lib/cannery_web/controllers/user_auth.ex | 6 +-- .../user_confirmation_controller.ex | 14 ++++-- .../user_registration_controller.ex | 12 ++--- .../user_reset_password_controller.ex | 13 +++-- .../controllers/user_session_controller.ex | 2 +- .../controllers/user_settings_controller.ex | 18 ++++--- priv/gettext/default.pot | 5 -- priv/gettext/errors.pot | 42 ++++++++++++++++ priv/gettext/prompts.pot | 50 +++++++++++++++++++ test/cannery_web/views/error_view_test.exs | 7 ++- 11 files changed, 144 insertions(+), 33 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3491e279..68eca2cc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -41,9 +41,11 @@ If you're multilingual, this project can use your translations! Visit - When adding text, please use `gettext` macros to enable things to be translated in the future. After adding `gettext` macros, run `mix format` in order to add your new text strings to the files in `priv/gettext`. - - Existing domains: `"default"` (for anything general), `"prompts"` (as a - result of the user doing an action), `"actions"` (actions that the user can - take), `"emails"`, and `"errors"` + - Existing domains: `"default"` (for anything general), `"prompts"` + (informational messages as a result of the user doing an action, i.e. in + flashes), `"actions"` (actions that the user can take), `"emails"`, and + `"errors"`. Using these domains accurately will let translators know which + messages are higher and lower priority. Thank you! - Before submitting a PR, please make sure all tests are passing using `mix test`. And as always, thank you! diff --git a/lib/cannery_web/controllers/user_auth.ex b/lib/cannery_web/controllers/user_auth.ex index ed08be41..3c362ccd 100644 --- a/lib/cannery_web/controllers/user_auth.ex +++ b/lib/cannery_web/controllers/user_auth.ex @@ -5,7 +5,7 @@ defmodule CanneryWeb.UserAuth do import Plug.Conn import Phoenix.Controller - + import CanneryWeb.Gettext alias Cannery.Accounts alias CanneryWeb.HomeLive alias CanneryWeb.Router.Helpers, as: Routes @@ -142,7 +142,7 @@ defmodule CanneryWeb.UserAuth do conn else conn - |> put_flash(:error, "You must log in to access this page.") + |> put_flash(:error, dgettext("errors", "You must log in to access this page.")) |> maybe_store_return_to() |> redirect(to: Routes.user_session_path(conn, :new)) |> halt() @@ -157,7 +157,7 @@ defmodule CanneryWeb.UserAuth do conn else conn - |> put_flash(:error, "You are not authorized to view this page.") + |> put_flash(:error, dgettext("errors", "You are not authorized to view this page.")) |> maybe_store_return_to() |> redirect(to: Routes.live_path(conn, HomeLive)) |> halt() diff --git a/lib/cannery_web/controllers/user_confirmation_controller.ex b/lib/cannery_web/controllers/user_confirmation_controller.ex index c504b567..64a3429b 100644 --- a/lib/cannery_web/controllers/user_confirmation_controller.ex +++ b/lib/cannery_web/controllers/user_confirmation_controller.ex @@ -19,8 +19,11 @@ defmodule CanneryWeb.UserConfirmationController do conn |> put_flash( :info, - "If your email is in our system and it has not been confirmed yet, " <> - "you will receive an email with instructions shortly." + dgettext( + "prompts", + "If your email is in our system and it has not been confirmed yet, " <> + "you will receive an email with instructions shortly." + ) ) |> redirect(to: "/") end @@ -31,7 +34,7 @@ defmodule CanneryWeb.UserConfirmationController do case Accounts.confirm_user(token) do {:ok, _} -> conn - |> put_flash(:info, "User confirmed successfully.") + |> put_flash(:info, dgettext("prompts", "User confirmed successfully.")) |> redirect(to: "/") :error -> @@ -45,7 +48,10 @@ defmodule CanneryWeb.UserConfirmationController do %{} -> conn - |> put_flash(:error, "User confirmation link is invalid or it has expired.") + |> put_flash( + :error, + dgettext("errors", "User confirmation link is invalid or it has expired.") + ) |> redirect(to: "/") end end diff --git a/lib/cannery_web/controllers/user_registration_controller.ex b/lib/cannery_web/controllers/user_registration_controller.ex index 57597f0c..f93f398b 100644 --- a/lib/cannery_web/controllers/user_registration_controller.ex +++ b/lib/cannery_web/controllers/user_registration_controller.ex @@ -1,6 +1,6 @@ defmodule CanneryWeb.UserRegistrationController do use CanneryWeb, :controller - + import CanneryWeb.Gettext alias Cannery.{Accounts, Invites} alias Cannery.Accounts.User alias CanneryWeb.{HomeLive, UserAuth} @@ -12,7 +12,7 @@ defmodule CanneryWeb.UserRegistrationController do conn |> render_new(invite) else conn - |> put_flash(:error, "Sorry, this invite was not found or expired") + |> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired")) |> redirect(to: Routes.live_path(CanneryWeb.Endpoint, HomeLive)) end end @@ -22,7 +22,7 @@ defmodule CanneryWeb.UserRegistrationController do conn |> render_new() else conn - |> put_flash(:error, "Sorry, public registration is disabled") + |> put_flash(:error, dgettext("errors", "Sorry, public registration is disabled")) |> redirect(to: Routes.live_path(CanneryWeb.Endpoint, HomeLive)) end end @@ -40,7 +40,7 @@ defmodule CanneryWeb.UserRegistrationController do conn |> create_user(attrs, invite) else conn - |> put_flash(:error, "Sorry, this invite was not found or expired") + |> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired")) |> redirect(to: Routes.live_path(CanneryWeb.Endpoint, HomeLive)) end end @@ -50,7 +50,7 @@ defmodule CanneryWeb.UserRegistrationController do conn |> create_user(attrs) else conn - |> put_flash(:error, "Sorry, public registration is disabled") + |> put_flash(:error, dgettext("errors", "Sorry, public registration is disabled")) |> redirect(to: Routes.live_path(CanneryWeb.Endpoint, HomeLive)) end end @@ -69,7 +69,7 @@ defmodule CanneryWeb.UserRegistrationController do ) conn - |> put_flash(:info, "User created successfully.") + |> put_flash(:info, dgettext("prompts", "User created successfully.")) |> UserAuth.log_in_user(user) {:error, %Ecto.Changeset{} = changeset} -> diff --git a/lib/cannery_web/controllers/user_reset_password_controller.ex b/lib/cannery_web/controllers/user_reset_password_controller.ex index f0f27826..3bc79a9e 100644 --- a/lib/cannery_web/controllers/user_reset_password_controller.ex +++ b/lib/cannery_web/controllers/user_reset_password_controller.ex @@ -21,7 +21,11 @@ defmodule CanneryWeb.UserResetPasswordController do conn |> put_flash( :info, - "If your email is in our system, you will receive instructions to reset your password shortly." + dgettext( + "prompts", + "If your email is in our system, you will receive instructions to " <> + "reset your password shortly." + ) ) |> redirect(to: "/") end @@ -36,7 +40,7 @@ defmodule CanneryWeb.UserResetPasswordController do case Accounts.reset_user_password(conn.assigns.user, user_params) do {:ok, _} -> conn - |> put_flash(:info, "Password reset successfully.") + |> put_flash(:info, dgettext("prompts", "Password reset successfully.")) |> redirect(to: Routes.user_session_path(conn, :new)) {:error, changeset} -> @@ -51,7 +55,10 @@ defmodule CanneryWeb.UserResetPasswordController do conn |> assign(:user, user) |> assign(:token, token) else conn - |> put_flash(:error, "Reset password link is invalid or it has expired.") + |> put_flash( + :error, + dgettext("errors", "Reset password link is invalid or it has expired.") + ) |> redirect(to: "/") |> halt() end diff --git a/lib/cannery_web/controllers/user_session_controller.ex b/lib/cannery_web/controllers/user_session_controller.ex index f18f2a79..774ec01f 100644 --- a/lib/cannery_web/controllers/user_session_controller.ex +++ b/lib/cannery_web/controllers/user_session_controller.ex @@ -20,7 +20,7 @@ defmodule CanneryWeb.UserSessionController do def delete(conn, _params) do conn - |> put_flash(:info, gettext("Logged out successfully.")) + |> put_flash(:info, dgettext("prompts", "Logged out successfully.")) |> UserAuth.log_out_user() end end diff --git a/lib/cannery_web/controllers/user_settings_controller.ex b/lib/cannery_web/controllers/user_settings_controller.ex index 23aa9abc..df6f4bab 100644 --- a/lib/cannery_web/controllers/user_settings_controller.ex +++ b/lib/cannery_web/controllers/user_settings_controller.ex @@ -25,7 +25,10 @@ defmodule CanneryWeb.UserSettingsController do conn |> put_flash( :info, - "A link to confirm your email change has been sent to the new address." + dgettext( + "prompts", + "A link to confirm your email change has been sent to the new address." + ) ) |> redirect(to: Routes.user_settings_path(conn, :edit)) @@ -41,7 +44,7 @@ defmodule CanneryWeb.UserSettingsController do case Accounts.update_user_password(user, password, user_params) do {:ok, user} -> conn - |> put_flash(:info, "Password updated successfully.") + |> put_flash(:info, dgettext("prompts", "Password updated successfully.")) |> put_session(:user_return_to, Routes.user_settings_path(conn, :edit)) |> UserAuth.log_in_user(user) @@ -54,12 +57,15 @@ defmodule CanneryWeb.UserSettingsController do case Accounts.update_user_email(conn.assigns.current_user, token) do :ok -> conn - |> put_flash(:info, "Email changed successfully.") + |> put_flash(:info, dgettext("prompts", "Email changed successfully.")) |> redirect(to: Routes.user_settings_path(conn, :edit)) :error -> conn - |> put_flash(:error, "Email change link is invalid or it has expired.") + |> put_flash( + :error, + dgettext("errors", "Email change link is invalid or it has expired.") + ) |> redirect(to: Routes.user_settings_path(conn, :edit)) end end @@ -69,11 +75,11 @@ defmodule CanneryWeb.UserSettingsController do Accounts.delete_user!(conn.assigns.current_user) conn - |> put_flash(:error, "Your account has been deleted") + |> put_flash(:error, dgettext("prompts", "Your account has been deleted")) |> redirect(to: Routes.live_path(conn, HomeLive)) else conn - |> put_flash(:error, "Unable to delete user") + |> put_flash(:error, dgettext("errors", "Unable to delete user")) |> redirect(to: Routes.user_settings_path(conn, :edit)) end end diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index f389e544..2574858a 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -109,8 +109,3 @@ msgstr "" #: lib/cannery_web/templates/user_settings/edit.html.heex:3 msgid "Settings" msgstr "" - -#, elixir-format, ex-autogen -#: lib/cannery_web/controllers/user_session_controller.ex:23 -msgid "Logged out successfully." -msgstr "" diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index da9abeaa..30b51d81 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -157,3 +157,45 @@ msgstr "" #: lib/cannery_web/controllers/user_session_controller.ex:17 msgid "Invalid email or password" msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_settings_controller.ex:67 +msgid "Email change link is invalid or it has expired." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_reset_password_controller.ex:60 +msgid "Reset password link is invalid or it has expired." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_registration_controller.ex:25 +#: lib/cannery_web/controllers/user_registration_controller.ex:53 +msgid "Sorry, public registration is disabled" +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_registration_controller.ex:15 +#: lib/cannery_web/controllers/user_registration_controller.ex:43 +msgid "Sorry, this invite was not found or expired" +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_settings_controller.ex:82 +msgid "Unable to delete user" +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_confirmation_controller.ex:53 +msgid "User confirmation link is invalid or it has expired." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_auth.ex:160 +msgid "You are not authorized to view this page." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_auth.ex:145 +msgid "You must log in to access this page." +msgstr "" diff --git a/priv/gettext/prompts.pot b/priv/gettext/prompts.pot index 6df2bdc6..3e0d3fd9 100644 --- a/priv/gettext/prompts.pot +++ b/priv/gettext/prompts.pot @@ -25,3 +25,53 @@ msgstr "" #: lib/cannery_web/templates/user_settings/edit.html.heex:104 msgid "Are you sure you want to delete your account?" msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_settings_controller.ex:28 +msgid "A link to confirm your email change has been sent to the new address." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_settings_controller.ex:60 +msgid "Email changed successfully." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_confirmation_controller.ex:22 +msgid "If your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_reset_password_controller.ex:24 +msgid "If your email is in our system, you will receive instructions to reset your password shortly." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_session_controller.ex:23 +msgid "Logged out successfully." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_reset_password_controller.ex:43 +msgid "Password reset successfully." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_settings_controller.ex:47 +msgid "Password updated successfully." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_confirmation_controller.ex:37 +msgid "User confirmed successfully." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_registration_controller.ex:72 +msgid "User created successfully." +msgstr "" + +#, elixir-format, ex-autogen +#: lib/cannery_web/controllers/user_settings_controller.ex:78 +msgid "Your account has been deleted" +msgstr "" diff --git a/test/cannery_web/views/error_view_test.exs b/test/cannery_web/views/error_view_test.exs index d28f57da..5801c8b7 100644 --- a/test/cannery_web/views/error_view_test.exs +++ b/test/cannery_web/views/error_view_test.exs @@ -1,14 +1,17 @@ defmodule CanneryWeb.ErrorViewTest do use CanneryWeb.ConnCase, async: true + import CanneryWeb.Gettext # Bring render/3 and render_to_string/3 for testing custom views import Phoenix.View test "renders 404.html" do - assert render_to_string(CanneryWeb.ErrorView, "404.html", []) == "Not Found" + assert render_to_string(CanneryWeb.ErrorView, "404.html", []) == + dgettext("errors", "Not Found") end test "renders 500.html" do - assert render_to_string(CanneryWeb.ErrorView, "500.html", []) == "Internal Server Error" + assert render_to_string(CanneryWeb.ErrorView, "500.html", []) == + dgettext("errors", "Internal Server Error") end end