Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
f9bded7957 | |||
3fa056a1a3 | |||
0743f21847 |
@ -10,7 +10,7 @@ by adding `localizator` to your list of dependencies in `mix.exs`:
|
|||||||
```elixir
|
```elixir
|
||||||
def deps do
|
def deps do
|
||||||
[
|
[
|
||||||
{:localizator, "~> 0.1.14"}
|
{:localizator, "~> 0.1.17"}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
@ -5,6 +5,17 @@ defmodule Localizator.Commons do
|
|||||||
String.match?(string, @html_regex)
|
String.match?(string, @html_regex)
|
||||||
end
|
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
|
def struct_from_map(a_map, as: a_struct) do
|
||||||
# Find the keys within the map
|
# Find the keys within the map
|
||||||
keys =
|
keys =
|
||||||
|
@ -8,6 +8,12 @@ defmodule Localizator.Translator.Microsoft do
|
|||||||
|
|
||||||
@behaviour Localizator.Translator.Base
|
@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
|
@impl true
|
||||||
@spec detect(text) :: {:ok, locale} | {:error, message}
|
@spec detect(text) :: {:ok, locale} | {:error, message}
|
||||||
def detect(text) when is_bitstring(text) do
|
def detect(text) when is_bitstring(text) do
|
||||||
@ -19,14 +25,7 @@ defmodule Localizator.Translator.Microsoft do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@spec detect(map) :: {:ok, locale} | {:error, message}
|
@spec detect(map) :: {:ok, locale} | {:error, message}
|
||||||
def detect(map) when is_map(map) do
|
def detect(map) when is_map(map), do: detect(Commons.first_non_empty_longest_string(map))
|
||||||
{: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
|
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@spec translate(text, to, optional_from) :: {:ok, text} | {:error, message}
|
@spec translate(text, to, optional_from) :: {:ok, text} | {:error, message}
|
||||||
|
@ -51,6 +51,9 @@ defmodule Localizator.Translator do
|
|||||||
translate(source, map.to, map.from, translator)
|
translate(source, map.to, map.from, translator)
|
||||||
end
|
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
|
@spec translate(String.t(), to, from_may_be_nil, translator) :: result
|
||||||
def translate(string, to, from, translator) when is_binary(string) do
|
def translate(string, to, from, translator) when is_binary(string) do
|
||||||
case Commons.is_html?(string) do
|
case Commons.is_html?(string) do
|
||||||
|
@ -8,6 +8,12 @@ defmodule Localizator.Translator.Yandex do
|
|||||||
|
|
||||||
@behaviour Localizator.Translator.Base
|
@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
|
@impl true
|
||||||
@spec detect(text) :: {:ok, locale} | {:error, message}
|
@spec detect(text) :: {:ok, locale} | {:error, message}
|
||||||
def detect(text) when is_bitstring(text) do
|
def detect(text) when is_bitstring(text) do
|
||||||
@ -19,14 +25,7 @@ defmodule Localizator.Translator.Yandex do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@spec detect(map) :: {:ok, locale} | {:error, message}
|
@spec detect(map) :: {:ok, locale} | {:error, message}
|
||||||
def detect(map) when is_map(map) do
|
def detect(map) when is_map(map), do: detect(Commons.first_non_empty_longest_string(map))
|
||||||
{: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
|
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@spec translate(text, to, optional_from) :: {:ok, text} | {:error, message}
|
@spec translate(text, to, optional_from) :: {:ok, text} | {:error, message}
|
||||||
|
2
mix.exs
2
mix.exs
@ -4,7 +4,7 @@ defmodule Localizator.MixProject do
|
|||||||
def project do
|
def project do
|
||||||
[
|
[
|
||||||
app: :localizator,
|
app: :localizator,
|
||||||
version: "0.1.14",
|
version: "0.1.17",
|
||||||
elixir: "~> 1.10",
|
elixir: "~> 1.10",
|
||||||
start_permanent: Mix.env() == :prod,
|
start_permanent: Mix.env() == :prod,
|
||||||
deps: deps()
|
deps: deps()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user