C'è un come posso automatizzare la creazione di file .json usati per le traduzioni della lingua?
SI, l'esecuzione di attività automatiche è esattamente ciò che strumenti di automazione come Grunt e Gulp dove progettato per fare.
Come hai detto, fare le cose manualmente è laborioso e soggetto a errori, quindi Grunt/Gulp è la strada da percorrere.
Con un semplice Grunt/Gulp config, tutti i file .json rilevanti possono essere guardato contemporaneamente: un tasto qualsiasi aggiunto a qualsiasi di essi sarà rilevato istantaneamente, e ordinare l'esecuzione dello script personalizzato di vostra scelta.
COME GRUNT/GULP può farlo:
- Grunt/Gulp sarà costantemente orologio tutti i file JSON pertinenti;
- Quando viene rilevata una modifica in un file guardato, viene eseguito uno script personalizzato ;
- Lo script personalizzato sarà letto il file modificato e recupererà la nuova chiave (s) e valore (s);
- Lo script personalizzato sarà quindi scrivere in tutti gli altri file JSON pertinenti.
CONFIGURAZIONE GRUNT
Per rilevare automaticamente le modifiche di file ed eseguire myCustomScript
, basta usare grunt-contrib-watch in questo modo:
watch: {
scripts: {
files: ['**/*.locale.json'],
tasks: ['myCustomScript'],
},
}
CUSTOM script per aggiungere la nuova chiave (S) PER IL RILEVANTE.JSON FILES:
grunt.event.on('watch', function(action, filepath) {
// filepath is the path to the file where change is detected
grunt.config.set('filepath', grunt.config.escape(filepath));
});
var myCustomScript=function(changedFile,keyFile){
var project = grunt.file.readJSON(changedFile);
//will store the file where changes were detected as a json object
var keys=grunt.file.readJSON(keyFile);
//will store keyFile as a json object
//walk changedFile keys, and check is keys are in keyFile
for (var key in project) {
if (project.hasOwnProperty(key)) {
if(!keys.hasOwnProperty(key)){
//a new key was detected
newKeyArray.push(key);
}
}
}
//should update all the other relevant JSON files with `grunt.file.write`, and add all the keys in newKeyArray:
var filesToChangeArray=grunt.file.match('**/*.locale.json');
//returns an array that contains all filepaths where change is desired
filesToChangeArray.forEach(function(path){
//walk newKeyArray to set addedContent string
newKeyArray.forEach(function(key){
addedContent+='"'+key+'":"to be set",';
//this will write all the new keys, with a value of "to be set", to the addedContent string
}
grunt.file.write(path,addedContent);
});
}
Idealmente mi piacerebbe essere in grado di eseguire uno script da Windows PowerShell
Anche se Grunt/Gulp vengono spesso utilizzati per eseguire i file personalizzati scritti in JavaScript/nodejs, sono in grado di ordinare l'esecuzione di script scritti in altre lingue.
per eseguire uno script PowerShell, è possibile utilizzare un plugin chiamato Grunt grunt-shell, in questo modo:
grunt.initConfig({
shell: {
ps: {
options: {
stdout: true
},
command: 'powershell myScript.ps1'
}
}
});
come detailed in this SO post.
Quindi, se PowerShell è la vostra passione, si potrebbe avere il meglio dei due mondi:
- rilevamento facile con Grunt/Gulp guardare;
- Esecuzione di script PowerShell quando viene rilevata una modifica.
Tuttavia, si potrebbe facilmente utilizzare Grunt/Gulp solo per questo: come Grunt/Gulp sta già prendendo cura della rilevazione in background, tutto quello che dovete fare è avere correre uno script personalizzato che legge le nuove chiavi (grunt.file.readJSON
) e le copia (grunt.file.write
) nei file pertinenti.
È possibile semplicemente creare un'attività di gulp, passare la chiave di traduzione e specificare in quali file si desidera aggiungere. –
Inoltre, forse ci sono alcuni servizi online per i traduttori che consentono all'utente di tradurre le cose ed esportarle in diversi formati. – DWand
Quale IDE stai usando? –