gettext controllers

This commit is contained in:
shibao 2022-02-09 00:13:01 -05:00
parent 9972299fc4
commit 3675d0b4f4
11 changed files with 144 additions and 33 deletions

View File

@ -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!

View File

@ -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()

View File

@ -19,9 +19,12 @@ defmodule CanneryWeb.UserConfirmationController do
conn
|> put_flash(
:info,
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

View File

@ -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} ->

View File

@ -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

View File

@ -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

View File

@ -25,8 +25,11 @@ defmodule CanneryWeb.UserSettingsController do
conn
|> put_flash(
:info,
dgettext(
"prompts",
"A link to confirm your email change has been sent to the new address."
)
)
|> redirect(to: Routes.user_settings_path(conn, :edit))
{:error, changeset} ->
@ -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

View File

@ -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 ""

View File

@ -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 ""

View File

@ -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 ""

View File

@ -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