Change ArgosTranslate strategy for dealing with nil from-language

Instead of choosing a fallback language, we just return the input, supposedly translated from the target language.
This gives us a much faster response, so you can immedialty choose the correct from language yourself in pleroma-fe.

Some other cleanup and improvements to the docs are also done.
This commit is contained in:
ilja 2022-12-11 00:53:45 +01:00
parent f78bada08d
commit d1bb480146
4 changed files with 16 additions and 27 deletions

View File

@ -885,7 +885,6 @@ config :pleroma, :libre_translate,
config :pleroma, :argos_translate,
command_argos_translate: "argos-translate",
command_argospm: "argospm",
fallback_language: "en",
strip_html: true
# Import environment specific config. This must remain at the bottom

View File

@ -1119,7 +1119,7 @@ Each job has these settings:
### Translation Settings
Settings to automatically translate statuses for end users. Currently supported
translation services are DeepL and LibreTranslate.
translation services are DeepL and LibreTranslate. The supported command line tool is [Argos Translate](https://github.com/argosopentech/argos-translate).
Translations are available at `/api/v1/statuses/:id/translations/:language`, where
`language` is the target language code (e.g `en`)
@ -1128,7 +1128,7 @@ Translations are available at `/api/v1/statuses/:id/translations/:language`, whe
- `:enabled` - enables translation
- `:module` - Sets module to be used
- Either `Pleroma.Akkoma.Translators.DeepL` or `Pleroma.Akkoma.Translators.LibreTranslate`
- Either `Pleroma.Akkoma.Translators.DeepL`, `Pleroma.Akkoma.Translators.LibreTranslate`, or `Pleroma.Akkoma.Translators.ArgosTranslate`
### `:deepl`
@ -1143,9 +1143,6 @@ Translations are available at `/api/v1/statuses/:id/translations/:language`, whe
### `:argos_translate`
[Argos Translate](https://github.com/argosopentech/argos-translate) is the library used by Libre Translate and can run as a command line tool. It's more basic than Libre Translate as it doesn't provide a way to properly handle html or language detection.
- `:command_argos_translate` - command for `argos-translate`. Can be the command if it's in your PATH, or the full path to the file (default: `argos-translate`).
- `:command_argospm` - command for `argospm`. Can be the command if it's in your PATH, or the full path to the file (default: `argospm`).
- `:fallback_language` - When no language is provided to translate from, this language will be used. Must be a two letter langage code from a language you have installed (default: `en`).
- `:strip_html` - Strip html from the post before translating the text (default: `true`).
- `:strip_html` - Strip html from the post before translating it (default: `true`).

View File

@ -11,8 +11,8 @@ defmodule Pleroma.Akkoma.Translators.ArgosTranslate do
Config.get([:argos_translate, :command_argospm])
end
defp fallback_language do
Config.get([:argos_translate, :fallback_language])
defp strip_html? do
Config.get([:argos_translate, :strip_html])
end
defp safe_languages() do
@ -85,23 +85,24 @@ defmodule Pleroma.Akkoma.Translators.ArgosTranslate do
defp htmlify_response(string, _), do: string
@impl Pleroma.Akkoma.Translator
def translate(string, from_language, to_language) do
strip_html = Config.get([:argos_translate, :strip_html])
def translate(string, nil, to_language) do
# Akkoma's Pleroma-fe expects us to detect the source language automatically.
# Argos-translate doesn't have that option (yet?)
# see <https://github.com/argosopentech/argos-translate/issues/9>
# For now we choose a fallback source language from settings.
# For now we return the text unchanged, supposedly translated from the target language.
# Afterwards people get the option to overwrite the source language from a dropdown.
from_language = from_language || fallback_language()
to_language = to_language || fallback_language()
{:ok, to_language, string}
end
def translate(string, from_language, to_language) do
# Argos Translate doesn't properly translate HTML (yet?)
# For now we give admins the option to strip the html before translating
# Note that we have to add some html back to the response afterwards
string = clean_string(string, strip_html)
string = clean_string(string, strip_html?())
with {translated, 0} <-
safe_translate(string, from_language, to_language) do
{:ok, from_language, translated |> htmlify_response(strip_html)}
{:ok, from_language, translated |> htmlify_response(strip_html?())}
else
{response, _} -> {:error, "ArgosTranslate failed to translate (#{response})"}
end

View File

@ -27,19 +27,11 @@ defmodule Pleroma.Akkoma.Translators.ArgosTranslateTest do
assert [%{code: "en", name: "en"}, %{code: "nl", name: "nl"}] = dest_langs |> Enum.sort()
end
test "it translates from default language when no language is set" do
translation_response =
with_mock System, [:passthrough],
cmd: fn "argos-translate_test", ["--from-lang", "en", "--to-lang", "fr", "blabla"], _ ->
{"yadayada", 0}
end do
ArgosTranslate.translate("blabla", nil, "fr")
end
assert {:ok, "en", "yadayada"} = translation_response
test "it translates from the to language when no language is set and returns the text unchanged" do
assert {:ok, "nl", "blabla"} = ArgosTranslate.translate("blabla", nil, "nl")
end
test "it translates from the provided language" do
test "it translates from the provided language if provided" do
translation_response =
with_mock System, [:passthrough],
cmd: fn "argos-translate_test", ["--from-lang", "nl", "--to-lang", "en", "blabla"], _ ->