mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-06-16 12:10:40 +00:00
Deepl: use :base_url
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
2b739faa7e
commit
fedae008c8
|
@ -3540,10 +3540,10 @@ config :pleroma, :config_description, [
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
group: {:subgroup, Pleroma.Language.Translation.Deepl},
|
group: {:subgroup, Pleroma.Language.Translation.Deepl},
|
||||||
key: :plan,
|
key: :base_url,
|
||||||
label: "DeepL plan",
|
label: "DeepL base URL",
|
||||||
type: {:dropdown, :atom},
|
type: :string,
|
||||||
suggestions: [:free, :pro]
|
suggestions: ["https://api-free.deepl.com", "https://api.deepl.com"]
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
group: {:subgroup, Pleroma.Language.Translation.Deepl},
|
group: {:subgroup, Pleroma.Language.Translation.Deepl},
|
||||||
|
@ -3555,7 +3555,7 @@ config :pleroma, :config_description, [
|
||||||
%{
|
%{
|
||||||
group: {:subgroup, Pleroma.Language.Translation.Libretranslate},
|
group: {:subgroup, Pleroma.Language.Translation.Libretranslate},
|
||||||
key: :base_url,
|
key: :base_url,
|
||||||
label: "LibreTranslate plan",
|
label: "LibreTranslate instance URL",
|
||||||
type: :string,
|
type: :string,
|
||||||
suggestions: ["https://libretranslate.com"]
|
suggestions: ["https://libretranslate.com"]
|
||||||
},
|
},
|
||||||
|
|
|
@ -11,12 +11,12 @@ defmodule Pleroma.Language.Translation.Deepl do
|
||||||
|
|
||||||
@impl Provider
|
@impl Provider
|
||||||
def configured? do
|
def configured? do
|
||||||
not_empty_string(get_plan()) and not_empty_string(get_api_key())
|
is_atom(get_base_url()) and not_empty_string(get_api_key())
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl Provider
|
@impl Provider
|
||||||
def translate(content, source_language, target_language) do
|
def translate(content, source_language, target_language) do
|
||||||
endpoint = endpoint_url()
|
endpoint = get_base_url()
|
||||||
|
|
||||||
case Pleroma.HTTP.post(
|
case Pleroma.HTTP.post(
|
||||||
endpoint <>
|
endpoint <>
|
||||||
|
@ -58,15 +58,8 @@ defmodule Pleroma.Language.Translation.Deepl do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp endpoint_url do
|
defp get_base_url do
|
||||||
case get_plan() do
|
Pleroma.Config.get([__MODULE__, :base_url])
|
||||||
:free -> "https://api-free.deepl.com/v2/translate"
|
|
||||||
_ -> "https://api.deepl.com/v2/translate"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp get_plan do
|
|
||||||
Pleroma.Config.get([__MODULE__, :plan])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_api_key do
|
defp get_api_key do
|
||||||
|
|
27
test/pleroma/language/translation/deepl_test.ex
Normal file
27
test/pleroma/language/translation/deepl_test.ex
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Language.Translation.DeeplTest do
|
||||||
|
use Pleroma.Web.ConnCase
|
||||||
|
|
||||||
|
alias Pleroma.Language.Translation.Deepl
|
||||||
|
|
||||||
|
test "it translates text" do
|
||||||
|
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
||||||
|
clear_config([Pleroma.Language.Translation.Deepl, :base_url], "https://api-free.deepl.com")
|
||||||
|
clear_config([Pleroma.Language.Translation.Deepl, :api_key], "API_KEY")
|
||||||
|
|
||||||
|
{:ok, res} =
|
||||||
|
Deepl.translate(
|
||||||
|
"USUNĄĆ ŚLEDZIKA!Wklej to na swojego śledzika. Jeżeli uzbieramy 70% użytkowników nk...to usuną śledzika!!!",
|
||||||
|
"pl",
|
||||||
|
"en"
|
||||||
|
)
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
detected_source_language: "PL",
|
||||||
|
provider: "DeepL"
|
||||||
|
} = res
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue