npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fis3-deploy-i18n-ejs

v2.0.5

Published

A fis3 plugin to generate intl file with i18n language file and template file on deploy stage.

Downloads

13

Readme

fis3-deploy-i18n-ejs

A FIS3 plugin to generate html file with i18n data on deploy stage.

npm Build Status semantic-release tested with jest license

Thanks for fis3-deploy-i18n-template

在前端的工程构建工具FIS3发布阶段,将所有拥有isHtmlLike: true的文件包含的多语言标记替换成指定内容,并生成文件的插件。

使用说明

如何安装

yarn add fis3-deploy-i18n-ejs -D
# OR
npm install fis3-deploy-i18n-ejs -D

默认配置

/**
 * @type {Object} DEFAULT_CONFIG - 插件默认配置
 * @property {string} [open='<%'] - ejs模板的起始标识符
 * @property {string} [close='%>'] - ejs模板的结束标识符
 * @property {string} [dist='i18n/$lang/$file'] - 编译后的输出路径,相对于release的根目录,其中`$lang`代表语言文件夹,`$file`代表编译的文件
 * @property {string} [templatePattern=''] - 需要做多语言处理文件subpath的glob规则,默认为所有html文件
 * @property {string} [defaultLangName=''] - 默认语言名字,如果匹配默认语言,该语言的输出将自动去除dist中的`$lang`部分
 * @property {string} [i18nPattern='translations/*.{js,json}'] - 多语言文件的glob规则
 * @property {string} [ignorePattern=''] - 需要忽略编译的glob规则
 * @property {string} [noKeepSubPathPattern=''] - 不需要保持原有目录结构输出的glob规则
 * @property {function} [onLangFileParse=noop] - 在读取多语言文件后的自定义处理函数,其返回值会与当前读取的文件内容合并
 */
{
  open: '<%',
  close: '%>',
  dist: 'i18n/$lang/$file',
  templatePattern: '',
  defaultLangName: '',
  i18nPattern: 'translations/*.{js,json}',
  ignorePattern: '',
  noKeepSubPathPattern: '',
  /**
   * @desc 多语言文件处理函数
   * @param {string} i18nFileJSONClone - 语言文件内容(JSON格式)的拷贝对象
   * @param {string} fileLangName - 当前语言文件对应的语言名字
   * @param {string} defaultLangName - 默认语言名字
   * @return {object} 处理以后的文件内容
   *
   * @example
   * onLangFileParse(i18nFileJSONClone, fileLangName, defaultLangName) {},
   */
  onLangFileParse: noop,
}

参考示例

具体的实验可以参考这个项目fis3-examples

项目目录结构

# project root path
│
├── i18n-folder # 文件名及语言名
│   ├── en.json
│   ├── zh.json
│   └── ...
│
├── template-folder
│   ├── index.html
│   ├── ...
│   └── sub-folder
│       ├── detail.html
│       └── ...
│
├── fis-conf.js
│
└── package.json

配置fis-conf.jsfis3-deploy-i18n-ejs相关的内容

// ------ translations ------
fis.match('/i18n-folder/**', {
  release: false,
});
// ------ templates ------
fis.match('/template-folder/(**)/(*.html)', {
  release: '/$1/$2',
});
// ------ deploy ------
fis.match('**', {
  deploy: [
    fis.plugin('i18n-folder', {
      open: '<%',
      close: '%>',
      dist: '$lang/$file',
      defaultLangName: 'zh',
      templatePattern: '',
      i18nPattern: 'i18n-folder/*.json',
      ignorePattern: '',
      noKeepSubPathPattern: '',
      onLangFileParse() {},
    }),
  ],
});

i18n-folder中的文件内容示例

i18n-folder/en.json

{
    "hello": "hello",
    "world": "world"
}

i18n-folder/zh.json

{
    "hello": "你好",
    "world": "世界"
}

template-folder待转换的模板文件夹

template-folder/index.html

<html>
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <p><%= hello %></p>
    <p><%= world %></p>
</body>
</html>

template-folder/sub-floder/detail.html

<html>
<head>
    <meta charset="UTF-8">
    <title>detail</title>
</head>
<body>
    <p><%= hello %></p>
    <p><%= world %></p>
</body>
</html>

输出结果

语言为zh的输出结果:

默认语言为zh,所以输出的文件去除$lang层级的目录,即

  • dist/index.html
<html>
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <p>你好</p>
    <p>世界</p>
</body>
</html>
  • dist/sub-folder/detail.html
<html>
<head>
    <meta charset="UTF-8">
    <title>detail</title>
</head>
<body>
    <p>你好</p>
    <p>世界</p>
</body>
</html>

语言为en的输出结果:

  • dist/en/index.html
<html>
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <p>hello</p>
    <p>world</p>
</body>
</html>
  • dist/en/sub-folder/detail.html
<html>
<head>
    <meta charset="UTF-8">
    <title>detail</title>
</head>
<body>
    <p>hello</p>
    <p>world</p>
</body>
</html>

参考

  • node-project-kit - 快速创建项目的模板
  • glob - 使用 glob 语法获取匹配文件的工具
  • ejs - 模板引擎

License

Copyright © 2017-present. This source code is licensed under the MIT license found in the LICENSE file.


Made by Tony (blog)