简体   繁体   English

使用gulp创建本地化的资源包

[英]Using gulp to create localized resource bundles

I have a bunch of files in a resources directory: 我在资源目录中有一堆文件:

  • labels.no.js labels.no.js
  • labels.en.js labels.en.js
  • errors.no.js errors.no.js
  • errors.en.js errors.en.js

I want to create one resource bundle per language so that I will server only one of these bundles to the client based on client's language. 我想为每种语言创建一个资源束,以便仅根据客户端的语言将这些束中的一个捆绑到客户端。 So I want to have this result: 所以我想得到这个结果:

  • bundle.en.js (all en files from various directories) bundle.en.js(来自各个目录的所有en文件)
  • bundle.no.js (the same for no files) bundle.no.js(无文件相同)

I don't want to hardcode the languages (otherwise I could have copy/pasted), so I need some way to automatically "join" files in one bundle based on the suffix (so no matter how many languages I have, I will have the number of bundles equal to the number of languages grouped by suffix) 我不想对语言进行硬编码(否则我可以复制/粘贴),因此我需要一些方法根据后缀自动将文件“捆绑”在一个捆绑包中(因此,无论我有多少种语言,我都会有捆绑数量等于后缀分组的语言数量)

I managed to do it with help of gulp-group-aggregate: 我设法在gulp-group-aggregate的帮助下做到了:

var groupAggregate = require("gulp-group-aggregate");

gulp.task("buildResources:js", function () {
return gulp.src(["/sourcefolder"], { base: "." })
    .pipe(groupAggregate({
        group: function (file) {

            var extractResourceLocale = function() {
                var result = file.relative.substring(0, file.relative.length - 3); // remove ".js"
                var dotIndex = result.lastIndexOf(".");
                return result.substring(dotIndex + 1, result.length);
            }

            return extractResourceLocale(file);
        },
        aggregate: function (group, files) {

            var filesArray = [];

            for (var i = 0; i < files.length; i++) {
                filesArray[i] = files[i].relative;
            }

            var bundlePath = "bundles/result." + group + ".js";

            gulp.src(filesArray)
                .pipe(concat(bundlePath))
                .pipe(uglify())
                .pipe(gulp.dest("."));

            return {
                path: ".",
                contents: null
            }
        }
    }))
    .pipe(gulp.dest("."));

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM