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 73 additions and 8 deletions
Showing only changes of commit f78bada08d - Show all commits

View file

@ -885,7 +885,8 @@
config :pleroma, :argos_translate,
command_argos_translate: "argos-translate",
command_argospm: "argospm",
default_language: "en"
fallback_language: "en",
strip_html: true
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.

View file

@ -1143,6 +1143,9 @@ 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`).
- `:default_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`).
- `: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`).

View file

@ -11,8 +11,8 @@ defp argospm do
Config.get([:argos_translate, :command_argospm])
end
defp default_language do
Config.get([:argos_translate, :default_language])
defp fallback_language do
Config.get([:argos_translate, :fallback_language])
end
defp safe_languages() do
@ -63,19 +63,45 @@ defp safe_translate(string, from_language, to_language) do
end
end
defp clean_string(string, true) do
string
|> String.replace("<p>", "\n")
|> String.replace("</p>", "\n")
|> String.replace("<br>", "\n")
|> String.replace("<br/>", "\n")
|> String.replace("<li>", "\n")
|> Pleroma.HTML.strip_tags()
|> HtmlEntities.decode()
end
defp clean_string(string, _), do: string
defp htmlify_response(string, true) do
string
|> HtmlEntities.encode()
|> String.replace("\n", "<br/>")
end
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])
# 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 default source language from settings.
# For now we choose a fallback source language from settings.
# Afterwards people get the option to overwrite the source language from a dropdown.
from_language = from_language || default_language()
to_language = to_language || default_language()
from_language = from_language || fallback_language()
to_language = to_language || fallback_language()
# 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)
with {translated, 0} <-
safe_translate(string, from_language, to_language) do
{:ok, from_language, translated}
{:ok, from_language, translated |> htmlify_response(strip_html)}
else
{response, _} -> {:error, "ArgosTranslate failed to translate (#{response})"}
end

View file

@ -63,4 +63,39 @@ test "it returns a proper error when the executable can't be found" do
assert {:error, "ArgosTranslate failed to translate" <> _} =
ArgosTranslate.translate("blabla", "nl", "en")
end
test "it can strip html" do
content =
~s[<p>What&#39;s up my fellow fedizens?</p><p>So anyway</p><ul><li><a class="hashtag" data-tag="cofe" href="https://suya.space/tag/cofe">#cofe</a></li><li><a class="hashtag" data-tag="suya" href="https://cofe.space/tag/suya">#Suya</a></li></ul><p>ammiright!<br/>:ablobfoxhyper:</p>]
stripped_content =
"\nWhat's up my fellow fedizens?\n\nSo anyway\n\n#cofe\n#Suya\nammiright!\n:ablobfoxhyper:\n"
expected_response_strip_html =
"<br/>What&#39;s up my fellow fedizens?<br/><br/>So anyway<br/><br/>#cofe<br/>#Suya<br/>ammiright!<br/>:ablobfoxhyper:<br/>"
response_strip_html =
with_mock System, [:passthrough],
cmd: fn "argos-translate_test",
["--from-lang", _, "--to-lang", _, ^stripped_content],
_ ->
{stripped_content, 0}
end do
ArgosTranslate.translate(content, "nl", "en")
end
clear_config([:argos_translate, :strip_html], false)
response_no_strip_html =
with_mock System, [:passthrough],
cmd: fn "argos-translate_test", ["--from-lang", _, "--to-lang", _, string], _ ->
{string, 0}
end do
ArgosTranslate.translate(content, "nl", "en")
end
assert {:ok, "nl", content} == response_no_strip_html
assert {:ok, "nl", expected_response_strip_html} == response_strip_html
end
end