'Automatic compare check in Angular i18n json files [closed]

I have 2 files en.json and xx.json with translations in Angular application. Common idea is to create translation in both files for multilanguage application, but sometimes some programmers adding translation just in one of these file, because they testing app only in their language.

I am looking for tool that check same structure in both JSON files, otherwise it throw error and it helps with addition when you creating translations for second language. Do you know any good practices, tools or plugins for this? I am using WebStorm.



Solution 1:[1]

Try POEditor. It's free up to 1000 strings.

Solution 2:[2]

Use a small Node.js script

Since you already have Angular installed (which means you have NPM & Node.Js), you can find inconsistencies in your translation JSON files with a script. Here's how it looks

Code

const fs = require('fs');

// Define your file paths here
const files = ['./english.json', './russian.json']

// Read contents and parse JSON
const filesWithKeys = files.map(f => ({
  name: f,
  content: JSON.parse(fs.readFileSync(f, 'utf8'))
}));

// Gather all the keys used in all files in one array
const allKeys = filesWithKeys.map(f => Object.keys(f.content)).flat();

// Find the missing keys by file
const missingKeysByFile = filesWithKeys.map(f => ({
  name: f.name,
  missingKeys: allKeys.filter(k => !(k in f.content))
})).filter(f => f.missingKeys.length > 0);


// Print the result
missingKeysByFile.forEach(f => {
  console.log(`File "${f.name}" is missing keys [ ${f.missingKeys.join(' ')} ]`);
});

Sample output

File "english.json" is missing keys [ appTitle, contentHeader ]
File "russian.json" is missing keys [ usernameHint ]

Solution 3:[3]

Solution 4:[4]

I use this tool for handling translations in my projects https://translation-manager-86c3d.firebaseapp.com/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabriel Csöllei
Solution 2 molamk
Solution 3 Ankur Jain
Solution 4 Ivan Zinchenko