Add translation module for Argos Translate #351

Merged
floatingghost merged 4 commits from ilja/akkoma:add_translator_argos_translate into develop 2022-12-19 13:06:39 +00:00
4 changed files with 16 additions and 27 deletions
Showing only changes of commit d1bb480146 - Show all commits

View file

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

View file

@ -1119,7 +1119,7 @@ Each job has these settings:
### Translation Settings ### Translation Settings
Settings to automatically translate statuses for end users. Currently supported 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 Translations are available at `/api/v1/statuses/:id/translations/:language`, where
`language` is the target language code (e.g `en`) `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 - `:enabled` - enables translation
- `:module` - Sets module to be used - `: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` ### `:deepl`
@ -1143,9 +1143,6 @@ Translations are available at `/api/v1/statuses/:id/translations/:language`, whe
### `:argos_translate` ### `: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_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`). - `: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 it (default: `true`).
- `:strip_html` - Strip html from the post before translating the text (default: `true`).

View file

@ -11,8 +11,8 @@ defmodule Pleroma.Akkoma.Translators.ArgosTranslate do
Config.get([:argos_translate, :command_argospm]) Config.get([:argos_translate, :command_argospm])
end end
defp fallback_language do defp strip_html? do
Config.get([:argos_translate, :fallback_language]) Config.get([:argos_translate, :strip_html])
end end
defp safe_languages() do defp safe_languages() do
@ -85,23 +85,24 @@ defmodule Pleroma.Akkoma.Translators.ArgosTranslate do
defp htmlify_response(string, _), do: string defp htmlify_response(string, _), do: string
@impl Pleroma.Akkoma.Translator @impl Pleroma.Akkoma.Translator
def translate(string, from_language, to_language) do def translate(string, nil, to_language) do
strip_html = Config.get([:argos_translate, :strip_html])
# Akkoma's Pleroma-fe expects us to detect the source language automatically. # Akkoma's Pleroma-fe expects us to detect the source language automatically.
# Argos-translate doesn't have that option (yet?) # Argos-translate doesn't have that option (yet?)
# see <https://github.com/argosopentech/argos-translate/issues/9> # 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. # Afterwards people get the option to overwrite the source language from a dropdown.
from_language = from_language || fallback_language() {:ok, to_language, string}
to_language = to_language || fallback_language() end
def translate(string, from_language, to_language) do
# Argos Translate doesn't properly translate HTML (yet?) # Argos Translate doesn't properly translate HTML (yet?)
# For now we give admins the option to strip the html before translating # 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 # 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} <- with {translated, 0} <-
safe_translate(string, from_language, to_language) do 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 else
{response, _} -> {:error, "ArgosTranslate failed to translate (#{response})"} {response, _} -> {:error, "ArgosTranslate failed to translate (#{response})"}
end 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() assert [%{code: "en", name: "en"}, %{code: "nl", name: "nl"}] = dest_langs |> Enum.sort()
end end
test "it translates from default language when no language is set" do test "it translates from the to language when no language is set and returns the text unchanged" do
translation_response = assert {:ok, "nl", "blabla"} = ArgosTranslate.translate("blabla", nil, "nl")
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
end end
test "it translates from the provided language" do test "it translates from the provided language if provided" do
translation_response = translation_response =
with_mock System, [:passthrough], with_mock System, [:passthrough],
cmd: fn "argos-translate_test", ["--from-lang", "nl", "--to-lang", "en", "blabla"], _ -> cmd: fn "argos-translate_test", ["--from-lang", "nl", "--to-lang", "en", "blabla"], _ ->