Skip to content

Commit

Permalink
Translator returns an empty string if an empty map or nil is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuprog committed Nov 2, 2019
1 parent a87e661 commit 6377cae
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ Add `i18n_helpers` for Elixir as a dependency in your `mix.exs` file:
```elixir
def deps do
[
{:i18n_helpers, "~> 0.7.1"}
{:i18n_helpers, "~> 0.8.0"}
]
end
```
Expand Down
6 changes: 4 additions & 2 deletions lib/ecto/translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule I18nHelpers.Ecto.Translator do
4. Repeat step 1. for each associated Ecto struct.
"""
@spec translate(list | struct | map, String.t() | atom, keyword) ::
list | struct | String.t() | nil
list | struct | String.t()
def translate(data_structure, locale \\ Gettext.get_locale(), opts \\ [])

def translate([], _locale, _opts), do: []
Expand Down Expand Up @@ -62,7 +62,9 @@ defmodule I18nHelpers.Ecto.Translator do
entity
end

def translate(map, _locale, _opts) when map == %{}, do: nil
def translate(nil, _locale, _opts), do: ""

def translate(map, _locale, _opts) when map == %{}, do: ""

def translate(%{} = translations_map, locale, opts) do
locale = to_string(locale)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule I18nHelpers.MixProject do
use Mix.Project

@version "0.7.1"
@version "0.8.0"

def project do
[
Expand Down
7 changes: 4 additions & 3 deletions test/ecto/translator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ defmodule I18nHelpers.Ecto.TranslatorTest do
end
end

test "get translation from empty map" do
assert Translator.translate(%{}) == nil
assert Translator.translate(%{}, "fr") == nil
test "get translation from empty map or nil" do
assert Translator.translate(%{}) == ""
assert Translator.translate(%{}, "fr") == ""
assert Translator.translate(nil) == ""
end

test "get translation from map, given key is present" do
Expand Down

0 comments on commit 6377cae

Please sign in to comment.