localizator/lib/translator/translator.ex
2020-06-07 17:00:56 +03:00

48 lines
1.1 KiB
Elixir

defmodule Localizator.Translator do
alias Localizator.Translator.Direction
@typedoc """
Locale
"""
@type locale :: String.t()
@type from :: locale
@type from_may_be_nil :: from | nil
@type to :: locale
@type direction :: {from, to} | to | %{from: from, to: to} | %{to: to}
@type direction_map :: %{to: to, from: from_may_be_nil}
@typedoc """
Translation service
"""
@type translator :: Atom.t()
@typedoc """
Translation services list
"""
@type translators :: [translator]
@typedoc """
Translation resource
"""
@type resource :: String.t() | Map.t() | List.t()
@type source :: resource
@type result :: resource
@typedoc """
Error Message
"""
@type message :: String.t()
@spec list() :: translators
def list(), do: Application.get_env(:localizator, :translators)
@spec default() :: translator
def default(), do: list() |> List.first()
@spec translate(source, direction, translator) :: {:ok, result} | {:error, message}
def translate(source, direction, translator \\ default()) do
map = Direction.get(direction)
translator.translate(source, map.to, map.from)
end
end