i18n Check: Tips & Tricks for Comparing Localization Files
How many times have I forgotten to update my localization JSON file when I added a new key?
We are human beings, and we can do wrong above all when we write something like code.
I wanted to search for something that can give me more safety when I add a new key to the localization files.
My first idea was to write a node script to check all JSON files in the locales dir, but then I found this useful plugin:
Yes, I know: It doesn’t have a lot of stars on GitHub, but I tried it and it works perfectly.
Why do we have to recreate the wheel?
Let’s dive into this plugin.
Installation and configuration
You can see all the documentation inside the Readme file on the GitHub repo.
here to install the plugin I used the pnpm command
pnpm add --save-dev @lingual/i18n-check
Then we have to create a command in package.json
like this:
"i18n:check": "npx i18n-check --locales public/locales -s en -f i18next && i18n-check --locales public/locales -s it -f i18next"
here I’m calling i18n-check with --locales
option. With this you define which folder or multiple folders you want to run the i18n checks against.
My structure files are this:
.
└── public/
└── locales/
├── it/
│ ├── pricing.json
│ └── external.json
└── en/
├── pricing.json
└── external.json
I used the &&
operator because i18n-check wants a target to compare the files from to start. In my case, I want to have every file notify me about missing keys or invalid translations.
So if you use husky precommit you can configure that with this command:
pnpm run i18n:check
if you try to remove any keys from any files and launch the command you can see this:
So with this, every change (add or remove) in the JSON localization file will be notified, and we have to update it.
Thanks for the reading. See ya ✨