summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/translation_helpers.ex
blob: e9638b00ab1a81ed444477f5bbdaa7a277a3514b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.TranslationHelpers do
  defmacro render_error(
             conn,
             status,
             msgid,
             bindings \\ Macro.escape(%{}),
             identifier \\ Macro.escape("")
           ) do
    quote do
      require Pleroma.Web.Gettext

      error_map =
        %{
          error: Pleroma.Web.Gettext.dgettext("errors", unquote(msgid), unquote(bindings)),
          identifier: unquote(identifier)
        }
        |> Enum.reject(fn {_k, v} -> v == "" end)
        |> Map.new()

      unquote(conn)
      |> Plug.Conn.put_status(unquote(status))
      |> Phoenix.Controller.json(error_map)
    end
  end
end