Is there a way to automate the creation of .json files for language translations?
Absolutely, automation tools like Grunt and Gulp are designed specifically for executing automatic tasks.
Manually handling these tasks is time-consuming and prone to errors, which is why using Grunt/Gulp is highly recommended.
By setting up a simple configuration in Grunt/Gulp, all relevant .json files can be monitored at the same time: any new key added to one file will trigger the execution of a custom script of your choice.
HOW GRUNT/GULP CAN MAKE IT HAPPEN:
- Grunt/Gulp will continuously monitor all necessary JSON files;
- When a change is detected in one of these files, a custom script is executed;
- The custom script will read the modified file to get the new key(s) and value(s);
- This information is then written to all other related JSON files.
SETTING UP THE GRUNT CONFIGURATION
To automatically detect file changes and run myCustomScript
, you can use grunt-contrib-watch as follows:
watch: {
scripts: {
files: ['**/*.locale.json'],
tasks: ['myCustomScript'],
},
}
CUSTOM SCRIPT FOR ADDING NEW KEYS TO THE RELEVANT .JSON FILES:
grunt.event.on('watch', function(action, filepath) {
// Path to the file with detected changes
grunt.config.set('filepath', grunt.config.escape(filepath));
});
var myCustomScript=function(changedFile,keyFile){
var project = grunt.file.readJSON(changedFile);
// Store the changed file as a json object
var keys=grunt.file.readJSON(keyFile);
// Store keyFile as a json object
// Check if keys from changedFile are in keyFile
for (var key in project) {
if (project.hasOwnProperty(key)) {
if(!keys.hasOwnProperty(key)){
// New key detected
newKeyArray.push(key);
}
}
}
// Update all the other relevant JSON files with `grunt.file.write`, by adding all keys in newKeyArray:
var filesToChangeArray=grunt.file.match('**/*.locale.json');
// Array containing all filepaths where changes should be made
filesToChangeArray.forEach(function(path){
// Add new keys to the addedContent string from newKeyArray
newKeyArray.forEach(function(key){
addedContent+='"'+key+'":"to be set",';
// Write all new keys to be set in addedContent string
}
grunt.file.write(path,addedContent);
});
}
Ideally I would like to be able to run a script from Windows PowerShell
While Grunt/Gulp are commonly used to execute javascript/nodejs files, they can also handle the execution of scripts written in different languages.
To run a PowerShell script, you could utilize a Grunt plugin called grunt-shell, like this:
grunt.initConfig({
shell: {
ps: {
options: {
stdout: true
},
command: 'powershell myScript.ps1'
}
}
});
as explained in this StackOverflow post.
If PowerShell is your preference, you can combine both approaches:
- Effortless detection with Grunt/Gulp watch;
- PowerShell script execution upon detecting changes.
You could alternatively use only Grunt/Gulp for this task: since Grunt/Gulp already handles the detection process, you just need it to run a custom script that reads and copies new keys (grunt.file.readJSON
and grunt.file.write
) to the relevant files.