i18next-parser icon indicating copy to clipboard operation
i18next-parser copied to clipboard

Detect Reused Keys

Open amandapouget opened this issue 3 years ago • 1 comments

🚀 Feature Proposal

Allow detect key reuse and option to warn or fail on reuse. Example: dev writes t('foobar') in two different files ultimately writing translations to the same JSON. With config warnOnDuplicates: true, parser would log something like, Key 'foobar' is reused 2 times. With config failOnWarnings, this situation would cause an exit 1.

I believe this can be accomplished via an option and conflict type added here.

Motivation

Accidental key reuse results in wrong translations. The parser should be able to detect and prevent this scenario. It happens especially in cases where the key is used as default value.

For example, t('Cancel') in one place might mean "Decide not to submit a form", while in another place, it might mean "End your subscription". Forcing developers to use unique keys prevents accidental mistranslations.

Example

// Foo.js

t('Foobar')

// Bar.js

t('Foobar')

=> 'Warning: key 'Foobar' is reused 2 times.'

amandapouget avatar Dec 23 '21 21:12 amandapouget

A warning is thrown if there is a key use twice with a conflict on the value. What you are proposition is not yet implemented though.

karellm avatar Apr 04 '22 02:04 karellm