Automatic compare check in Angular i18n json files [closed]
Asked Answered
Z

3

6

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.

Zoography answered 5/2, 2019 at 12:6 Comment(2)
"attranslate" is a modern tool that is specifically designed for comparing and synchronizing JSON-files: github.com/fkirc/attranslateUnmentionable
I've done the exact thing you're looking for in a C# unit test, but since the question is closed I cannot post my findings. Sometimes stackoverflow is too strict..Anfractuous
C
1

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

Cavender answered 5/2, 2019 at 12:9 Comment(0)
L
4

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 ]
Lexicostatistics answered 5/2, 2019 at 13:2 Comment(2)
Note: Doesn't seem to work for nested JSONs.Rate
You could also do it in a c# unit test and gather all the possible jpaths like described in this; #27341519Anfractuous
C
1

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

Cavender answered 5/2, 2019 at 12:9 Comment(0)
T
1

You can check Pootle (http://pootle.translatehouse.org/index.html) or Poedit (https://poedit.net) or POEditor (https://poeditor.com/)

Thriller answered 5/2, 2019 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.