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

wp-i18n-plugin

v1.2.3

Published

wp-i18n-plugin

Downloads

29

Readme

🎉🎉 前端多语言编译处理解决方案 wp-i18n-plugin

  • 基于webpack4 AST分析多语言生成插件, 支持Vue, React, Angular
  • 自动管理语言配置, 在编译期后直接生成多语言拆分, 无需在客户端重新渲染
  • 项目打包后根据配置的多语言数量生成多分具有单一语言的文件. 例如使用zh-cn, en则最后输出将会有
/dist/zh-cn
/dist/en

👏👏安装

npm install webpack webpack-cli wp-i18n-plugin --save-dev

⚙ webpack.config.js

const WebpackI18n = require('wp-i18n-plugin'),
  path = require('path'),
  _ = require('lodash');

let webpackConfig = {
  name: '',
  mode: 'development' || 'production',
  entry: './example/example.js',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'output.js'
  },
  plugins: [
    // 多语言处理插件
    new WebpackI18n.Plugin({
      sourcePath: './example/lang.json',
      // 自动新增i18n对象语言列表
      languageList: ['zh-cn', 'en'],
      // 设置默认将对于key设置为zh-cn的值
      useLanguage: 'zh-cn',
      // 是否根据内容自动生成对象
      autoWriteAble: true,
      // 格式化对象空格数
      formatSpace: 2,
      // 是否展示未翻译的项
      showDetail: true,
      // 自动移除无用的key
      removeUnUseKeys: true
    }),
    // 后端返回code对应多语言文件拆分
    new WebpackI18n.Spliter({
      // 输出文件路径名称
      outputName: './example/async-lang.json',
      // 语言包路径
      sourcePath: './example/async-lang.json'
    })
  ]
};

📦webpack多编译设置(使用node api 或者 webpack config中的一种)

  • node api 启动配置方式
webpackConfigArr = ['zh-cn', 'en'].map(language => {
  webpackConfig = _.cloneDeep(webpackConfig);
  // 配置当前编译参数
  webpackConfig.name = language;
  // 配置输出路径
  webpackConfig.output.path = path.join(__dirname, 'dist') + '/' + language;
  return webpackConfig;
});
const webpack = require('webpack');
webpack(webpackConfigArr, (err, stats) => {
  err && console.log(err);
});
  • webpack config 启动配置方式
module.exports = ['zh-cn', 'en'].map((language) => {
  webpackConfig = _.cloneDeep(webpackConfig);
	// 配置当前编译参数
  webpackConfig.name = language;
  // 配置输出路径
  webpackConfig.output.path = path.join(__dirname, 'dist') + '/' + language;
  return webpackConfig;
});

🌰例子

example.js

console.log('__("你好")');

lang.json

{
  "你好": {
    "zh-cn": "你好",
    "en": "Hello"
  }
}

🐭example.js编译后输出结果:

  • dist/en/output.js
console.log('Hello');
  • dist/zh-cn/output.js
console.log('你好');

🖖 关于编译性能提升

  • 建议使用在项目中加入cache-loader进行缓存以提高效率
  • 建议开发时只启动单个语言进行开发, 以降低编译次数以达到快速编译的效果