Skip to content

Commit

Permalink
Update Translator module
Browse files Browse the repository at this point in the history
The translator module does not require to be passed the name of the
translation container field. The test have also been updated to reflect
this change.
  • Loading branch information
crbelaus committed Mar 31, 2017
1 parent a682ec1 commit 69285e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 13 additions & 3 deletions lib/trans/translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ defmodule Trans.Translator do
"Cómo escribir un corrector ortográfico"
"""
def translate(struct, locale, field, opts \\ []) when is_map(struct) do
translation_container = opts[:container] || :translations
translated_field = with {:ok, all_translations} <- Map.fetch(struct, translation_container),
def translate(struct, locale, field) when is_map(struct) do
# Check if the struct is a map or an actual struct.
# If it is a struct, check if it uses Trans and if the field is among the translatable fields
# Look for `translations` and `"translations"
raise_if_untranslatable(struct, field)
translated_field = with {:ok, all_translations} <- Map.fetch(struct, translation_container(struct)),
{:ok, translations_for_locale} <- Map.fetch(all_translations, to_string(locale)),
{:ok, translated_field} <- Map.fetch(translations_for_locale, to_string(field)),
do: translated_field
Expand All @@ -114,5 +117,12 @@ defmodule Trans.Translator do
end
end

defp translation_container(%{translations: _}), do: :translations
defp translation_container(%{"translations" => _}), do: "translations"
defp translation_container(_) do
raise ArgumentError, message: "Translation container not found. You must define a 'translations' field into your map or struct so it can be used by Trans."
end

defp raise_if_untranslatable(_struct, _field), do: nil

end
14 changes: 10 additions & 4 deletions test/translator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@ defmodule TranslatorTest do

test "retrieve translation for existing attribute" do
article = build(:article)
fr_body = Translator.translate(article, :fr, :body, container: :test_translation_container)
assert fr_body == article.test_translation_container["fr"]["body"]
fr_body = Translator.translate(article, :fr, :body)
assert fr_body == article.translations["fr"]["body"]
end

test "fallback to default value when no translation available" do
article = build(:article)
# Since we don't have a "de" translation, it will return the default value'
body = Translator.translate(article, :de, :body, container: :test_translation_container)
body = Translator.translate(article, :de, :body)
assert body == article.body
end

test "raise error wen translating an untraslatable attribute" do
article = build(:article)
assert_raise KeyError, fn ->
Translator.translate(article, :es, :fake_attr, container: :test_translation_container)
Translator.translate(article, :es, :fake_attr)
end
end

test "raise error when no translation container" do
assert_raise ArgumentError, fn ->
Translator.translate(%{}, :es, :fake_attr)
end
end
end

0 comments on commit 69285e0

Please sign in to comment.