Skip to content

Commit

Permalink
fix: Translate module
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Oct 1, 2023
1 parent 5ec18cd commit a75316d
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions elixir-api/lib/utils/translate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Hexerei.Translate do
Util to handle all translation methods
"""

use Hexerei.Cache.QueryCache
use Hexerei.Utils

require Logger
Expand Down Expand Up @@ -59,7 +60,7 @@ defmodule Hexerei.Translate do
def handle_translate(type, doc, lang_param) do
case lang_param do
"en" -> doc
"fr" -> Translate.translate(type, doc, "fr", "en")
"fr" -> translate(type, doc, "fr", "en")
_ -> {:error, "Invalid language"}
end
|> case do
Expand Down Expand Up @@ -319,10 +320,26 @@ defmodule Hexerei.Translate do
end

defp translate_field(text, target_lang, source_lang) do
if text == "" || text == nil do
if text == "" or text == nil do
{:ok, text}
else
construct_body(text, target_lang, source_lang) |> send_request
with cache_res <-
QueryCache.get(
"translate" <> "_" <> text <> "_" <> target_lang <> "_" <> source_lang
),
true <- cache_res != nil do
{:ok, cache_res}
else
_ ->
res = construct_body(text, target_lang, source_lang) |> send_request

QueryCache.put(
"translate" <> "_" <> text <> "_" <> target_lang <> "_" <> source_lang,
res
)

res
end
end
end

Expand Down

0 comments on commit a75316d

Please sign in to comment.