简体   繁体   English

如何从一个模板创建多个不同语言的页面?

[英]How to create multiple pages with different languages from one template?

I want to generate multiple pages which will have content on different languages from one common template.我想从一个通用模板生成多个页面,这些页面将包含不同语言的内容。 How can I do it with webpack?我怎样才能用 webpack 做到这一点?

I tried to use different webpack plugins like webpack-static-i18n-html , i18n-webpack-plugin but nothing works for me.我尝试使用不同的 webpack 插件,如webpack-static-i18n-htmli18n-webpack-plugin但对我来说没有任何效果。 The best thing I found is a webpack-static-i18n-html , but it has bad support and this plugin can't watch changes in JSON files with translated text.我发现的最好的东西是webpack-static-i18n-html ,但它的支持很差,这个插件不能用翻译的文本观察 JSON 文件中的变化。 Below is what I have for now.以下是我现在所拥有的。

This is my code from webpack.common.js .这是我来自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')
        }),
        //...
    ],
    //...
};

I also have webpack.dev.js and webpack.prod.js which I merge with webpack.common.js via webpack-merge plugin.我还有webpack.dev.jswebpack.prod.js ,我通过webpack-merge插件将它们与webpack.common.js webpack-merge As you can see, after generating pages, I have to use HtmlWebpackPlugin to serve them.如您所见,生成页面后,我必须使用HtmlWebpackPlugin来为它们提供服务。 It's awkward to use.使用起来很尴尬。

locales folder: locales文件夹:

locales
|-en.json
|-ua.json
|-ru.json

en.json file: en.json文件:

{
    "key": {
        "innerKey" : "value"
    }
}

Then plugin generates from:然后插件生成:

<p data-t>key.innerKay</p>

this这个

<p>value</p>

But as I said, If I change en.json nothing will regenerate.但正如我所说,如果我改变en.json什么都不会再生。 I will not use this way to generate multiple pages for different languages.我不会使用这种方式为不同语言生成多个页面。

So, I would like to generate several pages from one template.所以,我想从一个模板生成几个页面。 Is there any way to do this with webpack?有没有办法用 webpack 做到这一点?

I was working on a multi language admin dashboard with Webpack and was wondering how could I tackle this problem until I found a way to make everything automatic with a multiple language web template.我正在使用 Webpack 开发一个多语言管理仪表板,并且想知道如何解决这个问题,直到我找到了一种使用多语言 Web 模板使一切都自动化的方法。

First of all, webpack-static-i18n-html isn't a good solution because most of its packages are deprecated.首先, webpack-static-i18n-html不是一个好的解决方案,因为它的大部分包都被弃用了。 But actually the mentioned package is based on a good npm package called node-static-i18n .但实际上提到的包是基于一个名为node-static-i18n的好的 npm 包。 So, the first thing you need to do is installing this npm package using this command所以,你需要做的第一件事就是使用这个命令安装这个 npm 包

npm install -g static-i18n

Next, you need to make your translation file as *.json files and in json format and put them in a folder which I named "locales" and I put it in my "src" folder of my project.接下来,您需要将您的翻译文件制作为 *.json 文件和 json 格式,并将它们放在我命名为“locales”的文件夹中,并将其放在我的项目的“src”文件夹中。 I need two languages for my website.我的网站需要两种语言。 One of them is English and another is Farsi or Persian.其中一个是英语,另一个是波斯语或波斯语。 Therefore I made two file namely fa.json and en.json.因此我制作了两个文件,即 fa.json 和 en.json。 So, I have folder and file structure like the picture below:所以,我有如下图所示的文件夹和文件结构:

My file and folder structure in my Webpack project我的 Webpack 项目中的文件和文件夹结构

  • This is part of my en.json file as an example:这是我的 en.json 文件的一部分,例如:

{ {

"menu": {
    "items": {
      "dashboard": "Dashboard",
      "posts": "Posts",
      "media": "Media"
    },
    "sub": {
      "items": {
        "all-posts": "All Posts",
        "add-new-post": "Add New",
        "categories": "Categories"
      }
    }
  }

} }

  • This is part of my fa.json file as an example:这是我的 fa.json 文件的一部分,例如:

{ {

"menu": {
    "items": {
      "dashboard": "پیشخوان",
      "posts": "نوشته ها",
      "media": "رسانه"
    },
    "sub": {
      "items": {
        "all-posts": "نوشته ها",
        "add-new-post": "افزودن نوشته",
        "categories": "دسته ها"
      }
    }
  }

} }

and you can use them in your html tags like this:你可以在你的 html 标签中使用它们,如下所示:

<span class="className" data-t>menu.items.dashboard</span>

Please notice that for using translation you should use the attribute data-t in your tags like span then you can use key and values saved in your related json file to use translations between your tags.请注意,要使用翻译,您应该在标签中使用属性data-t ,例如 span,然后您可以使用保存在相关 json 文件中的键和值来使用标签之间的翻译。 for more information about data-t and its usage please go to the plugin's Github page that I mentioned it earlier in this text on the plugin name.有关data-t及其用法的更多信息,请访问我在本文前面关于插件名称的文本中提到的插件的 Github 页面。

Next, you should write needed command in the script section of your package.json file to run node-static-i18n to translate your template based on your html template file and save them in i18n folder in root of your project as below:接下来,您应该在 package.json 文件的脚本部分编写所需的命令以运行node-static-i18n以根据您的 html 模板文件翻译您的模板,并将它们保存在项目根目录下的 i18n 文件夹中,如下所示:

"scripts": {

"i18n": "static-i18n -l en -i fa -i en  src --localesPath src/locales/",

} }

in the above command:在上面的命令中:

-l : The default locale. -l :默认语言环境。

-i : the list of locales to be generated. -i :要生成的语言环境列表。

--localesPath : The directory of the translations, where each file should be named LOCALE_NAME.json --localesPath : 翻译的目录,每个文件都应该命名为 LOCALE_NAME.json

Now if you run npm run i18n this command should make a folder in root path of your project called i18n containing html files in two languages in this case.现在,如果你运行npm run i18n这个命令应该在你的项目的根路径中创建一个名为i18n的文件夹,在这种情况下包含两种语言的 html 文件。 it will be like the picture below:它将如下图所示:

i18n folder and translated html files in it i18n 文件夹和其中的翻译 html 文件

Next you should config your Html Webpack Plugin in your Webpack config file to show these pages in your browser like this:接下来,您应该在您的 Webpack 配置文件中配置您的 Html Webpack 插件,以在您的浏览器中显示这些页面,如下所示:

    plugins: [
.
.
.
new HtmlWebpackPlugin({
  //inject: false,
  chunks: ['main'],
  template: 'i18n/index.html',
  filename: 'index.html'
}),
new HtmlWebpackPlugin({
  //inject: false,
  chunks: ['main-rtl'],
  template: 'i18n/fa/index.html',
  filename: 'fa/index.html'
})

] ]

because you need to see changes on your browser automatically you need another package called npm-watch to install through this command:因为您需要自动查看浏览器上的更改,所以您需要另一个名为npm-watch 的包通过以下命令安装:

npm install -D npm-watch

Then, you should change script section of your package.json like this:然后,您应该像这样更改 package.json 的脚本部分:

"scripts": {
    "i18n-watch": "watch 'npm run i18n' src",
    "i18n": "static-i18n -l en -i fa -i en  src --localesPath src/locales/",
  }

By using the command npm run i18n-watch whenever you make any changes in your locale files or your original html template in src folder it's gonna re-translate your html file based on new information and if you're running your webpack dev server you can see the result right after you save changes.通过使用命令npm run i18n-watch每当您对 src 文件夹中的区域设置文件或原始 html 模板进行任何更改时,它都会根据新信息重新翻译您的 html 文件,如果您正在运行 webpack 开发服务器,则可以保存更改后立即查看结果。

After that, to run i18n-watch command and your Webpack dev server at the same time it would be great installing another npm package for this purpose called npm-run-all by using the command below:之后,要同时运行i18n-watch命令和 Webpack 开发服务器,最好使用以下命令安装另一个名为npm-run-all的 npm 包:

npm i -D npm-run-all

Finally, you can change the script section of your package.json like this to run i18n-watch and your Webpack dev server at the same time and after that if you make any changes you can see the result in the browser right after saving changes.最后,您可以像这样更改 package.json 的脚本部分以同时运行i18n-watch和您的Webpack dev server ,之后如果您进行任何更改,您可以在保存更改后立即在浏览器中看到结果。

"scripts": {
    "i18n-watch": "watch 'npm run i18n' src",
    "i18n": "static-i18n -l en -i fa -i en  src --localesPath src/locales/",
    "webpack-dev": "webpack-dev-server --open --config=config/webpack.dev.js",

    "start": "npm-run-all --parallel webpack-dev i18n-watch"
  }

Now, if you use npm start in your terminal you will see your Webpack dev server and i18n-watch are running at the same time watching for any changes.现在,如果您在终端中使用npm start ,您将看到您的Webpack dev serveri18n-watch同时运行,以观察任何更改。

Hopefully this makes sense.希望这是有道理的。

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

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