Pass in msgctxt for config translation strings

This commit is contained in:
Tusooa Zhu 2022-07-14 17:41:33 -04:00
parent 074a94e90d
commit 1d7e8d6e01
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
4 changed files with 5799 additions and 4242 deletions

View File

@ -17,9 +17,15 @@ defmodule Pleroma.Docs.Translator.Compiler do
unquote do unquote do
Enum.map( Enum.map(
strings, strings,
fn string -> fn {path, type, string} ->
ctxt = msgctxt_for(path, type)
quote do quote do
Pleroma.Web.Gettext.dgettext_noop("config_descriptions", unquote(string)) Pleroma.Web.Gettext.dpgettext_noop(
"config_descriptions",
unquote(ctxt),
unquote(string)
)
end end
end end
) )
@ -36,7 +42,8 @@ defmodule Pleroma.Docs.Translator.Compiler do
def extract_strings(descriptions) do def extract_strings(descriptions) do
descriptions descriptions
|> Enum.reduce([], &process_item/2) |> Enum.reduce(%{strings: [], path: []}, &process_item/2)
|> Map.get(:strings)
end end
defp process_item(entity, acc) do defp process_item(entity, acc) do
@ -48,28 +55,65 @@ defmodule Pleroma.Docs.Translator.Compiler do
process_children(entity, current_level) process_children(entity, current_level)
end end
defp process_desc(acc, %{description: desc}) do defp process_desc(acc, %{description: desc} = item) do
[desc | acc] %{
strings: [{acc.path ++ [key_for(item)], "description", desc} | acc.strings],
path: acc.path
}
end end
defp process_desc(acc, _) do defp process_desc(acc, _) do
acc acc
end end
defp process_label(acc, %{label: label}) do defp process_label(acc, %{label: label} = item) do
[label | acc] %{
strings: [{acc.path ++ [key_for(item)], "label", label} | acc.strings],
path: acc.path
}
end end
defp process_label(acc, _) do defp process_label(acc, _) do
acc acc
end end
defp process_children(%{children: children}, acc) do defp process_children(%{children: children} = item, acc) do
current_level = Map.put(acc, :path, acc.path ++ [key_for(item)])
children children
|> Enum.reduce(acc, &process_item/2) |> Enum.reduce(current_level, &process_item/2)
|> Map.put(:path, acc.path)
end end
defp process_children(_, acc) do defp process_children(_, acc) do
acc acc
end 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 end

View File

@ -22,35 +22,51 @@ defmodule Pleroma.Web.AdminAPI.ConfigController do
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ConfigOperation defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ConfigOperation
defp translate_descriptions(descriptions) do defp translate_descriptions(descriptions, path \\ []) do
Enum.map(descriptions, &translate_item/1) Enum.map(descriptions, fn desc -> translate_item(desc, path) end)
end end
defp translate_string(str) do defp translate_string(str, path, type) do
Gettext.dgettext(Pleroma.Web.Gettext, "config_descriptions", str) Gettext.dpgettext(
Pleroma.Web.Gettext,
"config_descriptions",
Pleroma.Docs.Translator.Compiler.msgctxt_for(path, type),
str
)
end end
defp maybe_put_translated(item, key) do defp maybe_put_translated(item, key, path) do
if item[key] do if item[key] do
Map.put(item, key, translate_string(item[key])) Map.put(
item,
key,
translate_string(
item[key],
path ++ [Pleroma.Docs.Translator.Compiler.key_for(item)],
to_string(key)
)
)
else else
item item
end end
end end
defp translate_item(item) do defp translate_item(item, path) do
item item
|> maybe_put_translated(:label) |> maybe_put_translated(:label, path)
|> maybe_put_translated(:description) |> maybe_put_translated(:description, path)
|> translate_children() |> translate_children(path)
end end
defp translate_children(%{children: children} = item) when is_list(children) do defp translate_children(%{children: children} = item, path) when is_list(children) do
item item
|> Map.put(:children, translate_descriptions(children)) |> Map.put(
:children,
translate_descriptions(children, path ++ [Pleroma.Docs.Translator.Compiler.key_for(item)])
)
end end
defp translate_children(item) do defp translate_children(item, _path) do
item item
end end

File diff suppressed because it is too large Load Diff

View File

@ -9,30 +9,36 @@ defmodule Pleroma.Docs.Translator.CompilerTest do
@descriptions [ @descriptions [
%{ %{
key: "1",
label: "1", label: "1",
description: "2", description: "2",
children: [ children: [
%{ %{
key: "3",
label: "3", label: "3",
description: "4" description: "4"
}, },
%{ %{
key: "5",
label: "5", label: "5",
description: "6" description: "6"
} }
] ]
}, },
%{ %{
key: "7",
label: "7", label: "7",
description: "8", description: "8",
children: [ children: [
%{ %{
key: "9",
description: "9", description: "9",
children: [ children: [
%{ %{
key: "10",
description: "10", description: "10",
children: [ children: [
%{description: "11"}, %{key: "11", description: "11"},
%{description: "12"} %{description: "12"}
] ]
} }
@ -42,14 +48,43 @@ defmodule Pleroma.Docs.Translator.CompilerTest do
label: "13" label: "13"
} }
] ]
},
%{
group: "14",
label: "14"
},
%{
group: "15",
key: "15",
label: "15"
},
%{
group: {":subgroup", "16"},
label: "16"
} }
] ]
describe "extract_strings/1" do describe "extract_strings/1" do
test "it extracts all labels and descriptions" do test "it extracts all labels and descriptions" do
strings = Compiler.extract_strings(@descriptions) strings = Compiler.extract_strings(@descriptions)
assert length(strings) == 13 assert length(strings) == 16
assert Enum.all?(1..13, &(to_string(&1) in strings))
assert {["1"], "label", "1"} in strings
assert {["1"], "description", "2"} in strings
assert {["1", "3"], "label", "3"} in strings
assert {["1", "3"], "description", "4"} in strings
assert {["1", "5"], "label", "5"} in strings
assert {["1", "5"], "description", "6"} in strings
assert {["7"], "label", "7"} in strings
assert {["7"], "description", "8"} in strings
assert {["7", "9"], "description", "9"} in strings
assert {["7", "9", "10"], "description", "10"} in strings
assert {["7", "9", "10", "11"], "description", "11"} in strings
assert {["7", "9", "10", nil], "description", "12"} in strings
assert {["7", nil], "label", "13"} in strings
assert {["14"], "label", "14"} in strings
assert {["15-15"], "label", "15"} in strings
assert {["16"], "label", "16"} in strings
end end
end end
end end