From a75316d0d440d686c76892f95dd655c1e3b02842 Mon Sep 17 00:00:00 2001 From: kiosion Date: Sat, 30 Sep 2023 23:29:48 -0300 Subject: [PATCH] fix: Translate module --- elixir-api/lib/utils/translate.ex | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/elixir-api/lib/utils/translate.ex b/elixir-api/lib/utils/translate.ex index 8ad9217ab..abcb853f6 100644 --- a/elixir-api/lib/utils/translate.ex +++ b/elixir-api/lib/utils/translate.ex @@ -3,6 +3,7 @@ defmodule Hexerei.Translate do Util to handle all translation methods """ + use Hexerei.Cache.QueryCache use Hexerei.Utils require Logger @@ -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 @@ -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