Skip to content

Commit

Permalink
Translator.translate/2 should fall back to default locale when transl…
Browse files Browse the repository at this point in the history
…ation is missing (#84)
  • Loading branch information
krns authored Jul 7, 2024
1 parent 3070662 commit 22a54ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/trans/translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ defmodule Trans.Translator do
end
end

defp translate_fields(%{__struct__: module} = struct, locale, default_locale) do
defp translate_fields(%{__struct__: module} = struct, locale, default_locale) when is_list(locale) do
fields = module.__trans__(:fields)

Enum.reduce(fields, struct, fn field, struct ->
Expand All @@ -193,6 +193,10 @@ defmodule Trans.Translator do
end)
end

defp translate_fields(%{__struct__: module} = struct, locale, default_locale) do

Check warning on line 196 in lib/trans/translator.ex

View workflow job for this annotation

GitHub Actions / test (1.15.0, 25.3)

variable "module" is unused (if the variable is not meant to be used, prefix it with an underscore)
translate_fields(struct, [locale], default_locale)
end

defp translate_assocs(%{__struct__: module} = struct, locale) do
associations = module.__schema__(:associations)
embeds = module.__schema__(:embeds)
Expand Down
6 changes: 6 additions & 0 deletions test/trans/translator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ defmodule Trans.TranslatorTest do

assert translated.content == struct.content
end

test "falls back to the default locale if translation does not exist", %{struct: struct} do
translated = Translator.translate(struct, :fr)

assert translated.content == struct.content
end
end

describe inspect(&Translator.translate/3) do
Expand Down

0 comments on commit 22a54ad

Please sign in to comment.