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.
This commit is contained in:
parent
1a917cfeec
commit
8de573b047
@ -49,6 +49,20 @@ defmodule Pleroma.Web.Gettext do
|
|||||||
|> Enum.member?(locale)
|
|> Enum.member?(locale)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def variant?(locale), do: String.contains?(locale, "_")
|
||||||
|
|
||||||
|
def supported_variants_of_locale(locale) do
|
||||||
|
cond do
|
||||||
|
variant?(locale) ->
|
||||||
|
[locale]
|
||||||
|
supports_locale?(locale) ->
|
||||||
|
[locale]
|
||||||
|
true ->
|
||||||
|
Gettext.known_locales(Pleroma.Web.Gettext)
|
||||||
|
|> Enum.filter(fn l -> String.starts_with?(l, locale <> "_") end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def locale_or_default(locale) do
|
def locale_or_default(locale) do
|
||||||
if supports_locale?(locale) do
|
if supports_locale?(locale) do
|
||||||
locale
|
locale
|
||||||
|
@ -20,6 +20,12 @@ defmodule Pleroma.Web.Plugs.SetLocalePlug do
|
|||||||
conn
|
conn
|
||||||
|> extract_preferred_language()
|
|> extract_preferred_language()
|
||||||
|> normalize_language_codes()
|
|> normalize_language_codes()
|
||||||
|
|> first_supported()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp first_supported(locales) do
|
||||||
|
locales
|
||||||
|
|> Enum.flat_map(&Pleroma.Web.Gettext.supported_variants_of_locale/1)
|
||||||
|> Enum.find(&supported_locale?/1)
|
|> Enum.find(&supported_locale?/1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,6 +47,20 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||||||
assert %{locale: "zh_Hans"} == conn.assigns
|
assert %{locale: "zh_Hans"} == conn.assigns
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "fallback to some variant of the language if the unqualified language is not supported" do
|
||||||
|
conn =
|
||||||
|
:get
|
||||||
|
|> conn("/cofe")
|
||||||
|
|> Conn.put_req_header(
|
||||||
|
"accept-language",
|
||||||
|
"zh;q=0.9, en;q=0.8, *;q=0.5"
|
||||||
|
)
|
||||||
|
|> SetLocalePlug.call([])
|
||||||
|
|
||||||
|
assert "zh_" <> _ = Gettext.get_locale()
|
||||||
|
assert %{locale: "zh_" <> _} = conn.assigns
|
||||||
|
end
|
||||||
|
|
||||||
test "use supported locale from cookie" do
|
test "use supported locale from cookie" do
|
||||||
conn =
|
conn =
|
||||||
:get
|
:get
|
||||||
|
Loading…
Reference in New Issue
Block a user