Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-10-31 21:58:10 +01:00
parent 066ec8fe95
commit 2b739faa7e
10 changed files with 34 additions and 31 deletions

View file

@ -3526,38 +3526,41 @@ config :pleroma, :config_description, [
},
%{
group: :pleroma,
key: Pleroma.Translation,
key: Pleroma.Language.Translation,
type: :group,
description: "Translation providers",
children: [
%{
key: :service,
key: :provider,
type: :module,
suggestions: [Pleroma.Translation.Deepl, Pleroma.Translation.Libretranslate]
suggestions: [
Pleroma.Language.Translation.Deepl,
Pleroma.Language.Translation.Libretranslate
]
},
%{
group: {:subgroup, Pleroma.Translation.Deepl},
group: {:subgroup, Pleroma.Language.Translation.Deepl},
key: :plan,
label: "DeepL plan",
type: {:dropdown, :atom},
suggestions: [:free, :pro]
},
%{
group: {:subgroup, Pleroma.Translation.Deepl},
group: {:subgroup, Pleroma.Language.Translation.Deepl},
key: :api_key,
label: "DeepL API Key",
type: :string,
suggestions: ["YOUR_API_KEY"]
},
%{
group: {:subgroup, Pleroma.Translation.Libretranslate},
group: {:subgroup, Pleroma.Language.Translation.Libretranslate},
key: :base_url,
label: "LibreTranslate plan",
type: :string,
suggestions: ["https://libretranslate.com"]
},
%{
group: {:subgroup, Pleroma.Translation.Libretranslate},
group: {:subgroup, Pleroma.Language.Translation.Libretranslate},
key: :api_key,
label: "LibreTranslate API Key",
type: :string,

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Translation do
defmodule Pleroma.Language.Translation do
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
def configured? do
@ -37,7 +37,7 @@ defmodule Pleroma.Translation do
end
end
defp get_service, do: Pleroma.Config.get([__MODULE__, :service])
defp get_service, do: Pleroma.Config.get([__MODULE__, :provider])
defp get_cache_key(text, source_language, target_language) do
"#{source_language}/#{target_language}/#{content_hash(text)}"

View file

@ -2,19 +2,19 @@
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Translation.Deepl do
defmodule Pleroma.Language.Translation.Deepl do
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
alias Pleroma.Translation.Service
alias Pleroma.Language.Translation.Provider
@behaviour Service
@behaviour Provider
@impl Service
@impl Provider
def configured? do
not_empty_string(get_plan()) and not_empty_string(get_api_key())
end
@impl Service
@impl Provider
def translate(content, source_language, target_language) do
endpoint = endpoint_url()

View file

@ -2,17 +2,17 @@
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Translation.Libretranslate do
defmodule Pleroma.Language.Translation.Libretranslate do
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
alias Pleroma.Translation.Service
alias Pleroma.Language.Translation.Provider
@behaviour Service
@behaviour Provider
@impl Service
@impl Provider
def configured?, do: not_empty_string(get_base_url())
@impl Service
@impl Provider
def translate(content, source_language, target_language) do
endpoint = endpoint_url()

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Translation.Service do
defmodule Pleroma.Language.Translation.Provider do
@callback configured?() :: boolean()
@callback translate(

View file

@ -12,10 +12,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
alias Pleroma.Activity
alias Pleroma.Bookmark
alias Pleroma.Language.Translation
alias Pleroma.Object
alias Pleroma.Repo
alias Pleroma.ScheduledActivity
alias Pleroma.Translation
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Visibility

View file

@ -130,7 +130,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do
end,
"pleroma:get:main/ostatus",
"pleroma:group_actors",
if Pleroma.Translation.configured?() do
if Pleroma.Language.Translation.configured?() do
"translation"
end
]
@ -206,7 +206,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do
vapid: %{
public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
},
translation: %{enabled: Pleroma.Translation.configured?()}
translation: %{enabled: Pleroma.Language.Translation.configured?()}
})
end

View file

@ -1,10 +1,10 @@
defmodule Pleroma.TranslationTest do
defmodule Pleroma.Language.TranslationTest do
use Pleroma.Web.ConnCase
alias Pleroma.Translation
alias Pleroma.Language.Translation
# use Oban.Testing, repo: Pleroma.Repo
setup do: clear_config([Pleroma.Translation, :service], TranslationMock)
setup do: clear_config([Pleroma.Language.Translation, :provider], TranslationMock)
test "it translates text" do
assert {:ok,

View file

@ -2552,7 +2552,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
end
describe "translating statuses" do
setup do: clear_config([Pleroma.Translation, :service], TranslationMock)
setup do: clear_config([Pleroma.Language.Translation, :provider], TranslationMock)
test "it translates a status to user language" do
user = insert(:user, language: "fr")

View file

@ -3,14 +3,14 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule TranslationMock do
alias Pleroma.Translation.Service
alias Pleroma.Language.Translation.Provider
@behaviour Service
@behaviour Provider
@impl Service
@impl Provider
def configured?, do: true
@impl Service
@impl Provider
def translate(content, source_language, _target_language) do
{:ok,
%{