diff --git a/frontend/src/components/GoogleTranslate.jsx b/frontend/src/components/GoogleTranslate.jsx new file mode 100644 index 00000000..4294fab9 --- /dev/null +++ b/frontend/src/components/GoogleTranslate.jsx @@ -0,0 +1,109 @@ +import React, { useEffect } from "react"; +import { useState } from "react"; + +const GoogleTranslate = () => { + const [isVisible,setIsVisible] = useState(true); + + 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'); + } + }; + + 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); + } + }; + + loadGoogleTranslateScript(); + + if (window.google && window.google.translate) { + window.googleTranslateInit(); + } + + const handleScroll = () => { + setIsVisible(window.scrollY < 100); // Adjust the scroll amount as needed + }; + + window.addEventListener('scroll', handleScroll); + + return () => { + window.removeEventListener('scroll', handleScroll); + }; + }, []); + + return ( +
+ +
+ ); +}; + +export default GoogleTranslate; diff --git a/frontend/src/components/Navbar.jsx b/frontend/src/components/Navbar.jsx index 917198f8..414fec97 100644 --- a/frontend/src/components/Navbar.jsx +++ b/frontend/src/components/Navbar.jsx @@ -2,6 +2,7 @@ import { useState } from 'react'; import { Link } from 'react-router-dom'; // import { FaSun, FaMoon } from 'react-icons/fa'; // Import icons from react-icons import icon from '../assets/favicon2.png'; +import GoogleTranslate from './GoogleTranslate'; // import useTheme from '../hooks/useTheme'; // Import useTheme hook @@ -18,7 +19,7 @@ const Navbar = () => { }; return ( -