1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-23 16:33:28 +00:00
mobilizon/lib/service/activity/renderer/resource.ex
Thomas Citharel a670a7d7a7
Fix and improve language handling
- Refactor plugs to detect and set language
- Translate ecto validation errors
- Use Gettext directly, not Mobilizon.Web.Gettext
- Set the language in the <html> attribute according to the one loaded
  on front-end

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2021-07-27 20:11:56 +02:00

124 lines
3.7 KiB
Elixir

defmodule Mobilizon.Service.Activity.Renderer.Resource do
@moduledoc """
Insert a comment activity
"""
alias Mobilizon.Activities.Activity
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.Activity.Renderer
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.Router.Helpers, as: Routes
import Mobilizon.Web.Gettext, only: [dgettext: 3]
@behaviour Renderer
@impl Renderer
def render(%Activity{} = activity, options) do
locale = Keyword.get(options, :locale, "en")
Gettext.put_locale(locale)
case activity.subject do
:resource_created ->
if activity.subject_params["is_folder"] do
%{
body:
dgettext("activity", "%{profile} created the folder %{resource}.", %{
profile: profile(activity),
resource: title(activity)
}),
url: resource_url(activity)
}
else
%{
body:
dgettext("activity", "%{profile} created the resource %{resource}.", %{
profile: profile(activity),
resource: title(activity)
}),
url: resource_url(activity)
}
end
:resource_renamed ->
if activity.subject_params["is_folder"] do
%{
body:
dgettext(
"activity",
"%{profile} renamed the folder from %{old_resource_title} to %{resource}.",
%{
profile: profile(activity),
resource: title(activity),
old_resource_title: activity.subject_params["old_resource_title"]
}
),
url: resource_url(activity)
}
else
%{
body:
dgettext(
"activity",
"%{profile} renamed the resource from %{old_resource_title} to %{resource}.",
%{
profile: profile(activity),
resource: title(activity),
old_resource_title: activity.subject_params["old_resource_title"]
}
),
url: resource_url(activity)
}
end
:resource_moved ->
if activity.subject_params["is_folder"] do
%{
body:
dgettext("activity", "%{profile} moved the folder %{resource}.", %{
profile: profile(activity),
resource: title(activity)
}),
url: resource_url(activity)
}
else
%{
body:
dgettext("activity", "%{profile} moved the resource %{resource}.", %{
profile: profile(activity),
resource: title(activity)
}),
url: resource_url(activity)
}
end
:resource_deleted ->
if activity.subject_params["is_folder"] do
%{
body:
dgettext("activity", "%{profile} deleted the folder %{resource}.", %{
profile: profile(activity),
resource: title(activity)
}),
url: resource_url(activity)
}
else
%{
body:
dgettext("activity", "%{profile} deleted the resource %{resource}.", %{
profile: profile(activity),
resource: title(activity)
}),
url: resource_url(activity)
}
end
end
end
defp resource_url(activity) do
Endpoint
|> Routes.page_url(:resource, activity.subject_params["resource_uuid"])
|> URI.decode()
end
defp profile(activity), do: Actor.display_name_and_username(activity.author)
defp title(activity), do: activity.subject_params["resource_title"]
end