From 2947e32d90369f622a274124bb4fb70b721ca19a Mon Sep 17 00:00:00 2001 From: reborn_star1 Date: Mon, 7 Oct 2024 10:27:35 +0530 Subject: [PATCH 1/3] Google Translate Functionality is Added in 10-12 languages --- frontend/src/components/GoogleTranslate.jsx | 63 +++++++++++++++++++++ frontend/src/components/Navbar.jsx | 6 +- frontend/src/pages/About.jsx | 4 ++ frontend/src/pages/Home.jsx | 4 ++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 frontend/src/components/GoogleTranslate.jsx diff --git a/frontend/src/components/GoogleTranslate.jsx b/frontend/src/components/GoogleTranslate.jsx new file mode 100644 index 00000000..dd174c5a --- /dev/null +++ b/frontend/src/components/GoogleTranslate.jsx @@ -0,0 +1,63 @@ +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'); + } + }; + + 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(); + } + }, []); + + return ( +
+ {/* Google translate element */} +
+ + {/* Custom select styling */} + +
+ ); +}; + +export default GoogleTranslate; diff --git a/frontend/src/components/Navbar.jsx b/frontend/src/components/Navbar.jsx index 917198f8..414b60c8 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 ( -