Fix React issues with Google Translate

Note: we are only talking about Google Translate function in Chrome or Chromium based browsers. Other translate plugins or software don’t necessarily work with this solution. And this solution doesn’t solve all issues, just a part of them.

When does it happen

You have a component:

const Button = ({children, icon, isLoading, ...rest}) => (
  <button {...rest}>
    {icon}
    {children}
    {isLoading && <Loader/>}
  </button>
)

const Test = () => <Button icon="+">Hello World</Button>

Which is rendered to:

<button>+Hello World</button>

If an element (<button> in this case) contains multiple rendered string variables, they become text nodes in React’s VDOM and HTML DOM. However, Google Translate doesn’t care about text nodes and wrap them inside a <font> element:

<button><font>+你好世界</font><button>

Since they are not text nodes anymore, synchronization between React and DOM was broken. The button content will not be updated anymore.

How to fix it

Simply avoid this situation. Write every {variable} inside an element, as the only child.

const Button = ({children, icon, isLoading, ...rest}) => (
  <button {...rest}>
    <span className="button__icon">{icon}</span>
    <span className="button__text">{children}</span>
    {isLoading && <Loader/>}
  </button>
)

const Test = () => <Button icon="+">Hello World</Button>

Now every variable is synchronized with the <span> element, not text nodes. Inserting <font> elements doesn’t break the connection.

Another situation is that when you mix text nodes with elements.

<p>
  Copyright 2016-{new Date().getFullYear()}
</p>

Should be converted to:

<p>
  <span>Copyright 2016-</span>
  <span>{new Date().getFullYear()}</span>
</p>

Just make sure: a text node or string variable must be the only child of an element.

Event target

Another issue is that the click event target will change. It might be <font> elements instead of <button>. Carefully check everywhere you use e.target, considering they might not be the button, input or other elements you expected.

A further suggestion is to avoid using any e.target reference. Here is a piece of legacy code:

const {sizes, onChange} = this.props
return (
  <div>
    {sizes.map(size => (
      <button
        key={size}
        name="size"
        value={size}
        onClick={e => this.props.onChange(e.target.value)}>
        {size.toUpperCase()}
      </button>
    ))}
  </div>
)

Can be changed to:

const {sizes, onChange} = this.props
return (
  <div>
    {sizes.map(size => (
      <button
        key={size}
        name="size"
        value={size}
        onClick={e => this.props.onChange(size)}>
        {size.toUpperCase()}
      </button>
    ))}
  </div>
)

If you use third-party components from a library

In this case, you usually cannot modify the component as you want.

You can try to use <span>your string</span> instead of "your string" as component properties.

Report to the library maintainers and make a PR with above methods.

If the element doesn’t support child elements

Some components doesn’t support inner wrappers, like <option>. If you put <span> inside <option>, React will give you warnings in console, even though the rendering works. So you probably don’t want to do it.

For example you have:

<select>
  {fruites.map(f => <option value={f.name}>{f.name} - {f.price}</option>)}
</select>

Change it to:

<select>
  {fruites.map(f => {
    const label = f.name + ' - ' + f.price;
  	return <option value={f.name}>{label}</option>
  })}
</select>

If you want to disable Google Translate

Even if you do all above, something can still go wrong. If you don’t have time to waste and just want to disable Google Translate, it is simple:

<body class="notranslate">
  ...
</body>

Posted

in

by

Comments

2 responses to “Fix React issues with Google Translate”

  1. Devon Avatar

    I was just troubleshooting this exact issue on a vanilla JS website and found this article which was really helpful. I discovered you can also set the tag style to pointer-events: none using CSS and it also fixes issues where the tag steals the click event;

    font {
      pointer-events: none
    }

    This disables pointer events and prevents the event from bubbling up from the tag. https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events

  2. Minh Vu Avatar

    Thank you very much, the first way works for me. I am using Nextjs 13. The error happens when users translate to another language.

Leave a Reply

Your email address will not be published. Required fields are marked *