0

The more complex our tech-stack gets, the harder it is to find solutions online. Or at least that's what I believe and experiencing right now. I'm using Next.js with Typescript and want to use google translate like this from w3shools. I have found and implemented this solution but errors because of Typescript. Referring from the code below, it shows squiggly lines on new window.google.translate.TranslateElement and window.googleTranslateElementInit

import React, { useEffect } from "react";
import styles from "./index.module.css";
const BotBar: React.FC = () => {
  const googleTranslateElementInit = () => {
        new window.google.translate.TranslateElement(
            {
                pageLanguage: "en",
                autoDisplay: false,
            },
            "google_translate_element"
        );
    };
    useEffect(() => {
        var addScript = document.createElement("script");
        addScript.setAttribute(
            "src",
            "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"
        );
        document.body.appendChild(addScript);
        window.googleTranslateElementInit = googleTranslateElementInit;
    }, []);
    return <div className={styles.outerBotBar}>Botbar</div>;
};

export default BotBar;
Nugget
  • 81
  • 1
  • 6
  • 23
  • What are the errors that you're seeing? Is it related to this [How do you explicitly set a new property on `window` in TypeScript?](https://stackoverflow.com/questions/12709074/how-do-you-explicitly-set-a-new-property-on-window-in-typescript)? – juliomalves Sep 10 '22 at 00:58

0 Answers0