Add functions to process multi-language content
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
00591d9016
commit
8822dc1191
3 changed files with 104 additions and 0 deletions
|
@ -931,6 +931,12 @@
|
|||
|
||||
config :pleroma, Pleroma.Web.WebFinger, domain: nil, update_nickname_on_user_fetch: true
|
||||
|
||||
config :pleroma, Pleroma.MultiLanguage,
|
||||
template: "<div lang=\"{code}\">{content}</div>",
|
||||
separator: "<br><hr><br>",
|
||||
single_line_template: "[{code}] {content}",
|
||||
single_line_separator: " | "
|
||||
|
||||
config :pleroma, Pleroma.Language.Translation, allow_unauthenticated: false, allow_remote: true
|
||||
|
||||
config :geospatial, Geospatial.Service, service: Geospatial.Providers.Nominatim
|
||||
|
|
48
lib/pleroma/multi_language.ex
Normal file
48
lib/pleroma/multi_language.ex
Normal file
|
@ -0,0 +1,48 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.MultiLanguage do
|
||||
defp template(:multi), do: Pleroma.Config.get([__MODULE__, :template])
|
||||
defp template(:single), do: Pleroma.Config.get([__MODULE__, :single_line_template])
|
||||
|
||||
defp sep(:multi), do: Pleroma.Config.get([__MODULE__, :separator])
|
||||
defp sep(:single), do: Pleroma.Config.get([__MODULE__, :single_line_separator])
|
||||
|
||||
def map_to_str(data, opts \\ []) do
|
||||
map_to_str_impl(data, if(opts[:multiline], do: :multi, else: :single))
|
||||
end
|
||||
|
||||
defp map_to_str_impl(data, mode) do
|
||||
with ks <- Map.keys(data),
|
||||
[_, _ | _] <- ks,
|
||||
ks <- Enum.sort(ks) do
|
||||
template = template(mode)
|
||||
|
||||
ks
|
||||
|> Enum.map(fn lang ->
|
||||
format_template(template, %{code: lang, content: data[lang]})
|
||||
end)
|
||||
|> Enum.join(sep(mode))
|
||||
else
|
||||
[lang] -> data[lang]
|
||||
_ -> ""
|
||||
end
|
||||
end
|
||||
|
||||
def str_to_map(data) do
|
||||
%{"und" => data}
|
||||
end
|
||||
|
||||
def format_template(template, %{code: code, content: content}) do
|
||||
template
|
||||
|> String.replace(
|
||||
["{code}", "{content}"],
|
||||
fn
|
||||
"{code}" -> code
|
||||
"{content}" -> content
|
||||
end,
|
||||
global: true
|
||||
)
|
||||
end
|
||||
end
|
50
test/pleroma/multi_language_test.exs
Normal file
50
test/pleroma/multi_language_test.exs
Normal file
|
@ -0,0 +1,50 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.MultiLanguageTest do
|
||||
use Pleroma.DataCase, async: true
|
||||
|
||||
alias Pleroma.MultiLanguage
|
||||
|
||||
describe "map_to_str" do
|
||||
setup do
|
||||
%{
|
||||
data: %{
|
||||
"en-US" => "mew",
|
||||
"en-GB" => "meow"
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
test "single line", %{data: data} do
|
||||
assert MultiLanguage.map_to_str(data) == "[en-GB] meow | [en-US] mew"
|
||||
end
|
||||
|
||||
test "multi line", %{data: data} do
|
||||
assert MultiLanguage.map_to_str(data, multiline: true) ==
|
||||
"<div lang=\"en-GB\">meow</div><br><hr><br><div lang=\"en-US\">mew</div>"
|
||||
end
|
||||
|
||||
test "only one language" do
|
||||
data = %{"some" => "foo"}
|
||||
assert MultiLanguage.map_to_str(data) == "foo"
|
||||
assert MultiLanguage.map_to_str(data, multiline: true) == "foo"
|
||||
end
|
||||
|
||||
test "resistent to tampering" do
|
||||
data = %{
|
||||
"en-US" => "mew {code} {content}",
|
||||
"en-GB" => "meow {code} {content}"
|
||||
}
|
||||
|
||||
assert MultiLanguage.map_to_str(data) == "[en-GB] meow {code} {content} | [en-US] mew {code} {content}"
|
||||
end
|
||||
end
|
||||
|
||||
describe "str_to_map" do
|
||||
test "" do
|
||||
assert MultiLanguage.str_to_map("foo") == %{"und" => "foo"}
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue