Packages

Translate word and phrases using the Microsoft Translator API. See README.md for information.

Current section

Files

Jump to
microsoft_translator lib microsoft_translator.ex
Raw

lib/microsoft_translator.ex

defmodule MicrosoftTranslator do
@moduledoc """
Basic functions for requests to Microsoft Translator API on Microsoft Azure
"""
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
MicrosoftTranslator.detect()
%{
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("Криївка")
%{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: "Капелюх"})
%{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