我有一个项目,我必须生成翻译后的静态页面。之所以选择使用 gulp,是因为它在缩小资源、监视文件更改和重新编译方面有很大帮助,并且还可以在多个页面中注入 html 模板。
我使用了:
- 'gulp-inject':用于将模板插入最终文件
- 'gulp-translate-html':用于翻译,因为我有 '.json' 字典
所以我有两个问题:
- 'gulp-翻译-html'
这使用 json 作为翻译的输入,使用以下代码:
gulp.task('translate', function() {
return gulp.src('./temp/en/template.html')
.pipe(translateHtml({
messages: require('./dictionary/en.json'),
templateSettings: {
interpolate: /{{([\s\S]+?)}}/g
}
}))
.pipe(gulp.dest('./en'));
});
我在“.json”文件上创建了一个手表,修改后,它应该重新应用翻译。但不知何故,它使用旧文件而不是修改后的文件。有解决方法吗?或者我可以用于 json 文件的其他插件?
'gulp-inject' 在上面的代码示例中,我只翻译了一个文件。但是我需要为几种具有不同目的地的语言这样做,所以我对这些语言使用了一个循环。(对不起代码缩进)
var gulp = require('gulp'), inject = require('gulp-inject'), translateHtml = require('gulp-translate-html'); var languages = ['en', 'de']; gulp.task('injectContent', function() { /* the base file used as a reference*/ var target = gulp.src('./templates/base/baseTemplate.html'); /* get each language*/ languages.forEach(function(lang) { target.pipe(inject(gulp.src('./templates/partials/injectTemplate.html'), { relative: true, starttag: '<!-- inject:template -->', transform: function (filePath, file) { return file.contents.toString('utf8'); } })) /* put the merged files into "temp" folder under its language folder*/ .pipe(gulp.dest('./temp/'+lang)); }); }); /* The translation has to be made after the injection above is finished*/ gulp.task('translate', ['injectContent'] function() { /* get each language*/ languages.forEach(function(lang) { gulp.src('./temp/'+ lang +'/baseTemplate.html') .pipe(translateHtml({ messages: require('./dictionary/'+lang+'.json');, templateSettings: { interpolate: /{{([\s\S]+?)}}/g } })) .pipe(gulp.dest('./'+lang)); /* put file in the "en" or "de" language folder*/ }); }); gulp.task('watch', function() { gulp.watch(['./templates/**/*.html', './dictionary/*.json'], ['translate']); }); gulp.task('default', ['translate', 'watch']);
在这里,我希望在 'translation' 任务之前运行 'injectContent' 任务,但后者运行得太快了。发生这种情况是因为“injectContent”中没有特定的返回 gulp 回调,对吧?
如何合并结果而不让任务插入?