diff --git a/lib/mobilizon/users/user.ex b/lib/mobilizon/users/user.ex index 08bd65dc..be77498a 100644 --- a/lib/mobilizon/users/user.ex +++ b/lib/mobilizon/users/user.ex @@ -23,6 +23,7 @@ defmodule Mobilizon.Users.User do confirmation_token: String.t(), reset_password_sent_at: DateTime.t(), reset_password_token: String.t(), + locale: String.t(), default_actor: Actor.t(), actors: [Actor.t()], feed_tokens: [FeedToken.t()] @@ -37,7 +38,8 @@ defmodule Mobilizon.Users.User do :confirmation_sent_at, :confirmation_token, :reset_password_sent_at, - :reset_password_token + :reset_password_token, + :locale ] @attrs @required_attrs ++ @optional_attrs @@ -59,6 +61,7 @@ defmodule Mobilizon.Users.User do field(:confirmation_token, :string) field(:reset_password_sent_at, :utc_datetime) field(:reset_password_token, :string) + field(:locale, :string, default: "en") belongs_to(:default_actor, Actor) has_many(:actors, Actor) diff --git a/lib/mobilizon/users/users.ex b/lib/mobilizon/users/users.ex index 2fe78498..802dc42b 100644 --- a/lib/mobilizon/users/users.ex +++ b/lib/mobilizon/users/users.ex @@ -24,7 +24,7 @@ defmodule Mobilizon.Users do Registers an user. """ @spec register(map) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()} - def register(%{email: _email, password: _password} = args) do + def register(args) do with {:ok, %User{} = user} <- %User{} |> User.registration_changeset(args) diff --git a/lib/mobilizon_web/email/admin.ex b/lib/mobilizon_web/email/admin.ex index 60351130..0f78db66 100644 --- a/lib/mobilizon_web/email/admin.ex +++ b/lib/mobilizon_web/email/admin.ex @@ -17,7 +17,7 @@ defmodule MobilizonWeb.Email.Admin do @spec report(User.t(), Report.t(), String.t()) :: Bamboo.Email.t() def report(%User{email: email}, %Report{} = report, locale \\ "en") do - Gettext.put_locale(locale) + MobilizonWeb.Gettext.put_locale(locale) subject = gettext( diff --git a/lib/mobilizon_web/email/event.ex b/lib/mobilizon_web/email/event.ex index c1e55077..a568521a 100644 --- a/lib/mobilizon_web/email/event.ex +++ b/lib/mobilizon_web/email/event.ex @@ -24,7 +24,7 @@ defmodule MobilizonWeb.Email.Event do changes, locale \\ "en" ) do - Gettext.put_locale(locale) + MobilizonWeb.Gettext.put_locale(locale) subject = gettext( diff --git a/lib/mobilizon_web/email/participation.ex b/lib/mobilizon_web/email/participation.ex index 6029349f..4bf40066 100644 --- a/lib/mobilizon_web/email/participation.ex +++ b/lib/mobilizon_web/email/participation.ex @@ -46,7 +46,7 @@ defmodule MobilizonWeb.Email.Participation do %Participant{event: event, role: :rejected}, locale ) do - Gettext.put_locale(locale) + MobilizonWeb.Gettext.put_locale(locale) subject = gettext( @@ -67,7 +67,7 @@ defmodule MobilizonWeb.Email.Participation do %Participant{event: event, role: :participant}, locale ) do - Gettext.put_locale(locale) + MobilizonWeb.Gettext.put_locale(locale) subject = gettext( diff --git a/lib/mobilizon_web/email/user.ex b/lib/mobilizon_web/email/user.ex index 6476e698..b331e895 100644 --- a/lib/mobilizon_web/email/user.ex +++ b/lib/mobilizon_web/email/user.ex @@ -19,7 +19,7 @@ defmodule MobilizonWeb.Email.User do %User{email: email, confirmation_token: confirmation_token}, locale \\ "en" ) do - Gettext.put_locale(locale) + MobilizonWeb.Gettext.put_locale(locale) subject = gettext( @@ -39,7 +39,7 @@ defmodule MobilizonWeb.Email.User do %User{email: email, reset_password_token: reset_password_token}, locale \\ "en" ) do - Gettext.put_locale(locale) + MobilizonWeb.Gettext.put_locale(locale) subject = gettext( diff --git a/lib/mobilizon_web/gettext.ex b/lib/mobilizon_web/gettext.ex index 25a7544e..e6880ae8 100644 --- a/lib/mobilizon_web/gettext.ex +++ b/lib/mobilizon_web/gettext.ex @@ -21,4 +21,27 @@ defmodule MobilizonWeb.Gettext do See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage. """ use Gettext, otp_app: :mobilizon + + def put_locale(locale) do + locale = determine_best_locale(locale) + Gettext.put_locale(MobilizonWeb.Gettext, locale) + end + + @spec determine_best_locale(String.t()) :: String.t() + def determine_best_locale(locale) do + locale = String.trim(locale) + locales = Gettext.known_locales(MobilizonWeb.Gettext) + + cond do + # Either it matches directly, eg: "en" => "en", "fr" => "fr", "fr_FR" => "fr_FR" + locale in locales -> locale + # Either the first part matches, "fr_CA" => "fr" + split_locale(locale) in locales -> split_locale(locale) + # Otherwise default to english + true -> "en" + end + end + + # Keep only the first part of the locale + defp split_locale(locale), do: locale |> String.split("_", trim: true, parts: 2) |> hd end diff --git a/lib/mobilizon_web/resolvers/user.ex b/lib/mobilizon_web/resolvers/user.ex index 6d765e5e..613b7dde 100644 --- a/lib/mobilizon_web/resolvers/user.ex +++ b/lib/mobilizon_web/resolvers/user.ex @@ -116,7 +116,7 @@ defmodule MobilizonWeb.Resolvers.User do with {:registrations_open, true} <- {:registrations_open, Config.instance_registrations_open?()}, {:ok, %User{} = user} <- Users.register(args) do - Activation.send_confirmation_email(user) + Activation.send_confirmation_email(user, Map.get(args, :locale, "en")) {:ok, user} else {:registrations_open, false} -> @@ -154,10 +154,11 @@ defmodule MobilizonWeb.Resolvers.User do Send the confirmation email again. We only do this to accounts unconfirmed """ - def resend_confirmation_email(_parent, %{email: email, locale: locale}, _resolution) do - with {:ok, user} <- Users.get_user_by_email(email, false), + def resend_confirmation_email(_parent, args, _resolution) do + with {:ok, %User{locale: locale} = user} <- + Users.get_user_by_email(Map.get(args, :email), false), {:ok, email} <- - Activation.resend_confirmation_email(user, locale) do + Activation.resend_confirmation_email(user, Map.get(args, :locale, locale)) do {:ok, email} else {:error, :user_not_found} -> @@ -171,10 +172,11 @@ defmodule MobilizonWeb.Resolvers.User do @doc """ Send an email to reset the password from an user """ - def send_reset_password(_parent, %{email: email, locale: locale}, _resolution) do - with {:ok, user} <- Users.get_user_by_email(email, true), + def send_reset_password(_parent, args, _resolution) do + with email <- Map.get(args, :email), + {:ok, %User{locale: locale} = user} <- Users.get_user_by_email(email, true), {:ok, %Bamboo.Email{} = _email_html} <- - ResetPassword.send_password_reset_email(user, locale) do + ResetPassword.send_password_reset_email(user, Map.get(args, :locale, locale)) do {:ok, email} else {:error, :user_not_found} -> diff --git a/lib/mobilizon_web/schema/user.ex b/lib/mobilizon_web/schema/user.ex index d02aa645..fc3021d3 100644 --- a/lib/mobilizon_web/schema/user.ex +++ b/lib/mobilizon_web/schema/user.ex @@ -46,6 +46,8 @@ defmodule MobilizonWeb.Schema.UserType do field(:role, :user_role, description: "The role for the user") + field(:locale, :string, description: "The user's locale") + field(:participations, list_of(:participant), description: "The list of events this user goes to" ) do @@ -109,6 +111,7 @@ defmodule MobilizonWeb.Schema.UserType do field :create_user, type: :user do arg(:email, non_null(:string)) arg(:password, non_null(:string)) + arg(:locale, :string) resolve(handle_errors(&User.create_user/3)) end @@ -122,14 +125,14 @@ defmodule MobilizonWeb.Schema.UserType do @desc "Resend registration confirmation token" field :resend_confirmation_email, type: :string do arg(:email, non_null(:string)) - arg(:locale, :string, default_value: "en") + arg(:locale, :string) resolve(&User.resend_confirmation_email/3) end @desc "Send a link through email to reset user password" field :send_reset_password, type: :string do arg(:email, non_null(:string)) - arg(:locale, :string, default_value: "en") + arg(:locale, :string) resolve(&User.send_reset_password/3) end diff --git a/lib/mobilizon_web/templates/email/event_updated.html.eex b/lib/mobilizon_web/templates/email/event_updated.html.eex index 553dfe52..eb2ff8fd 100644 --- a/lib/mobilizon_web/templates/email/event_updated.html.eex +++ b/lib/mobilizon_web/templates/email/event_updated.html.eex @@ -58,7 +58,7 @@ <%= gettext "Start of event" %>
- <%= gettext "You requested a new password for your account on %{server}.", server: @instance[:name] %> + <%= gettext "You requested a new password for your account on %{instance}.", instance: @instance[:name] %>
<%= gettext "Resetting your password is easy. Just press the button below and follow the instructions. We'll have you up and running in no time." %>
diff --git a/lib/mobilizon_web/templates/email/password_reset.text.eex b/lib/mobilizon_web/templates/email/password_reset.text.eex
index 51268a12..58a4aacd 100644
--- a/lib/mobilizon_web/templates/email/password_reset.text.eex
+++ b/lib/mobilizon_web/templates/email/password_reset.text.eex
@@ -2,7 +2,7 @@
==
-<%= gettext "You requested a new password for your account on %{host}.", host: @instance[:name] %>
+<%= gettext "You requested a new password for your account on %{instance}.", instance: @instance[:name] %>
<%= gettext "Resetting your password is easy. Just click the link below and follow the instructions. We'll have you up and running in no time." %>
diff --git a/lib/mobilizon_web/views/email_view.ex b/lib/mobilizon_web/views/email_view.ex
index 2c911cb4..a65d3ce5 100644
--- a/lib/mobilizon_web/views/email_view.ex
+++ b/lib/mobilizon_web/views/email_view.ex
@@ -1,6 +1,8 @@
defmodule MobilizonWeb.EmailView do
use MobilizonWeb, :view
+ import MobilizonWeb.Gettext
+
def datetime_to_string(%DateTime{} = datetime, locale \\ "en") do
with {:ok, string} <-
Cldr.DateTime.to_string(datetime, Mobilizon.Cldr, format: :medium, locale: locale) do
diff --git a/lib/service/events/tools.ex b/lib/service/events/tools.ex
index dc8efef8..902e6ce5 100644
--- a/lib/service/events/tools.ex
+++ b/lib/service/events/tools.ex
@@ -38,13 +38,13 @@ defmodule Mobilizon.Service.Events.Tool do
end
defp send_notification_for_event_update_to_participant(
- {%Actor{} = actor, %User{} = user},
+ {%Actor{} = actor, %User{locale: locale} = user},
%Event{} = old_event,
%Event{} = event,
diff
) do
user
- |> Email.Event.event_updated(actor, old_event, event, diff)
+ |> Email.Event.event_updated(actor, old_event, event, diff, locale)
|> Email.Mailer.deliver_later()
end
end
diff --git a/priv/gettext/cs/LC_MESSAGES/default.po b/priv/gettext/cs/LC_MESSAGES/default.po
index ad411540..d8c6e77f 100644
--- a/priv/gettext/cs/LC_MESSAGES/default.po
+++ b/priv/gettext/cs/LC_MESSAGES/default.po
@@ -122,11 +122,6 @@ msgstr ""
msgid "You created an account on %{host} with this email address. You are one click away from activating it."
msgstr ""
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
-msgid "You requested a new password for your account on %{server}."
-msgstr ""
-
#, elixir-format
#: lib/mobilizon_web/email/user.ex:25
msgid "Instructions to confirm your Mobilizon account on %{instance}"
@@ -217,11 +212,6 @@ msgstr ""
msgid "You created an account on %{host} with this email address. You are one click away from activating it. If this wasn't you, please ignore this email."
msgstr ""
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
-msgid "You requested a new password for your account on %{host}."
-msgstr ""
-
#, elixir-format
#: lib/mobilizon_web/templates/email/event_participation_approved.html.eex:38
msgid "You requested to participate in event %{title}"
@@ -299,3 +289,9 @@ msgstr ""
#: lib/mobilizon_web/templates/email/event_updated.text.eex:19
msgid "View the updated event on: %{link}"
msgstr ""
+
+#, elixir-format, fuzzy
+#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
+#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
+msgid "You requested a new password for your account on %{instance}."
+msgstr ""
diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po
index e0e7f028..6a6081c2 100644
--- a/priv/gettext/de/LC_MESSAGES/default.po
+++ b/priv/gettext/de/LC_MESSAGES/default.po
@@ -122,11 +122,6 @@ msgstr ""
msgid "You created an account on %{host} with this email address. You are one click away from activating it."
msgstr ""
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
-msgid "You requested a new password for your account on %{server}."
-msgstr ""
-
#, elixir-format
#: lib/mobilizon_web/email/user.ex:25
msgid "Instructions to confirm your Mobilizon account on %{instance}"
@@ -217,11 +212,6 @@ msgstr ""
msgid "You created an account on %{host} with this email address. You are one click away from activating it. If this wasn't you, please ignore this email."
msgstr ""
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
-msgid "You requested a new password for your account on %{host}."
-msgstr ""
-
#, elixir-format
#: lib/mobilizon_web/templates/email/event_participation_approved.html.eex:38
msgid "You requested to participate in event %{title}"
@@ -299,3 +289,9 @@ msgstr ""
#: lib/mobilizon_web/templates/email/event_updated.text.eex:19
msgid "View the updated event on: %{link}"
msgstr ""
+
+#, elixir-format, fuzzy
+#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
+#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
+msgid "You requested a new password for your account on %{instance}."
+msgstr ""
diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot
index f4b50c1e..8b6c7d1a 100644
--- a/priv/gettext/default.pot
+++ b/priv/gettext/default.pot
@@ -108,11 +108,6 @@ msgstr ""
msgid "You created an account on %{host} with this email address. You are one click away from activating it."
msgstr ""
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
-msgid "You requested a new password for your account on %{server}."
-msgstr ""
-
#, elixir-format
#: lib/mobilizon_web/email/user.ex:25
msgid "Instructions to confirm your Mobilizon account on %{instance}"
@@ -203,11 +198,6 @@ msgstr ""
msgid "You created an account on %{host} with this email address. You are one click away from activating it. If this wasn't you, please ignore this email."
msgstr ""
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
-msgid "You requested a new password for your account on %{host}."
-msgstr ""
-
#, elixir-format
#: lib/mobilizon_web/templates/email/event_participation_approved.html.eex:38
msgid "You requested to participate in event %{title}"
@@ -285,3 +275,9 @@ msgstr ""
#: lib/mobilizon_web/templates/email/event_updated.text.eex:19
msgid "View the updated event on: %{link}"
msgstr ""
+
+#, elixir-format
+#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
+#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
+msgid "You requested a new password for your account on %{instance}."
+msgstr ""
diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po
index 506cdaae..8de59e58 100644
--- a/priv/gettext/en/LC_MESSAGES/default.po
+++ b/priv/gettext/en/LC_MESSAGES/default.po
@@ -134,11 +134,6 @@ msgstr ""
"You created an account on %{host} with this email address. You are one click "
"away from activating it."
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
-msgid "You requested a new password for your account on %{server}."
-msgstr "You requested a new password for your account on %{server}."
-
#, elixir-format
#: lib/mobilizon_web/email/user.ex:25
msgid "Instructions to confirm your Mobilizon account on %{instance}"
@@ -239,11 +234,6 @@ msgstr ""
"You created an account on %{host} with this email address. You are one click "
"away from activating it."
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
-msgid "You requested a new password for your account on %{host}."
-msgstr "You requested a new password for your account on %{server}."
-
#, elixir-format
#: lib/mobilizon_web/templates/email/event_participation_approved.html.eex:38
msgid "You requested to participate in event %{title}"
@@ -321,3 +311,9 @@ msgstr ""
#: lib/mobilizon_web/templates/email/event_updated.text.eex:19
msgid "View the updated event on: %{link}"
msgstr "Your participation to event %{title} has been rejected"
+
+#, elixir-format, fuzzy
+#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
+#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
+msgid "You requested a new password for your account on %{instance}."
+msgstr "You requested a new password for your account on %{instance}."
diff --git a/priv/gettext/es/LC_MESSAGES/default.po b/priv/gettext/es/LC_MESSAGES/default.po
index f42f9d3c..a2be30f6 100644
--- a/priv/gettext/es/LC_MESSAGES/default.po
+++ b/priv/gettext/es/LC_MESSAGES/default.po
@@ -122,11 +122,6 @@ msgstr ""
msgid "You created an account on %{host} with this email address. You are one click away from activating it."
msgstr ""
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
-msgid "You requested a new password for your account on %{server}."
-msgstr ""
-
#, elixir-format
#: lib/mobilizon_web/email/user.ex:25
msgid "Instructions to confirm your Mobilizon account on %{instance}"
@@ -217,11 +212,6 @@ msgstr ""
msgid "You created an account on %{host} with this email address. You are one click away from activating it. If this wasn't you, please ignore this email."
msgstr ""
-#, elixir-format
-#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
-msgid "You requested a new password for your account on %{host}."
-msgstr ""
-
#, elixir-format
#: lib/mobilizon_web/templates/email/event_participation_approved.html.eex:38
msgid "You requested to participate in event %{title}"
@@ -299,3 +289,9 @@ msgstr ""
#: lib/mobilizon_web/templates/email/event_updated.text.eex:19
msgid "View the updated event on: %{link}"
msgstr ""
+
+#, elixir-format, fuzzy
+#: lib/mobilizon_web/templates/email/password_reset.html.eex:38
+#: lib/mobilizon_web/templates/email/password_reset.text.eex:5
+msgid "You requested a new password for your account on %{instance}."
+msgstr ""
diff --git a/priv/gettext/fr_FR/LC_MESSAGES/default.po b/priv/gettext/fr/LC_MESSAGES/default.po
similarity index 97%
rename from priv/gettext/fr_FR/LC_MESSAGES/default.po
rename to priv/gettext/fr/LC_MESSAGES/default.po
index f927185c..d1ad8a06 100644
--- a/priv/gettext/fr_FR/LC_MESSAGES/default.po
+++ b/priv/gettext/fr/LC_MESSAGES/default.po
@@ -13,7 +13,7 @@ msgstr ""
"PO-Revision-Date: 2019-09-30 17:10+0000\n"
"Last-Translator: Thomas Citharel