当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - Modify all leaf nodes of JSON file with gulp

问题描述:

I'm using angular-translate, and have a number of JSON files containing the translation strings and I already have it setup with json-combine to merge all of the individual files together into a single file for each language. The resulting file looks like this:

{

"myNamespace" : {

"CANCEL" : "Cancel",

"PENDING" : "Pending",

"module" : {

"USERNAME" : "User name",

"PASSWORD" : "Password"

}

}

}

However, I have the need to add characters to the start and end of each of the translation strings in order to perform testing that there aren't any hard-coded strings anywhere in the application, and can't seem to figure out how to do this.

I could manually modify each of the strings and create a separate language file, but I would prefer to do it programmatically, and AFTER the rest of the gulp tasks. Basically it should take the fully combined English file and modify each of the leaf-nodes to look like this:

{

"myNamespace" : {

"CANCEL" : "\u4ECFCancel\u7D42",

"PENDING" : "\u4ECFPending\u7D42",

"module" : {

"USERNAME" : "\u4ECFUser name\u7D42",

"PASSWORD" : "\u4ECFPassword\u7D42"

}

}

}

Is this possible? Any help would be greatly appreciated.

网友答案:

I was able to solve this on my own, using the following gulp task:

gulp.task("i18n-pseudo-locale", function() {
    return gulp.src(distDirectory + "/l10n/en_US.json")
        .pipe(jsoncombine(pseudoLocale + ".json", function(data) {
            var originalJson = data["en_US"];
            function processJson(json) {
                // Leaf node
                if (typeof(json) === "string") {
                    return "\u4ECF" + json +  "s\u7D42";
                }
                // object
                for(var propertyName in json) {
                    json[propertyName] = processJson(json[propertyName]);
                }
                return json;
            }
            finalJson = processJson(originalJson);
            return new Buffer(JSON.stringify(finalJson));
         }))
        .pipe(gulp.dest(distDirectory + "/l10n"));
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: