Add test for translateLocally
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
9e19753269
commit
eb1995d7c6
4 changed files with 104 additions and 22 deletions
|
@ -3544,7 +3544,8 @@
|
||||||
type: :module,
|
type: :module,
|
||||||
suggestions: [
|
suggestions: [
|
||||||
Pleroma.Language.Translation.Deepl,
|
Pleroma.Language.Translation.Deepl,
|
||||||
Pleroma.Language.Translation.Libretranslate
|
Pleroma.Language.Translation.Libretranslate,
|
||||||
|
Pleroma.Language.Translation.TranslateLocally
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
|
@ -3586,6 +3587,14 @@
|
||||||
label: "LibreTranslate API Key",
|
label: "LibreTranslate API Key",
|
||||||
type: :string,
|
type: :string,
|
||||||
suggestions: ["YOUR_API_KEY"]
|
suggestions: ["YOUR_API_KEY"]
|
||||||
|
},
|
||||||
|
%{
|
||||||
|
group: {:subgroup, Pleroma.Language.Translation.TranslateLocally},
|
||||||
|
key: :intermediate_language,
|
||||||
|
label:
|
||||||
|
"translateLocally intermediate language (used when direct source->target model is not available)",
|
||||||
|
type: :string,
|
||||||
|
suggestions: ["en"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# Pleroma: A lightweight social networking server
|
# Pleroma: A lightweight social networking server
|
||||||
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
# Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.Language.Translation.TranslateLocally do
|
defmodule Pleroma.Language.Translation.TranslateLocally do
|
||||||
|
@ -71,16 +71,24 @@ def translate(content, source_language, target_language) do
|
||||||
|
|
||||||
@impl Provider
|
@impl Provider
|
||||||
def supported_languages(:source) do
|
def supported_languages(:source) do
|
||||||
languages_matrix()
|
languages =
|
||||||
|> Map.keys()
|
languages_matrix()
|
||||||
|
|> elem(1)
|
||||||
|
|> Map.keys()
|
||||||
|
|
||||||
|
{:ok, languages}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl Provider
|
@impl Provider
|
||||||
def supported_languages(:target) do
|
def supported_languages(:target) do
|
||||||
languages_matrix()
|
languages =
|
||||||
|> Map.values()
|
languages_matrix()
|
||||||
|> List.flatten()
|
|> elem(1)
|
||||||
|> Enum.uniq()
|
|> Map.values()
|
||||||
|
|> List.flatten()
|
||||||
|
|> Enum.uniq()
|
||||||
|
|
||||||
|
{:ok, languages}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl Provider
|
@impl Provider
|
||||||
|
@ -91,21 +99,25 @@ def languages_matrix do
|
||||||
|> Enum.map(fn {key, value} -> {key, Map.keys(value)} end)
|
|> Enum.map(fn {key, value} -> {key, Map.keys(value)} end)
|
||||||
|> Enum.into(%{})
|
|> Enum.into(%{})
|
||||||
|
|
||||||
if intermediary_language() do
|
matrix =
|
||||||
languages
|
if intermediary_language() do
|
||||||
|> Map.to_list()
|
languages
|
||||||
|> Enum.map(fn {key, value} ->
|
|> Map.to_list()
|
||||||
with_intermediary =
|
|> Enum.map(fn {key, value} ->
|
||||||
((value ++ languages[intermediary_language()])
|
with_intermediary =
|
||||||
|> Enum.uniq()) --
|
(((value ++ languages[intermediary_language()])
|
||||||
[intermediary_language()]
|
|> Enum.uniq()) --
|
||||||
|
[key])
|
||||||
|
|> Enum.sort()
|
||||||
|
|
||||||
{key, with_intermediary}
|
{key, with_intermediary}
|
||||||
end)
|
end)
|
||||||
|> Enum.into(%{})
|
|> Enum.into(%{})
|
||||||
else
|
else
|
||||||
languages
|
languages
|
||||||
end
|
end
|
||||||
|
|
||||||
|
{:ok, matrix}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl Provider
|
@impl Provider
|
||||||
|
|
59
test/pleroma/language/translation/translate_locally_test.exs
Normal file
59
test/pleroma/language/translation/translate_locally_test.exs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Language.Translation.TranslateLocallyTest do
|
||||||
|
use Pleroma.DataCase
|
||||||
|
|
||||||
|
alias Pleroma.Language.Translation.TranslateLocally
|
||||||
|
|
||||||
|
@example_models %{
|
||||||
|
"de" => %{
|
||||||
|
"en" => "de-en-base"
|
||||||
|
},
|
||||||
|
"en" => %{
|
||||||
|
"de" => "en-de-base",
|
||||||
|
"pl" => "en-pl-tiny"
|
||||||
|
},
|
||||||
|
"cs" => %{
|
||||||
|
"en" => "cs-en-base"
|
||||||
|
},
|
||||||
|
"pl" => %{
|
||||||
|
"en" => "pl-en-tiny"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "it returns languages list" do
|
||||||
|
clear_config([Pleroma.Language.Translation.TranslateLocally, :models], @example_models)
|
||||||
|
|
||||||
|
assert {:ok, languages} = TranslateLocally.supported_languages(:source)
|
||||||
|
assert ["cs", "de", "en", "pl"] = languages |> Enum.sort()
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "it returns languages matrix" do
|
||||||
|
test "without intermediary language" do
|
||||||
|
clear_config([Pleroma.Language.Translation.TranslateLocally, :models], @example_models)
|
||||||
|
|
||||||
|
assert {:ok,
|
||||||
|
%{
|
||||||
|
"cs" => ["en"],
|
||||||
|
"de" => ["en"],
|
||||||
|
"en" => ["de", "pl"],
|
||||||
|
"pl" => ["en"]
|
||||||
|
}} = TranslateLocally.languages_matrix()
|
||||||
|
end
|
||||||
|
|
||||||
|
test "with intermediary language" do
|
||||||
|
clear_config([Pleroma.Language.Translation.TranslateLocally, :models], @example_models)
|
||||||
|
clear_config([Pleroma.Language.Translation.TranslateLocally, :intermediary_language], "en")
|
||||||
|
|
||||||
|
assert {:ok,
|
||||||
|
%{
|
||||||
|
"cs" => ["de", "en", "pl"],
|
||||||
|
"de" => ["en", "pl"],
|
||||||
|
"en" => ["de", "pl"],
|
||||||
|
"pl" => ["de", "en"]
|
||||||
|
}} = TranslateLocally.languages_matrix()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -5,6 +5,8 @@
|
||||||
defmodule TranslationMock do
|
defmodule TranslationMock do
|
||||||
alias Pleroma.Language.Translation.Provider
|
alias Pleroma.Language.Translation.Provider
|
||||||
|
|
||||||
|
use Provider
|
||||||
|
|
||||||
@behaviour Provider
|
@behaviour Provider
|
||||||
|
|
||||||
@name "TranslationMock"
|
@name "TranslationMock"
|
||||||
|
|
Loading…
Reference in a new issue