react-facebook icon indicating copy to clipboard operation
react-facebook copied to clipboard

How to change locale without page refresh?

Open TimPchelintsev opened this issue 6 years ago • 3 comments

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 https://github.com/seeden/react-facebook/issues/82#issuecomment-404826316

TimPchelintsev avatar Nov 11 '19 15:11 TimPchelintsev

hah :) nice. I will

seeden avatar Nov 25 '19 07:11 seeden

Some updates on this?

sirshurak avatar Mar 17 '21 12:03 sirshurak

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

gtournie avatar Jul 01 '23 15:07 gtournie