The guys don’t know how to write javascript , which creates json with the same structure as in the original file, but only in one language, for example in cz-CZ and of course so that it contains only the missing labels.

PS labels are all elements of the page. The parser wrote, but again I don’t know how to pass the name of the source file and the source language as an input parameter.

 var fs = require("fs"); fs.readFile('labels.json', function (err, data) { if (err) { return console.error(err); } console.log(data.toString()); var obj = JSON.parse(data) fs.writeFile('labels.cz-CZ.json'); }); 

an example of the json file itself, labels in it, that is, the content of the page in two languages, it is necessary that javascript create json in which labels are only those that are not translated or the translation does not exist

 "Shell": { "Profile": "Profile", "SignOut": "Sign out", "CloseView": "Close current view", "Settings": "Settings", "ModifySettings": "Modify settings", "SettingsModified": "Settings have been changed", "Help": "Help" } 
  • Give an example of the source file, and what should come out of it. Without this, it is not clear what exactly you are trying to achieve. - Yaant
  • sorry forgot to add - t0rick

1 answer 1

I think you just need to compare two objects.

Suppose we loaded two objects from files:

 var target = JSON.parse(dataDefault); var current = JSON.parse(dataCZ); 

Next, we compare all the properties, if there is no such property or its value is equal to the original one, then we write it into the resulting object:

 var result = {}; for (var key in target) { if (!target.hasOwnProperty(key)) continue; if (target[key] == current[key] || !current[key]) { result[key] = target[key]; } } 

Next, save:

 fs.writeFile("result.json", JSON.stringify(result)); 

Update:

I see you have a complex structure, you need to walk through all the nested values ​​recursively. You can add the following function:

 function compare(target, current){ var result = {}; for (var key in target) { if (!target.hasOwnProperty(key)) continue; if(typeof target[key] == "object"){ result[key] = compare(target[key], current[key]) } else { if (target[key] == current[key] || !current[key]) { result[key] = target[key]; } } } return result; } 

And call:

 fs.writeFile("result.json", JSON.stringify(compare(target, current))); 

 var target = { "Shell" : { "Profile" : "Profile", "SignOut" : "Sign out", "CloseView" : "Close current view", "Settings" : "Settings", "ModifySettings" : "Modify settings", "SettingsModified" : "Settings have been changed", "Help" : "Help" } } var current = { "Shell" : { "Profile" : "Профиль", "SignOut" : "Выйти", "CloseView" : "", "Settings" : "Settings", "ModifySettings" : "Дополнительные настройки", "Help" : "Помощь" } }; function compare(target, current) { var result = {}; for (var key in target) { if (!target.hasOwnProperty(key)) continue; if (typeof target[key] == "object") { result[key] = compare(target[key], current[key]) } else { if (target[key] == current[key] || !current[key]) { result[key] = target[key]; } } } return result; } console.log(compare(target, current)); 

Update 2

Your code should look something like this:

 // получить третий аргумент из командной строки var name = process.argv[2]; var defaultName = "en_US"; var file = {/*...*/} var nfResult = {}; if(!file[name]){ nfResult = file[name] = name[defaultName]; } else { nfResult = compare(name[defaultName], file[name]); } 
  • You can chat to unclutter the broadcast @CoddWrench - t0rick
  • Yes, you correctly understood me, that is, I have to create a file that takes the name of a json file as an argument, that is, function extract (parametr1, parametr2) then reads labels.json only the en_US section, and checks if there is a cs_CZ section, if not, it copies in the missing_cs_CZ.json file, the entire en_US section, if there is one, then it compares the en_US section with the cs_CZ section and those that are different or do not exist are written in a separate json - t0rick
  • @ t0rick, you described something too complicated and not obvious, try to break it down into several different questions. - Codd Wrench
  • the first task, through the console using node.js I run extract-missing-labels.js with the labels.json cs_CZ parameter the second one, this js checks if there is a cs_CZ section in the structure of the labels.json file, if not, it copies the en_US section from the labels. json and inserts it into another missing-labels.js, if there is, then it compares the section in labels.json en_US with cs_CZ, and what does not match is written in labels.cs_CZ.json which creates something like it broke into puzzles @Codd Wrench - t0rick nov
  • @ t0rick, see the second update, I think you will figure it out further. - Codd Wrench