Compare commits

...

7 Commits

7 changed files with 35 additions and 20 deletions

View File

@ -10,7 +10,7 @@ by adding `localizator` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:localizator, "~> 0.1.10"}
{:localizator, "~> 0.1.17"}
]
end
```

View File

@ -5,6 +5,17 @@ defmodule Localizator.Commons do
String.match?(string, @html_regex)
end
def first_non_empty_longest_string(map) when is_map(map) do
map
|> Map.values()
|> Enum.reject(&is_nil(&1))
|> Enum.filter(&is_binary(&1))
|> Enum.map(&String.trim(&1))
|> Enum.reject(&(&1 == ""))
|> Enum.sort(&(String.length(&1) >= String.length(&2)))
|> List.first()
end
def struct_from_map(a_map, as: a_struct) do
# Find the keys within the map
keys =

View File

@ -8,6 +8,12 @@ defmodule Localizator.Translator.Microsoft do
@behaviour Localizator.Translator.Base
alias Localizator.Commons
@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
@ -19,14 +25,7 @@ defmodule Localizator.Translator.Microsoft do
@impl true
@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
end
def detect(map) when is_map(map), do: detect(Commons.first_non_empty_longest_string(map))
@impl true
@spec translate(text, to, optional_from) :: {:ok, text} | {:error, message}
@ -45,6 +44,9 @@ defmodule Localizator.Translator.Microsoft do
|> List.first()
|> Map.fetch(:text)
%{error: %{message: message}} ->
{:error, message}
%{message: message} ->
{:error, message}
end

View File

@ -51,6 +51,9 @@ defmodule Localizator.Translator do
translate(source, map.to, map.from, translator)
end
@spec translate(nil, to, from_may_be_nil, translator) :: result
def translate(empty, _to, _from, _translator) when is_nil(empty), do: nil
@spec translate(String.t(), to, from_may_be_nil, translator) :: result
def translate(string, to, from, translator) when is_binary(string) do
case Commons.is_html?(string) do

View File

@ -8,6 +8,12 @@ defmodule Localizator.Translator.Yandex do
@behaviour Localizator.Translator.Base
alias Localizator.Commons
@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
@ -19,14 +25,7 @@ defmodule Localizator.Translator.Yandex do
@impl true
@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
end
def detect(map) when is_map(map), do: detect(Commons.first_non_empty_longest_string(map))
@impl true
@spec translate(text, to, optional_from) :: {:ok, text} | {:error, message}

View File

@ -4,7 +4,7 @@ defmodule Localizator.MixProject do
def project do
[
app: :localizator,
version: "0.1.10",
version: "0.1.17",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps()
@ -22,7 +22,7 @@ defmodule Localizator.MixProject do
defp deps do
[
{:yandex_translate, "~> 0.4.1"},
{:microsoft_translator, "~> 0.1.4"},
{:microsoft_translator, "~> 0.1.5"},
# Parsers/generators
{:meeseeks, "~> 0.15.1"},

View File

@ -19,7 +19,7 @@
"meeseeks": {:hex, :meeseeks, "0.15.1", "148d5d9ea879cdb415b8bc4162ac5528f9a2fe42fbfe1802c681a2842cb1c0a4", [:mix], [{:meeseeks_html5ever, "~> 0.12.1", [hex: :meeseeks_html5ever, repo: "hexpm", optional: false]}], "hexpm", "5589957b7cca75e6683cecc308253d7854f43b07806939d7031b81ca6e8abd98"},
"meeseeks_html5ever": {:hex, :meeseeks_html5ever, "0.12.1", "718fab10d05b83204524a518b2b88caa37ba6a6e02f82e80d6a7bc47552fb54a", [:mix], [{:rustler, "~> 0.21.0", [hex: :rustler, repo: "hexpm", optional: false]}], "hexpm", "11489094637f49a26bad4610a9138352c8d229339d888169cb35b08cdfd8861a"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"microsoft_translator": {:hex, :microsoft_translator, "0.1.3", "5cf930aebf3e891bed6c4cc8e68ccf670a8a488cd4e046389150c7ff85ba4320", [:mix], [{:castore, "~> 0.1.6", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.2.0", [hex: :jason, repo: "hexpm", optional: false]}, {:joken, "~> 2.2.0", [hex: :joken, repo: "hexpm", optional: false]}, {:jose, "~> 1.10.1", [hex: :jose, repo: "hexpm", optional: false]}, {:mojito, "~> 0.7.3", [hex: :mojito, repo: "hexpm", optional: false]}], "hexpm", "04684fe36b63715c041e6057beecfe9127e3f62af112fa6342fd43d3ef3f0641"},
"microsoft_translator": {:hex, :microsoft_translator, "0.1.5", "15774b7d28a3174669283f455b2cac4f404dafab11dd2da227828d4444dcce90", [:mix], [{:castore, "~> 0.1.6", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.2.0", [hex: :jason, repo: "hexpm", optional: false]}, {:joken, "~> 2.2.0", [hex: :joken, repo: "hexpm", optional: false]}, {:jose, "~> 1.10.1", [hex: :jose, repo: "hexpm", optional: false]}, {:mojito, "~> 0.7.3", [hex: :mojito, repo: "hexpm", optional: false]}], "hexpm", "fb366a7a49ee69eed93cd2f03e8312186a79365a9c34c99cd43681c86f134df7"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mint": {:hex, :mint, "1.1.0", "1fd0189edd9e3ffdbd7fcd8bc3835902b987a63ec6c4fd1aa8c2a56e2165f252", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bfd316c3789340b682d5679a8116bcf2112e332447bdc20c1d62909ee45f48d"},
"mojito": {:hex, :mojito, "0.7.3", "7356f3b7697d79520a243b48cf0bf8bd1152b2e9cdb6ff7cf22cd0769f32dd40", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}], "hexpm", "433479d8ef1c882fafe864ac6d7b08249f321fc46bdcc8db78691bc1ddcf234a"},