Diff
Compare at least two i18n files and generate a report
Command
# Display help for diff
npx @jy95/i18n-tools diff --help
Purpose
Suppose you have several versions of a i18n locale file such as :
- fr_v0.json
- fr_v1.json
- fr_v2.json
fr_v0.json
{
"untouchedKey":"Hello World",
"commons":{
"nestedKey":{
"changedValue":"Changed value 0"
},
"array":[
"Pierre"
],
"conditionalDeletedKey":"Present"
}
}
fr_v1.json
{
"untouchedKey":"Hello World",
"commons":{
"nestedKey":{
"changedValue":"Changed value 1"
},
"array":[
"Pierre",
"Paul"
]
}
}
fr_v2.json
{
"untouchedKey":"Hello World",
"commons":{
"nestedKey":{
"changedValue":"Changed value 2"
},
"array":[
"Pierre",
"Paul",
"Jacques"
],
"conditionalDeletedKey":"Present"
}
}
This command generates a report (in JSON by default) that shows change(s) between files :
- fr_v0.json & fr_v0.json
- fr_v0.json & fr_v1.json
- fr_v0.json & fr_v1.json & fr_v2.json
fr_v0-fr_v0.json
{
"files": {
"file1":"D:\\TEMP\\fr_v0.json",
"file2":"D:\\TEMP\\fr_v0.json"
},
"changes": []
}
fr_v0-fr_v1.json
{
"files":{
"file1":"D:\\TEMP\\fr_v0.json",
"file2":"D:\\TEMP\\fr_v1.json"
},
"changes":[
{
"from":"file1",
"key":"commons.nestedKey.changedValue",
"newValue":"Changed value 1",
"oldValue":"Changed value 0",
"to":"file2",
"type":"REPLACED"
},
{
"from":"file1",
"key":"commons.conditionalDeletedKey",
"oldValue":"Present",
"to":"file2",
"type":"DELETE"
},
{
"from":"file1",
"key":"commons.array[1]",
"newValue":"Paul",
"to":"file2",
"type":"ADD"
}
]
}
fr_v0-fr_v1-fr_v2.json
{
"files":{
"file1":"D:\\TEMP\\fr_v0.json",
"file2":"D:\\TEMP\\fr_v1.json",
"file3":"D:\\TEMP\\fr_v2.json"
},
"changes":[
{
"key":"commons.nestedKey.changedValue",
"type":"REPLACED",
"from":"file1",
"to":"file2",
"oldValue":"Changed value 0",
"newValue":"Changed value 1"
},
{
"key":"commons.conditionalDeletedKey",
"type":"DELETE",
"from":"file1",
"to":"file2",
"oldValue":"Present"
},
{
"key":"commons.array[1]",
"type":"ADD",
"from":"file1",
"to":"file2",
"newValue":"Paul"
},
{
"key":"commons.nestedKey.changedValue",
"type":"REPLACED",
"from":"file2",
"to":"file3",
"oldValue":"Changed value 1",
"newValue":"Changed value 2"
},
{
"key":"commons.array[2]",
"type":"ADD",
"from":"file2",
"to":"file3",
"newValue":"Jacques"
},
{
"key":"commons.conditionalDeletedKey",
"type":"ADD",
"from":"file2",
"to":"file3",
"newValue":"Present"
}
]
}
Examples of settings
- With two files
- With three files
- Settings.js
npx @jy95/i18n-tools diff --settings "/absolutePath/to/settings1.json"
settings1.json
{
"filename":"diff_settings1-JSON",
"outputDir":"D:\\TEMP\\TEMP",
"outputFormat":"JSON",
"files":[
"D:\\TEMP\\fr_v0.json",
"D:\\TEMP\\fr_v1.json"
]
}
npx @jy95/i18n-tools diff --settings "/absolutePath/to/settings2.json"
settings2.json
{
"filename":"diff_settings2-JSON",
"outputDir":"D:\\TEMP\\TEMP",
"outputFormat":"JSON",
"files":[
"D:\\TEMP\\fr_v0.json",
"D:\\TEMP\\fr_v1.json",
"D:\\TEMP\\fr_v2.json",
]
}
npx @jy95/i18n-tools diff --settings "/absolutePath/to/settings3.js"
settings3.js
module.exports = {
"filename":"diff_settings3-JSON",
"outputDir":"D:\\TEMP\\TEMP",
"outputFormat":"JSON",
"files":[
"D:\\TEMP\\fr_v0.json",
"D:\\TEMP\\fr_v1.json"
]
}
FAQ
The files I use are flat JSON. How can I make this command work ?
Simply set option keySeparator
to false
in your settings.json
or settings.js
, such as :
settings.json
{
"keySeparator": false
}
I only want some types of changes reported in the result file. How can I achieve that ?
Simply set the operations
option in your settings.json
or settings.js
to your liking :
tip
Current operations are :
Operation | Description |
---|---|
PUT | When key exists in both file1 & file2 but value was replaced |
ADD | When key exists in file2 but not in file1 |
DEL | When key exists in file1 but not in file2 |
settings.json
{
"operations": ["PUT"]
}