Support multiple locales from userLanguage cookie
This commit is contained in:
parent
7ea330b4fe
commit
aca11fb70e
3 changed files with 39 additions and 17 deletions
|
|
@ -37,7 +37,7 @@ defmodule Pleroma.Web.Gettext do
|
|||
|
||||
def normalize_locale(locale) do
|
||||
if is_binary(locale) do
|
||||
String.replace(locale, "-", "_")
|
||||
String.replace(locale, "-", "_", global: true)
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
|
@ -51,13 +51,28 @@ defmodule Pleroma.Web.Gettext do
|
|||
|
||||
def variant?(locale), do: String.contains?(locale, "_")
|
||||
|
||||
def supported_variants_of_locale(locale) do
|
||||
cond do
|
||||
variant?(locale) ->
|
||||
[locale]
|
||||
def language_for_variant(locale) do
|
||||
Enum.at(String.split(locale, "_"), 0)
|
||||
end
|
||||
|
||||
def ensure_fallbacks(locales) do
|
||||
locales
|
||||
|> Enum.flat_map(fn locale ->
|
||||
others = other_supported_variants_of_locale(locale)
|
||||
|> Enum.filter(fn l -> not Enum.member?(locales, l) end)
|
||||
|
||||
[locale] ++ others
|
||||
end)
|
||||
end
|
||||
|
||||
def other_supported_variants_of_locale(locale) do
|
||||
cond do
|
||||
supports_locale?(locale) ->
|
||||
[locale]
|
||||
[]
|
||||
|
||||
variant?(locale) ->
|
||||
lang = language_for_variant(locale)
|
||||
if supports_locale?(lang), do: [lang], else: []
|
||||
|
||||
true ->
|
||||
Gettext.known_locales(Pleroma.Web.Gettext)
|
||||
|
|
|
|||
|
|
@ -26,11 +26,12 @@ defmodule Pleroma.Web.Plugs.SetLocalePlug do
|
|||
|> extract_preferred_language()
|
||||
|> normalize_language_codes()
|
||||
|> all_supported()
|
||||
|> Enum.uniq()
|
||||
end
|
||||
|
||||
defp all_supported(locales) do
|
||||
locales
|
||||
|> Enum.flat_map(&Pleroma.Web.Gettext.supported_variants_of_locale/1)
|
||||
|> Pleroma.Web.Gettext.ensure_fallbacks()
|
||||
|> Enum.filter(&supported_locale?/1)
|
||||
end
|
||||
|
||||
|
|
@ -53,8 +54,7 @@ defmodule Pleroma.Web.Plugs.SetLocalePlug do
|
|||
[]
|
||||
|
||||
fe_lang ->
|
||||
[fe_lang]
|
||||
|> ensure_language_fallbacks()
|
||||
String.split(fe_lang, ",")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -67,7 +67,6 @@ defmodule Pleroma.Web.Plugs.SetLocalePlug do
|
|||
|> Enum.sort(&(&1.quality > &2.quality))
|
||||
|> Enum.map(& &1.tag)
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|> ensure_language_fallbacks()
|
||||
|
||||
_ ->
|
||||
[]
|
||||
|
|
@ -89,11 +88,4 @@ defmodule Pleroma.Web.Plugs.SetLocalePlug do
|
|||
|
||||
%{tag: captures["tag"], quality: quality}
|
||||
end
|
||||
|
||||
defp ensure_language_fallbacks(tags) do
|
||||
Enum.flat_map(tags, fn tag ->
|
||||
[language | _] = String.split(tag, "-")
|
||||
if Enum.member?(tags, language), do: [tag], else: [tag, language]
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue