Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added functionality to translate a whole struct #67

Merged
merged 2 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions lib/trans/translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,74 @@ defmodule Trans.Translator do
translated_field
end
end

@doc """
Translates the whole struct with all translatable values and translatable associations to the given locale

## Usage example
Similar to `translate/3` but returns the whole struct

We can get the Spanish version like this:

iex> Trans.Translator.translate(article, :es)
...> %Article{
...> title: "Cómo escribir un corrector ortográfico",
...> body: "Un artículo maravilloso de Peter Norvig",
...> translations: %{
...> "es" => %{
...> title: "Cómo escribir un corrector ortográfico",
...> body: "Un artículo maravilloso de Peter Norvig"
...> },
...> "fr" => %{
...> title: "Comment écrire un correcteur orthographique",
...> body: "Un merveilleux article de Peter Norvig"
...> }
...> }
...> }

"""
@spec translate(struct, String.t() | atom) :: struct
def translate(%{__struct__: module} = struct, locale)
when is_binary(locale) or is_atom(locale) do
if Keyword.has_key?(module.__info__(:functions), :__trans__) do
struct
|> translate_fields(locale)
|> translate_assocs(locale)
else
struct
end
end

def translate(struct, _locale), do: struct

defp translate_fields(%{__struct__: module} = struct, locale) do
module.__trans__(:fields)
|> Enum.reduce(struct, fn field, struct ->
PhilWaldmann marked this conversation as resolved.
Show resolved Hide resolved
case translated_field(struct, locale, field) do
:error -> struct
translation -> Map.put(struct, field, translation)
end
end)
end

defp translate_assocs(%{__struct__: module} = struct, locale) do
associations = module.__schema__(:associations)
embeds = module.__schema__(:embeds)

Enum.reduce(associations ++ embeds, struct, fn assoc_name, struct ->
Map.update(struct, assoc_name, nil, fn
%Ecto.Association.NotLoaded{} = item ->
item

items when is_list(items) ->
Enum.map(items, &translate(&1, locale))

%{} = item ->
translate(item, locale)

item ->
item
end)
end)
end
end
13 changes: 13 additions & 0 deletions test/translator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ defmodule TranslatorTest do
Translator.translate(build(:article), :fake_attr, :es)
end
end

test "translates the whole struct at once" do
article = build(:article)
fr_article = Translator.translate(article, :fr)
assert fr_article.title == article.translations["fr"]["title"]
assert fr_article.body == article.translations["fr"]["body"]
end

test "translates the nested structs" do
%{comments: [comment | _]} = article = build(:article)
%{comments: [fr_comment | _]} = Translator.translate(article, :fr)
assert fr_comment.comment == comment.transcriptions["fr"]["comment"]
end
end