【发布时间】:2019-11-29 18:15:01
【问题描述】:
我想从一个通用模板生成多个页面,这些页面将包含不同语言的内容。如何使用 webpack 做到这一点?
我尝试使用不同的 webpack 插件,例如 webpack-static-i18n-html、i18n-webpack-plugin,但对我没有任何帮助。我找到的最好的东西是webpack-static-i18n-html,但它的支持很差,而且这个插件不能观察带有翻译文本的 JSON 文件的变化。以下是我目前拥有的。
这是我来自webpack.common.js的代码。
const Path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const StaticI18nHtmlPlugin = require("webpack-static-i18n-html");
//...
module.exports = {
//...
plugins: [
//...
new StaticI18nHtmlPlugin({
locale: 'en',
locales: ['en', 'ua', 'ru'],
baseDir: Path.posix.join(__dirname, ".."),
outputDir: 'src/localized-pages',
outputDefault: '__lng__/__file__',
localesPath: 'src/locales',
files: 'src/templates/index.html'
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: Path.resolve(__dirname, '../src/templates/index.html')
}),
new HtmlWebpackPlugin({
filename: 'ua/index.html',
template: Path.resolve(__dirname, '../src/localized-pages/ua/src/templates/index.html')
}),
new HtmlWebpackPlugin({
filename: 'ru/index.html',
template: Path.resolve(__dirname, '../src/localized-pages/ru/src/templates/index.html')
}),
//...
],
//...
};
我还有webpack.dev.js 和webpack.prod.js,我通过webpack-merge 插件与webpack.common.js 合并。如您所见,生成页面后,我必须使用HtmlWebpackPlugin 为它们提供服务。使用起来很尴尬。
locales文件夹:
locales
|-en.json
|-ua.json
|-ru.json
en.json文件:
{
"key": {
"innerKey" : "value"
}
}
然后插件生成自:
<p data-t>key.innerKay</p>
这个
<p>value</p>
但正如我所说,如果我改变en.json,什么都不会再生。我不会使用这种方式为不同的语言生成多个页面。
所以,我想从一个模板生成多个页面。有没有办法用 webpack 做到这一点?
【问题讨论】:
标签: javascript html npm webpack internationalization