Skip to content

Commit

Permalink
fix: Translate proj/post headings
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Sep 6, 2023
1 parent 1b118c6 commit 77ba611
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
43 changes: 21 additions & 22 deletions elixir-api/lib/routes/api/v1/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,32 @@ defmodule Router.Api.V1.Post do

conn
|> handle_sanity_fetch(query, fn conn, result, duration ->
{result, count} =
count =
case result["result"] do
nil ->
{result, 0}

_ ->
%{"result" => %{"body" => body}} = result
nil -> 0
_ -> 1
end

{
Kernel.put_in(result, ["result", "headings"], PT.build_summary(body)),
1
}
translated_result =
case params["lang"] do
"en" -> result
"fr" -> Translate.translate(:post, result, "fr", "en")
_ -> conn |> error_res(400, "Invalid request", "Invalid language") |> halt()
end
|> case do
{:error, message} ->
Logger.error("Error translating post: #{inspect(message)}")
result

case params["lang"] do
"en" -> result
"fr" -> Translate.translate(:post, result, "fr", "en")
_ -> conn |> error_res(400, "Invalid request", "Invalid language") |> halt()
end
|> case do
{:error, message} ->
Logger.error("Error translating post: #{inspect(message)}")
result
translated_result ->
translated_result
end

translated_result ->
translated_result
end
Kernel.put_in(
translated_result,
["result", "headings"],
PT.build_summary(translated_result["result"]["body"])
)
|> Map.put("meta", %{
"total" => count,
"count" => count,
Expand Down
15 changes: 4 additions & 11 deletions elixir-api/lib/routes/api/v1/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,10 @@ defmodule Router.Api.V1.Project do

conn
|> handle_sanity_fetch(query, fn conn, result, duration ->
{result, count} =
count =
case result["result"] do
nil ->
{result, 0}

_ ->
%{"result" => %{"body" => body}} = result

{
Kernel.put_in(result, ["result", "headings"], PT.build_summary(body)),
1
}
nil -> 0
_ -> 1
end

case params["lang"] do
Expand All @@ -97,6 +89,7 @@ defmodule Router.Api.V1.Project do
translated_result ->
translated_result
end
|> Kernel.put_in(["result", "headings"], PT.build_summary(& &1["result"]["body"]))
|> Map.put("meta", %{
"total" => count,
"count" => count,
Expand Down
2 changes: 1 addition & 1 deletion elixir-api/lib/utils/translate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ defmodule Hexerei.Translate do
case child do
# Check if the child is a text node and it's not marked w/ "notranslate"
%{"text" => text, "marks" => marks} when text != nil ->
if Enum.member?(marks || [], "notranslate") do
if Enum.member?(marks || [], "notranslate") or Enum.member?(marks || [], "code") do
child
else
translated =
Expand Down

0 comments on commit 77ba611

Please sign in to comment.