最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Integrating google translate widget in Next JS application - Stack Overflow

programmeradmin0浏览0评论

I am trying to integrate google translate dropdown widget in my Next Js application, I integrated that the widget also but when I try to redirect from one page to another where the widget will not be available, the page gets blank and throws error like NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

I tried to not clean up the component for persisting the script but it also doesn't seem to be working.

"use client";
  
import { useEffect } from "react";
  

  
const GoogleTranslate = () => {
  
  useEffect(() => {
  
    // Load the Google Translate script
  
    const script = document.createElement("script");
  
    script.src =
  
      ".js?cb=googleTranslateElementInit";
  
    script.async = true;
  
    document.body.appendChild(script);
  

  
    // Initialize the Google Translate element
  
    window.googleTranslateElementInit = () => {
  
      new window.google.translate.TranslateElement(
  
        { pageLanguage: "en" },
  
        "google_translate_element"
  
      );
  
    };
  
  }, []);
  

  
  return <div id="google_translate_element"></div>;
  
};
  

export default GoogleTranslate;

This is my google translate component code and I am using it in the header of the application like this,

<AppBox>
<GoogleTranslate />
</AppBox>

Note: I am using App Router

I am trying to integrate google translate dropdown widget in my Next Js application, I integrated that the widget also but when I try to redirect from one page to another where the widget will not be available, the page gets blank and throws error like NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

I tried to not clean up the component for persisting the script but it also doesn't seem to be working.

"use client";
  
import { useEffect } from "react";
  

  
const GoogleTranslate = () => {
  
  useEffect(() => {
  
    // Load the Google Translate script
  
    const script = document.createElement("script");
  
    script.src =
  
      "https://translate.google/translate_a/element.js?cb=googleTranslateElementInit";
  
    script.async = true;
  
    document.body.appendChild(script);
  

  
    // Initialize the Google Translate element
  
    window.googleTranslateElementInit = () => {
  
      new window.google.translate.TranslateElement(
  
        { pageLanguage: "en" },
  
        "google_translate_element"
  
      );
  
    };
  
  }, []);
  

  
  return <div id="google_translate_element"></div>;
  
};
  

export default GoogleTranslate;

This is my google translate component code and I am using it in the header of the application like this,

<AppBox>
<GoogleTranslate />
</AppBox>

Note: I am using App Router

Share Improve this question edited Mar 12 at 8:37 Hilory 2,1577 gold badges14 silver badges30 bronze badges asked Mar 11 at 13:57 Selvakumar SekarSelvakumar Sekar 11 bronze badge 2
  • Can the script not be available globally? Also, you may want to consider using the nextjs Script tag- nextjs./docs/app/api-reference/components/script – Mark Hollis Commented Mar 11 at 15:00
  • @MarkHollis Thanks for your reply. I tried to register it globally also, but I faced the same issue. I will try adding the next JS built-in script tag as you mentioned. Once again, thank you. – Selvakumar Sekar Commented Mar 11 at 15:22
Add a comment  | 

1 Answer 1

Reset to default 0

After hours of debugging, I came across this comment in github, which solves my problem. I hope those who face this issue can also look into this comment, which will be helpful, thank you! https://github/facebook/react/issues/11538#issuecomment-390386520

发布评论

评论列表(0)

  1. 暂无评论