Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change locale without page refresh? #156

Open
TimPchelintsev opened this issue Nov 11, 2019 · 3 comments
Open

How to change locale without page refresh? #156

TimPchelintsev opened this issue Nov 11, 2019 · 3 comments

Comments

@TimPchelintsev
Copy link

Thank you for great lib! I encounter this problem with dynamic language switching for embeded facebook page on gatsbyjs-powered site. When I try to use another lib(for facebook chat), it does not have this issue. Maybe you can kindly see their approach?

https://github.com/Yoctol/react-messenger-customer-chat/blob/master/src/MessengerCustomerChat.js

you need to "refresh" page because you need to redownload new sdk file. if you will found another way just let me know

Originally posted by @seeden in #82 (comment)

@seeden
Copy link
Owner

seeden commented Nov 25, 2019

hah :) nice. I will

@sirshurak
Copy link

Some updates on this?

@gtournie
Copy link

gtournie commented Jul 1, 2023

For now, here is my (ugly) workaround:

  const intl = useIntl()
  
  useEffect(() => {
    function replaceFBLocale() {
      const fbIframes = document.querySelectorAll("iframe[src*=facebook]")
      fbIframes.forEach((fbIframe) => {
        fbIframe.src = fbIframe.src.replace(/&locale=[a-zA-Z_]+/, "&locale=" + intl.locale.replace("-", "_"))
      })
      return !!fbIframes.length
    }

    // on facebook iframe first load
    let observer
    if (!replaceFBLocale()) {
      observer = new MutationObserver(() => replaceFBLocale() && observer.disconnect())
      observer.observe(document.body, { attributes: false, childList: true, characterData: false, subtree: true })
    }

    return () => observer && observer.disconnect()
  }, [intl.locale])

For convenience, I put this code in a hook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants