From f3148c68b420e51b997cd4b6e2f09642e4e3646b Mon Sep 17 00:00:00 2001 From: NancyAanchal Date: Wed, 7 Aug 2024 14:37:54 +0545 Subject: [PATCH] debug aggaaainnn --- nepalingo-web/src/hooks/useDictionary.tsx | 17 +++++++++++--- nepalingo-web/src/lib/getGTranslate.tsx | 27 ++++++++++++----------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/nepalingo-web/src/hooks/useDictionary.tsx b/nepalingo-web/src/hooks/useDictionary.tsx index 3348c9d..dd310c7 100644 --- a/nepalingo-web/src/hooks/useDictionary.tsx +++ b/nepalingo-web/src/hooks/useDictionary.tsx @@ -45,7 +45,8 @@ async function getFetcherByLanguage( newariResult = await getNewariWord(word); if (newariResult.meanings.length === 0) { - return await getGTranslate("new", word); + console.log("Used Google Translate for newari"); + return await getGTranslate("newari", word); // return { // language, // word, @@ -63,8 +64,18 @@ async function getFetcherByLanguage( case "Tajpuriya": return await getTajpuriyaWord(word); case "Maithili": - return await getGTranslate("mai", word); - + return await getGTranslate("maithili", word); + // return { + // language, + // word, + // meanings: [ + // { + // language, + // meaningOriginal: maithiliResult, + // meaningEn: word, + // }, + // ], + // }; default: throw new Error(`Language ${language} not supported`); } diff --git a/nepalingo-web/src/lib/getGTranslate.tsx b/nepalingo-web/src/lib/getGTranslate.tsx index b616afc..b51ed56 100644 --- a/nepalingo-web/src/lib/getGTranslate.tsx +++ b/nepalingo-web/src/lib/getGTranslate.tsx @@ -1,19 +1,28 @@ const GOOGLE_TRANSLATE_API_KEY = "AIzaSyCi239eredIAEEJkI8xzvGwW1GPS7B1vxQ"; -let language: string; + +const languageCodes: { [key: string]: string } = { + maithili: "mai", + newari: "new", +}; export const getGTranslate = async ( - targetLanguage: string, + language: string, word: string ): Promise<{ language: string; word: string; - meanings: { + meanings: Array<{ language: string; meaningOriginal: string; meaningEn: string; - }[]; + }>; }> => { - const url = `https://translation.googleapis.com/targetLanguage/translate/v2?key=${GOOGLE_TRANSLATE_API_KEY}&q=${word}&target=${targetLanguage}`; + const targetLanguage = languageCodes[language]; + if (!targetLanguage) { + 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}`; const response = await fetch(url); const data = await response.json(); @@ -22,14 +31,6 @@ export const getGTranslate = async ( throw new Error(data.error.message); } - if (targetLanguage === "new") { - language = "newari"; - } - if (targetLanguage === "mai") { - language = "maithili"; - } - - //return data.data.translations[0].translatedText; return { language, word,