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>

Code Journey #6

To avoid too many short posts, I will summarize my work weekly in future. Post date will be the every Sunday.

openSUSE

opi

  • Install Packman codecs, VS Code, Skype
  • PMBS search function research (once get a user account, it is ready to go)

Packaging

  • Update adobe-sourcehansans-fonts and adobe-sourcehanserif-fontswith additional provides for languages

KDE

Translation

  • Keep core translation 100%
  • Translate Kdenlive
  • Synchronize translation to SVN

Code Journey #5

openSUSE

opi

  • First version released
  • Packaging and submit to openSUSE Factory

Packaging

  • Update adobe-sourcehansans-fonts
  • Update adobe-sourcehanserif-fonts
  • Update google-noto-emoji-fonts

Code Journey #4

KDE

Translation

  • Kdenlive

openSUSE

Geeko Store

  • Test OBS search API with curl

Packaging

  • Submit vnote

Git Cola

  • Simplified Chinese translation

Code Journey #3

KDE

Translation

  • Translate core parts

openSUSE

Wiki

  • Fix print layout issue
  • Fix header/footer language
  • Fix sticky table-of-content
  • Edit Apache related pages
  • Edit Chromecast firewall section

Packaging

  • Update WoeUSB

Forum

  • Repair IRC forward bot

Translation

  • yast-control Desktop List

Code Journey #2

KDE

Translation

  • Update core translations

openSUSE

Packaging

  • Update python-PyMuPDF package

Bug

Geeko Store

  • Made UI with Qt Quick Controls
  • Tried to run the app in High DPI, and found Native Text Rendering cannot scale fonts properly. So I have to use Qt Text Rendering conditionally

Code Journey #1

This is the first post. Free Software is a meaningful thing. By recording what I have done, motivate me to keep going.

KDE

Translation

  • Synchronize Simplified Chinese translation.

openSUSE

Packaging

  • Update php-composer package
  • Submit python-PyMuPDF package
  • Update riot-web package

Geeko Store Development

  • Geeko Store project restarted