Save user locale and use it to translate things
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -24,7 +24,7 @@ defmodule MobilizonWeb.Email.Event do
|
||||
changes,
|
||||
locale \\ "en"
|
||||
) do
|
||||
Gettext.put_locale(locale)
|
||||
MobilizonWeb.Gettext.put_locale(locale)
|
||||
|
||||
subject =
|
||||
gettext(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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} ->
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
<%= gettext "Start of event" %>
|
||||
</td>
|
||||
<td bgcolor="#ffffff" align="left">
|
||||
<%= datetime_to_string(@event.begins_on) %>
|
||||
<%= datetime_to_string(@event.begins_on, @locale) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
@@ -68,7 +68,7 @@
|
||||
<%= gettext "Ending of event" %>
|
||||
</td>
|
||||
<td bgcolor="#ffffff" align="left">
|
||||
<%= datetime_to_string(@event.ends_on) %>
|
||||
<%= datetime_to_string(@event.ends_on, @locale) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -9,11 +9,11 @@
|
||||
<% end %>
|
||||
|
||||
<%= if MapSet.member?(@changes, :begins_on) do %>
|
||||
<%= gettext "New date and time for start of event: %{begins_on}", begins_on: datetime_to_string(@event.begins_on) %>
|
||||
<%= gettext "New date and time for start of event: %{begins_on}", begins_on: datetime_to_string(@event.begins_on, @locale) %>
|
||||
<% end %>
|
||||
|
||||
<%= if MapSet.member?(@changes, :ends_on) do %>
|
||||
<%= gettext "New date and time for ending of event: %{ends_on}", ends_on: datetime_to_string(@event.ends_on) %>
|
||||
<%= gettext "New date and time for ending of event: %{ends_on}", ends_on: datetime_to_string(@event.ends_on, @locale) %>
|
||||
<% end %>
|
||||
|
||||
<%= gettext "View the updated event on: %{link}", link: page_url(MobilizonWeb.Endpoint, :event, @event.id) %>
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
<tr>
|
||||
<td bgcolor="#ffffff" align="left" style="padding: 20px 30px 0px 30px; color: #666666; font-family: 'Lato', Helvetica, Arial, sans-serif; font-size: 18px; font-weight: 400; line-height: 25px;" >
|
||||
<p style="margin: 0;">
|
||||
<%= 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] %>
|
||||
</p>
|
||||
<p style="margin: 0">
|
||||
<%= 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." %>
|
||||
|
||||
@@ -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." %>
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user