Today I Learned

Make sure translation files have the same keys

It’s relatively easy to add/remove translation and forgot to update files for other languages.

Here’s how make sure your translation files always have the same keys.

If the translations are in .json

.json translation files can be linted using eslint-plugin-i18n-json. It has documentation and examples.

If the translations are in .js

The simplest option is to add jest test.

import en from '.translations/en.js'
import pl from '.translations/pl.js'
import de from '.translations/de.js'

const enKeys = Object.keys(en)

describe('Translations', () => {
  test('PL translation have matching keys', async () => {
    expect(enKeys).toEqual(Object.keys(pl))
  })

  test('DE translation have matching keys', async () => {
    expect(enKeys).toEqual(Object.keys(de))
  })
})

Imgur

Profit

Now it will be impossible to desynchronize translation files if the PR merging is blocked by linting or tests failure.