microsoft_translator/lib/microsoft_translator.ex

67 lines
2.2 KiB
Elixir
Raw Normal View History

2020-06-19 08:45:38 +03:00
defmodule MicrosoftTranslator do
@moduledoc """
Basic functions for requests to Yandex Translate API on Yandex Cloud
"""
alias MicrosoftTranslator.Client
@doc """
Retrieves the list of supported languages.
Return a map with language code (use it for translations) and native language name
```elixir
%{
languages: [
%{code: "af", "name":"Afrikaans","nativeName":"Afrikaans"},
%{code: "ar", "name":"Arabic","nativeName":"العربية"},
%{code: "bg", "name":"Bulgarian","nativeName":"Български"},
%{code: "bn", "name":"Bangla","nativeName":"বাংলা"},
%{code: "bs", "name":"Bosnian","nativeName":"bosanski (latinica)"}
...
]
}
```
"""
def languages(), do: Client.call(:languages, %{})
@doc """
Detect the language of the text
Get text as a string param and return a map with language code
```elixir
MicrosoftTranslator.detect("Криївка")
# Response
%{languageCode: "uk"}
```
Or get a map with :text and :languageCodeHints (for specify the most likely languages).
> In some languages, one and the same word has the same spelling. For example, the English word hand is also written as hand in German, Swedish, and Dutch. If the text you transmit contains words like this, Translate may detect the source language incorrectly.
To avoid mistakes, you can use the languageCodeHints field to specify which languages should be given priority when determining the language of the text
```elixir
MicrosoftTranslator.detect(%{text: "Капелюх"})
# Response
%{languageCode: "uk"}
```
"""
def detect(params) when is_map(params), do: Client.call(:detect, params)
def detect(text) when is_binary(text), do: Client.call(:detect, %{text: text})
def translate(params) when is_map(params), do: Client.call(:translate, params)
def translate(text, to) when is_binary(text) and is_binary(to),
do: Client.call(:translate, %{text: text, to: to})
def translate(text, to, from)
when is_binary(text) and is_binary(to) and is_binary(from),
do:
Client.call(:translate, %{
text: text,
to: to,
from: from
})
end