From 488738b6d241c218a8fa10676556516827738658 Mon Sep 17 00:00:00 2001 From: Tina Date: Tue, 29 Oct 2024 13:21:28 +0530 Subject: [PATCH] Multi-Language Support Using Language Translator --- frontend/src/App.jsx | 5 +- frontend/src/Pages/GoogleTranslate.jsx | 148 +++++++++++++++++++++++++ frontend/src/Pages/Herosection.jsx | 10 +- 3 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 frontend/src/Pages/GoogleTranslate.jsx diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index cc51905..65fa1b4 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -16,6 +16,7 @@ import NotificationPage from "./Pages/notification"; import Chatbot from "./components/chatbot"; import ContactUs from "./Pages/ContactUs"; import Settings from "./components/Settings"; +import GoogleTranslate from "./Pages/GoogleTranslate"; import Help from "./components/help"; import Contributor from "./Pages/contributor"; import Payment from "./Pages/Payment"; @@ -44,6 +45,7 @@ function App() { } /> } /> } /> + } /> } /> } /> } /> @@ -51,7 +53,8 @@ function App() { } /> } /> } /> - } /> {/* Restored PrivacyPolicy */} + } />{" "} + {/* Restored PrivacyPolicy */} } /> {/* Added User */} } /> diff --git a/frontend/src/Pages/GoogleTranslate.jsx b/frontend/src/Pages/GoogleTranslate.jsx new file mode 100644 index 0000000..b5df449 --- /dev/null +++ b/frontend/src/Pages/GoogleTranslate.jsx @@ -0,0 +1,148 @@ +import React, { useEffect } from "react"; + +const GoogleTranslate = () => { + useEffect(() => { + window.googleTranslateInit = () => { + if (!window.google?.translate?.TranslateElement) { + setTimeout(window.googleTranslateInit, 100); + } else { + new window.google.translate.TranslateElement( + { + pageLanguage: "en", + includedLanguages: + "en,hi,pa,sa,mr,ur,bn,es,ja,ko,zh-CN,es,nl,fr,de,it,ta,te", + layout: + window.google.translate.TranslateElement.InlineLayout.HORIZONTAL, + defaultLanguage: "en", + autoDisplay: false, + }, + "google_element" + ); + } + cleanUpGadgetText(); + }; + + const loadGoogleTranslateScript = () => { + if (!document.getElementById("google_translate_script")) { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.src = + "https://translate.google.com/translate_a/element.js?cb=googleTranslateInit"; + script.id = "google_translate_script"; + script.onerror = () => + console.error("Error loading Google Translate script"); + document.body.appendChild(script); + } + }; + + const cleanUpGadgetText = () => { + const gadgetElement = document.querySelector(".goog-te-gadget"); + if (gadgetElement) { + const textNodes = gadgetElement.childNodes; + textNodes.forEach((node) => { + if (node.nodeType === Node.TEXT_NODE) { + node.textContent = ""; // Clear text content + } + }); + } + }; + loadGoogleTranslateScript(); + + if (window.google && window.google.translate) { + window.googleTranslateInit(); + } + + return () => { + // Cleanup logic if necessary + }; + }, []); + + return ( +
+ +
+ ); +}; + +export default GoogleTranslate; diff --git a/frontend/src/Pages/Herosection.jsx b/frontend/src/Pages/Herosection.jsx index d674302..750dcf4 100644 --- a/frontend/src/Pages/Herosection.jsx +++ b/frontend/src/Pages/Herosection.jsx @@ -17,6 +17,7 @@ import Navbar from "../components/navbar"; import Language from "../components/language"; import { FaUserAlt } from "react-icons/fa"; import Popup from "../components/popup"; +import GoogleTranslate from "./GoogleTranslate"; const Herosection = () => { const navigate = useNavigate(); @@ -63,12 +64,19 @@ const Herosection = () => {
+ - +
+ +
+

Namaste !! Yatree{" "}