Completed
Push — master ( e4d488...d7a2df )
by Sander
30s
created

fixLocale.js (3 issues)

1
var fs = require('fs');
2
var en = JSON.parse(fs.readFileSync('_locales/en/messages.json', 'utf8'));
3
4
var walkSync = function (dir, filelist) {
5
    var files = fs.readdirSync(dir);
6
    filelist = filelist || [];
7
    files.forEach(function (file) {
8
        if (fs.statSync(dir + file).isDirectory()) {
9
            filelist = walkSync(dir + file + '/', filelist);
10
        }
11
        else {
12
            filelist.push(dir + file);
13
        }
14
    });
15
    return filelist;
16
};
17
18
var language_files = [];
19
language_files = walkSync('_locales/', language_files);
20
21
for (var i = 0; i < language_files.length; i++) {
22
    var file = language_files[i];
23
    if(file == '_locales/en/messages.json'){
24
        continue;
25
    }
26
    var json_data = JSON.parse(fs.readFileSync(file, 'utf8'));
27
    for (var translate_key in json_data) {
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
28
        if (en[translate_key] && !translate_key.hasOwnProperty('placeholders') && en[translate_key].hasOwnProperty('placeholders')) {
29
            console.log('Fixed ' + file + ' translate key: ' + translate_key);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
30
            json_data[translate_key].placeholders = en[translate_key].placeholders;
31
        }
32
    }
33
    var options = { flag : 'w' };
34
    fs.writeFile(file, JSON.stringify(json_data, null, 4), function (err) {
35
        if(err){
36
           console.log('Error during saving');
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
37
        }
38
    });
39
40
}