2019-07-09 11:30:15 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-26 06:11:42 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 11:30:15 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
# NOTE: this module is based on https://github.com/smeevil/set_locale
|
2020-06-24 06:24:29 +00:00
|
|
|
defmodule Pleroma.Web.Plugs.SetLocalePlug do
|
2019-07-09 11:30:15 +00:00
|
|
|
import Plug.Conn, only: [get_req_header: 2, assign: 3]
|
|
|
|
|
2022-02-22 00:12:32 +00:00
|
|
|
def frontend_language_cookie_name, do: "userLanguage"
|
2022-02-21 22:54:18 +00:00
|
|
|
|
2019-07-09 11:30:15 +00:00
|
|
|
def init(_), do: nil
|
|
|
|
|
|
|
|
def call(conn, _) do
|
2022-03-03 07:03:44 +00:00
|
|
|
locales = get_locales_from_header(conn)
|
|
|
|
first_locale = Enum.at(locales, 0, Gettext.get_locale())
|
|
|
|
|
|
|
|
Pleroma.Web.Gettext.put_locales(locales)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:locale, first_locale)
|
|
|
|
|> assign(:locales, locales)
|
2019-07-09 11:30:15 +00:00
|
|
|
end
|
|
|
|
|
2022-03-03 07:03:44 +00:00
|
|
|
defp get_locales_from_header(conn) do
|
2019-07-09 11:30:15 +00:00
|
|
|
conn
|
2022-02-21 22:54:18 +00:00
|
|
|
|> extract_preferred_language()
|
|
|
|
|> normalize_language_codes()
|
2022-03-03 07:03:44 +00:00
|
|
|
|> all_supported()
|
2022-03-03 07:31:36 +00:00
|
|
|
|> Enum.uniq()
|
Fallback to a variant if the language in general is not supported
For an example, here, zh is not supported, but zh_Hans and zh_Hant
are. If the user asks for zh, we should choose a variant for them
instead of fallbacking to default.
Some browsers (e.g. Firefox) does not allow users to customize
their language codes. For example, there is no zh-Hans, but only
zh, zh-CN, zh-TW, zh-HK, etc. This provides a workaround for
those users suffering from bad design decisions.
2022-03-03 00:59:11 +00:00
|
|
|
end
|
|
|
|
|
2022-03-03 07:03:44 +00:00
|
|
|
defp all_supported(locales) do
|
Fallback to a variant if the language in general is not supported
For an example, here, zh is not supported, but zh_Hans and zh_Hant
are. If the user asks for zh, we should choose a variant for them
instead of fallbacking to default.
Some browsers (e.g. Firefox) does not allow users to customize
their language codes. For example, there is no zh-Hans, but only
zh, zh-CN, zh-TW, zh-HK, etc. This provides a workaround for
those users suffering from bad design decisions.
2022-03-03 00:59:11 +00:00
|
|
|
locales
|
2022-03-03 07:31:36 +00:00
|
|
|
|> Pleroma.Web.Gettext.ensure_fallbacks()
|
2022-03-03 07:03:44 +00:00
|
|
|
|> Enum.filter(&supported_locale?/1)
|
2019-07-09 11:30:15 +00:00
|
|
|
end
|
|
|
|
|
2022-02-21 22:54:18 +00:00
|
|
|
defp normalize_language_codes(codes) do
|
|
|
|
codes
|
2022-03-02 06:41:13 +00:00
|
|
|
|> Enum.map(fn code -> Pleroma.Web.Gettext.normalize_locale(code) end)
|
2022-02-21 22:54:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_preferred_language(conn) do
|
|
|
|
extract_frontend_language(conn) ++ extract_accept_language(conn)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_frontend_language(conn) do
|
|
|
|
%{req_cookies: cookies} =
|
|
|
|
conn
|
|
|
|
|> Plug.Conn.fetch_cookies()
|
|
|
|
|
|
|
|
case cookies[frontend_language_cookie_name()] do
|
|
|
|
nil ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
fe_lang ->
|
2022-03-03 07:31:36 +00:00
|
|
|
String.split(fe_lang, ",")
|
2022-02-21 22:54:18 +00:00
|
|
|
end
|
2019-07-09 11:30:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_accept_language(conn) do
|
|
|
|
case get_req_header(conn, "accept-language") do
|
|
|
|
[value | _] ->
|
|
|
|
value
|
|
|
|
|> String.split(",")
|
|
|
|
|> Enum.map(&parse_language_option/1)
|
|
|
|
|> Enum.sort(&(&1.quality > &2.quality))
|
|
|
|
|> Enum.map(& &1.tag)
|
|
|
|
|> Enum.reject(&is_nil/1)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp supported_locale?(locale) do
|
2022-03-02 02:24:17 +00:00
|
|
|
Pleroma.Web.Gettext.supports_locale?(locale)
|
2019-07-09 11:30:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp parse_language_option(string) do
|
|
|
|
captures = Regex.named_captures(~r/^\s?(?<tag>[\w\-]+)(?:;q=(?<quality>[\d\.]+))?$/i, string)
|
|
|
|
|
|
|
|
quality =
|
|
|
|
case Float.parse(captures["quality"] || "1.0") do
|
|
|
|
{val, _} -> val
|
|
|
|
:error -> 1.0
|
|
|
|
end
|
|
|
|
|
|
|
|
%{tag: captures["tag"], quality: quality}
|
|
|
|
end
|
|
|
|
end
|