Merge pull request 'Return empty string in the event of no detected language' (#535) from midnight/akkoma:fix-libretranslate into develop
Some checks are pending
ci/woodpecker/push/woodpecker Pipeline is pending

Reviewed-on: #535
This commit is contained in:
floatingghost 2023-05-22 21:30:51 +00:00
commit 3437e11cf7
2 changed files with 19 additions and 3 deletions

View file

@ -39,9 +39,9 @@ def translate(string, from_language, to_language) do
detected = detected =
if Map.has_key?(body, "detectedLanguage") do if Map.has_key?(body, "detectedLanguage") do
get_in(body, ["detectedLanguage", "language"]) get_in(body, ["detectedLanguage", "language"])
else else
from_language from_language || ""
end end
{:ok, detected, translated} {:ok, detected, translated}
else else

View file

@ -133,5 +133,21 @@ test "should gracefully handle an unsupported language" do
assert {:error, "libre_translate: request failed (code 400)"} = assert {:error, "libre_translate: request failed (code 400)"} =
LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "zoop") LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "zoop")
end end
test "should work when no detected language is received" do
Tesla.Mock.mock(fn
%{method: :post, url: "http://libre.translate/translate"} ->
%Tesla.Env{
status: 200,
body:
Jason.encode!(%{
translatedText: "I will crush you"
})
}
end)
assert {:ok, "", "I will crush you"} =
LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
end
end end
end end