Skip to content

Commit

Permalink
fix: Guard against nil PT block values when translating
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Jul 19, 2024
1 parent 24ea6a2 commit 1de3bbd
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions elixir-api/lib/utils/translate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ defmodule Hexerei.Translate do
translated_blocks

# {:error, reason} ->
# Logger.error("Error translating: #{reason}")
# Logger.error("Error translating post blocks: #{reason}")
# text_blocks
end

Expand All @@ -275,7 +275,7 @@ defmodule Hexerei.Translate do

updated_docs =
Enum.map(documents, fn document ->
translate_post(%{"result" => document}, target_lang, source_lang, true)
translate_post(%{"result" => document}, target_lang, source_lang)
end)

{left, right} =
Expand Down Expand Up @@ -430,29 +430,35 @@ defmodule Hexerei.Translate do
updated_fields =
pt_blocks
|> Enum.map(fn {key, value} ->
text_array = get_text_from_blocks(value)
case value do
nil ->
{key, nil}

_ ->
text_array = get_text_from_blocks(value)

translations =
text_array
|> Task.async_stream(&translate_field(&1, target_lang, source_lang),
max_concurrency: Enum.count(text_array)
)
|> Enum.to_list()
|> Enum.map(fn
{:ok, {:ok, result}} ->
result
translations =
text_array
|> Task.async_stream(&translate_field(&1, target_lang, source_lang),
max_concurrency: Enum.count(text_array)
)
|> Enum.to_list()
|> Enum.map(fn
{:ok, {:ok, result}} ->
result

{:ok, {:error, _}} ->
{:error, "Error translating block"}
{:ok, {:error, _}} ->
{:error, "Error translating block"}

{:exit, reason} ->
Logger.error("Error translating block: #{inspect(reason)}")
{:error, "Error translating block"}
end)
{:exit, reason} ->
Logger.error("Error translating block: #{inspect(reason)}")
{:error, "Error translating block"}
end)

updated_blocks = replace_text_in_children(value, text_array, translations)
updated_blocks = replace_text_in_children(value, text_array, translations)

{key, updated_blocks}
{key, updated_blocks}
end
end)

{:ok, updated_fields |> Enum.into(%{})}
Expand Down

0 comments on commit 1de3bbd

Please sign in to comment.