logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

set_locale_plug.ex (1690B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. # NOTE: this module is based on https://github.com/smeevil/set_locale
  5. defmodule Pleroma.Plugs.SetLocalePlug do
  6. import Plug.Conn, only: [get_req_header: 2, assign: 3]
  7. def init(_), do: nil
  8. def call(conn, _) do
  9. locale = get_locale_from_header(conn) || Gettext.get_locale()
  10. Gettext.put_locale(locale)
  11. assign(conn, :locale, locale)
  12. end
  13. defp get_locale_from_header(conn) do
  14. conn
  15. |> extract_accept_language()
  16. |> Enum.find(&supported_locale?/1)
  17. end
  18. defp extract_accept_language(conn) do
  19. case get_req_header(conn, "accept-language") do
  20. [value | _] ->
  21. value
  22. |> String.split(",")
  23. |> Enum.map(&parse_language_option/1)
  24. |> Enum.sort(&(&1.quality > &2.quality))
  25. |> Enum.map(& &1.tag)
  26. |> Enum.reject(&is_nil/1)
  27. |> ensure_language_fallbacks()
  28. _ ->
  29. []
  30. end
  31. end
  32. defp supported_locale?(locale) do
  33. Pleroma.Web.Gettext
  34. |> Gettext.known_locales()
  35. |> Enum.member?(locale)
  36. end
  37. defp parse_language_option(string) do
  38. captures = Regex.named_captures(~r/^\s?(?<tag>[\w\-]+)(?:;q=(?<quality>[\d\.]+))?$/i, string)
  39. quality =
  40. case Float.parse(captures["quality"] || "1.0") do
  41. {val, _} -> val
  42. :error -> 1.0
  43. end
  44. %{tag: captures["tag"], quality: quality}
  45. end
  46. defp ensure_language_fallbacks(tags) do
  47. Enum.flat_map(tags, fn tag ->
  48. [language | _] = String.split(tag, "-")
  49. if Enum.member?(tags, language), do: [tag], else: [tag, language]
  50. end)
  51. end
  52. end