From 7687662123288045e8580d814218cf5b3d2c9652 Mon Sep 17 00:00:00 2001 From: NancyAanchal Date: Thu, 15 Aug 2024 21:38:55 +0545 Subject: [PATCH 1/2] implement a caching system --- nepalingo-web/src/lib/getGTranslate.tsx | 45 +++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/nepalingo-web/src/lib/getGTranslate.tsx b/nepalingo-web/src/lib/getGTranslate.tsx index b9d4cb9..31c5157 100644 --- a/nepalingo-web/src/lib/getGTranslate.tsx +++ b/nepalingo-web/src/lib/getGTranslate.tsx @@ -1,3 +1,5 @@ +import { supabaseClient } from "@/config/supabase-client"; + const GOOGLE_TRANSLATE_API_KEY = import.meta.env.VITE_GOOGLE_TRANSLATE_API_KEY; const languageCodes: { [key: string]: string } = { @@ -6,7 +8,7 @@ const languageCodes: { [key: string]: string } = { export const getGTranslate = async ( language: string, - word: string, + word: string ): Promise<{ language: string; word: string; @@ -21,8 +23,34 @@ export const getGTranslate = async ( throw new Error(`Language code for ${language} not found`); } - const url = `https://translation.googleapis.com/language/translate/v2?key=${GOOGLE_TRANSLATE_API_KEY}&q=${word}&target=${targetLanguage}`; + // Check if the translation is already cached in Supabase + const { data: cachedResult, error } = await supabaseClient + .from("translations_cache") + .select("translated_word") + .eq("language", language) + .eq("word", word) + .single(); + if (error) { + console.error("Error fetching from cache:", error); + } else if (cachedResult) { + const translatedWord = cachedResult.translated_word; + console.log("used table"); + return { + language, + word, + meanings: [ + { + language, + meaningOriginal: translatedWord, + meaningEn: word, + }, + ], + }; + } + + const url = `https://translation.googleapis.com/language/translate/v2?key=${GOOGLE_TRANSLATE_API_KEY}&q=${word}&target=${targetLanguage}`; + console.log("used api"); const response = await fetch(url); const data = await response.json(); @@ -30,13 +58,24 @@ export const getGTranslate = async ( throw new Error(data.error.message); } + const translatedWord = data.data.translations[0].translatedText; + + // Store the result in the Supabase cache + const { error: insertError } = await supabaseClient + .from("translations_cache") + .insert([{ language, word, translated_word: translatedWord }]); + + if (insertError) { + console.error("Error inserting into cache:", insertError); + } + return { language, word, meanings: [ { language, - meaningOriginal: data.data.translations[0].translatedText, + meaningOriginal: translatedWord, meaningEn: word, }, ], From 9a928cd12350dea1b827230819549418b378c2ea Mon Sep 17 00:00:00 2001 From: NancyAanchal Date: Thu, 15 Aug 2024 23:20:36 +0545 Subject: [PATCH 2/2] succesfully made it cache data for all g-translate languages --- nepalingo-web/src/lib/getGTranslate.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/nepalingo-web/src/lib/getGTranslate.tsx b/nepalingo-web/src/lib/getGTranslate.tsx index 201ab96..90d588f 100644 --- a/nepalingo-web/src/lib/getGTranslate.tsx +++ b/nepalingo-web/src/lib/getGTranslate.tsx @@ -10,7 +10,7 @@ const languageCodes: { [key: string]: string } = { export const getGTranslate = async ( language: string, - word: string + word: string, ): Promise<{ language: string; word: string; @@ -37,7 +37,6 @@ export const getGTranslate = async ( console.error("Error fetching from cache:", error); } else if (cachedResult) { const translatedWord = cachedResult.translated_word; - console.log("used table"); return { language, word, @@ -52,7 +51,6 @@ export const getGTranslate = async ( } const url = `https://translation.googleapis.com/language/translate/v2?key=${GOOGLE_TRANSLATE_API_KEY}&q=${word}&target=${targetLanguage}`; - console.log("used api"); const response = await fetch(url); const data = await response.json();