detect(nil) return error

This commit is contained in:
2020-07-06 17:21:23 +03:00
parent 0743f21847
commit 3fa056a1a3
4 changed files with 12 additions and 12 deletions

View File

@@ -8,6 +8,10 @@ defmodule Localizator.Translator.Microsoft do
@behaviour Localizator.Translator.Base
@impl true
@spec detect(nil) :: {:error, message}
def detect(nil), do: {:error, "Couldn't detect language"}
@impl true
@spec detect(text) :: {:ok, locale} | {:error, message}
def detect(text) when is_bitstring(text) do
@@ -21,11 +25,7 @@ defmodule Localizator.Translator.Microsoft do
@spec detect(map) :: {:ok, locale} | {:error, message}
def detect(map) when is_map(map) do
{:ok, sample} = Map.fetch(map, List.first(Map.keys(map)))
case MicrosoftTranslator.detect(sample) do
%{languageCode: locale} -> {:ok, locale}
%{} -> {:error, "Couldn't detect language"}
end
detect(sample)
end
@impl true

View File

@@ -8,6 +8,10 @@ defmodule Localizator.Translator.Yandex do
@behaviour Localizator.Translator.Base
@impl true
@spec detect(nil) :: {:error, message}
def detect(nil), do: {:error, "Couldn't detect language"}
@impl true
@spec detect(text) :: {:ok, locale} | {:error, message}
def detect(text) when is_bitstring(text) do
@@ -21,11 +25,7 @@ defmodule Localizator.Translator.Yandex do
@spec detect(map) :: {:ok, locale} | {:error, message}
def detect(map) when is_map(map) do
{:ok, sample} = Map.fetch(map, List.first(Map.keys(map)))
case YandexTranslate.detect(sample) do
%{languageCode: locale} -> {:ok, locale}
%{} -> {:error, "Couldn't detect language"}
end
detect(sample)
end
@impl true