Merge branch 'from/upstream-develop/tusooa/config-translatable' into 'develop'
Translatable config descriptions Closes pleroma-meta#65 See merge request pleroma/pleroma!3695
This commit is contained in:
commit
bb4860e222
8 changed files with 6571 additions and 258 deletions
10
lib/pleroma/docs/translator.ex
Normal file
10
lib/pleroma/docs/translator.ex
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Docs.Translator do
|
||||
require Pleroma.Docs.Translator.Compiler
|
||||
require Pleroma.Web.Gettext
|
||||
|
||||
@before_compile Pleroma.Docs.Translator.Compiler
|
||||
end
|
||||
119
lib/pleroma/docs/translator/compiler.ex
Normal file
119
lib/pleroma/docs/translator/compiler.ex
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Docs.Translator.Compiler do
|
||||
@external_resource "config/description.exs"
|
||||
@raw_config Pleroma.Config.Loader.read("config/description.exs")
|
||||
@raw_descriptions @raw_config[:pleroma][:config_description]
|
||||
|
||||
defmacro __before_compile__(_env) do
|
||||
strings =
|
||||
__MODULE__.descriptions()
|
||||
|> __MODULE__.extract_strings()
|
||||
|
||||
quote do
|
||||
def placeholder do
|
||||
unquote do
|
||||
Enum.map(
|
||||
strings,
|
||||
fn {path, type, string} ->
|
||||
ctxt = msgctxt_for(path, type)
|
||||
|
||||
quote do
|
||||
Pleroma.Web.Gettext.dpgettext_noop(
|
||||
"config_descriptions",
|
||||
unquote(ctxt),
|
||||
unquote(string)
|
||||
)
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def descriptions do
|
||||
Pleroma.Web.ActivityPub.MRF.config_descriptions()
|
||||
|> Enum.reduce(@raw_descriptions, fn description, acc -> [description | acc] end)
|
||||
|> Pleroma.Docs.Generator.convert_to_strings()
|
||||
end
|
||||
|
||||
def extract_strings(descriptions) do
|
||||
descriptions
|
||||
|> Enum.reduce(%{strings: [], path: []}, &process_item/2)
|
||||
|> Map.get(:strings)
|
||||
end
|
||||
|
||||
defp process_item(entity, acc) do
|
||||
current_level =
|
||||
acc
|
||||
|> process_desc(entity)
|
||||
|> process_label(entity)
|
||||
|
||||
process_children(entity, current_level)
|
||||
end
|
||||
|
||||
defp process_desc(acc, %{description: desc} = item) do
|
||||
%{
|
||||
strings: [{acc.path ++ [key_for(item)], "description", desc} | acc.strings],
|
||||
path: acc.path
|
||||
}
|
||||
end
|
||||
|
||||
defp process_desc(acc, _) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp process_label(acc, %{label: label} = item) do
|
||||
%{
|
||||
strings: [{acc.path ++ [key_for(item)], "label", label} | acc.strings],
|
||||
path: acc.path
|
||||
}
|
||||
end
|
||||
|
||||
defp process_label(acc, _) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp process_children(%{children: children} = item, acc) do
|
||||
current_level = Map.put(acc, :path, acc.path ++ [key_for(item)])
|
||||
|
||||
children
|
||||
|> Enum.reduce(current_level, &process_item/2)
|
||||
|> Map.put(:path, acc.path)
|
||||
end
|
||||
|
||||
defp process_children(_, acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
def msgctxt_for(path, type) do
|
||||
"config #{type} at #{Enum.join(path, " > ")}"
|
||||
end
|
||||
|
||||
defp convert_group({_, group}) do
|
||||
group
|
||||
end
|
||||
|
||||
defp convert_group(group) do
|
||||
group
|
||||
end
|
||||
|
||||
def key_for(%{group: group, key: key}) do
|
||||
"#{convert_group(group)}-#{key}"
|
||||
end
|
||||
|
||||
def key_for(%{group: group}) do
|
||||
convert_group(group)
|
||||
end
|
||||
|
||||
def key_for(%{key: key}) do
|
||||
key
|
||||
end
|
||||
|
||||
def key_for(_) do
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
|
@ -22,10 +22,58 @@ defmodule Pleroma.Web.AdminAPI.ConfigController do
|
|||
|
||||
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ConfigOperation
|
||||
|
||||
defp translate_descriptions(descriptions, path \\ []) do
|
||||
Enum.map(descriptions, fn desc -> translate_item(desc, path) end)
|
||||
end
|
||||
|
||||
defp translate_string(str, path, type) do
|
||||
Gettext.dpgettext(
|
||||
Pleroma.Web.Gettext,
|
||||
"config_descriptions",
|
||||
Pleroma.Docs.Translator.Compiler.msgctxt_for(path, type),
|
||||
str
|
||||
)
|
||||
end
|
||||
|
||||
defp maybe_put_translated(item, key, path) do
|
||||
if item[key] do
|
||||
Map.put(
|
||||
item,
|
||||
key,
|
||||
translate_string(
|
||||
item[key],
|
||||
path ++ [Pleroma.Docs.Translator.Compiler.key_for(item)],
|
||||
to_string(key)
|
||||
)
|
||||
)
|
||||
else
|
||||
item
|
||||
end
|
||||
end
|
||||
|
||||
defp translate_item(item, path) do
|
||||
item
|
||||
|> maybe_put_translated(:label, path)
|
||||
|> maybe_put_translated(:description, path)
|
||||
|> translate_children(path)
|
||||
end
|
||||
|
||||
defp translate_children(%{children: children} = item, path) when is_list(children) do
|
||||
item
|
||||
|> Map.put(
|
||||
:children,
|
||||
translate_descriptions(children, path ++ [Pleroma.Docs.Translator.Compiler.key_for(item)])
|
||||
)
|
||||
end
|
||||
|
||||
defp translate_children(item, _path) do
|
||||
item
|
||||
end
|
||||
|
||||
def descriptions(conn, _params) do
|
||||
descriptions = Enum.filter(Pleroma.Docs.JSON.compiled_descriptions(), &whitelisted_config?/1)
|
||||
|
||||
json(conn, descriptions)
|
||||
json(conn, translate_descriptions(descriptions))
|
||||
end
|
||||
|
||||
def show(conn, %{only_db: true}) do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue