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

magic-i18n

v1.0.3

Published

magic-i18n 一键式实现项目的国际化 语言包 处理的过程

Downloads

4

Readme

vue3-i18n-webpack

一键式实现项目的国际化 语言包 处理的过程,vue2和vue3版本版本的loader


vue-i18n版本的wepback loader,给出一个简单的demo。

HTML

<div>你好,世界</div>

资源文件

//zh.js
module.exports = {
  "a018615b35588a01": "你好,世界" //key是根据中文生成16的MD5
}

//en.js
module.exports = {
  "a018615b35588a01": "Hello, world" //key是根据中文生成16的MD5
}

页面在中文下展示为

你好世界

在英文下展示为

Hello, world

介绍

该loader的主要目的是将国际化资源与项目代码分离维护,这样我们去查找对应文案的时候更加简单方便,如上述demo所示,我们原文件中不需要去使用 <div>{{ $t('a018615b35588a01') }}</div> 的方式去显示声明国际化,通过loader自动进行替换完成我们的国际化工作,这样对开发查找页面文案之类带来了极大的方便。本组件还支持在vue的props中以及单独的js文件中进行国际化处理。

安装

npm i vue3-i18n-webpack --save-dev

yarn add vue3-i18n-webpack --dev

使用

本组件分为两部分,一部分是cli,目的是为了生成资源文件,一部分是loader,目的是为了替换项目中的中文为国际化的代码,我们最好在打包测试/上线前执行以下cli命令,生成资源文件,然后拷贝一份资源文件给翻译组进行各国语言的翻译,之后将资源文件配置到[email protected]上,再进行打包即可。

cli的使用

cli可使用的所有命令

自动抓取中文

项目根目录执行

npx i18n generate [src]
对src目录下的vue/js/ts/tsx/jsx文件进行中文提取并生成国际化资源文件
默认src目录   src  默认值为src
npx i18n generate -h
Usage: i18n generate [src]

对src目录下的vue/js/ts/tsx/jsx文件进行中文提取并生成国际化资源文件,默认src为执行目录下的src目录

Options:
  -p, --filepath <filepath>  设置国际化文件的路径,默认为执行目录下的src/locale目录,请务必设置一个单独的目录来放置国际化资源文件
  -f, --filename <filename>  设置生成文件的文件名,默认为 zh,会自动添加.js 后缀
  -h, --help                 output usage information

请务必记住上述的国际化资源文件的路径和文件名,loader中需要配置,若未设置采用默认,loader中也可以不用设置

自动翻译

npx i18n init 
初始化翻译配置项(如已有配置项,不要重复执行,不然会覆盖)

npx i18n translate [code]
开始翻译文件 
默认中文翻译成英文   code  默认值为en

Options:
  -f, --file <filename>,  选择翻译的文件,默认选取目录下的src/locale目录下的文件

code必须为百度翻译文档上的code值

初始化项目,生成的配置文件 i18n-config.json

module.exports = {
    dir: "./src/locale/", // 目标目录
    file: 'zh.js', // 翻译的文件
    distLangs: ['en'], // 要翻译的语言
    open: true, // leader是否启用 默认true 打包环境下不生效
    appId:'', // 百度翻译appid
    secret:'', // 百度翻译密钥
};

对应翻译语言代码

lang

百度翻译文档:https://fanyi-api.baidu.com/product/113

人工修改翻译导入

人工审核百度翻译错误的语言包,可直接在map映射表内修改,执行以下命令重新生成语言包

npx i18n init 
初始化翻译配置项(如已有配置项,不要重复执行,不然会覆盖)

npx i18n transform
开始遍历所有map文件,并导入对应语言包内

清理语言包

npx i18n init 
初始化翻译配置项(如已有配置项,不要重复执行,不然会覆盖)

npx i18n clear 
清理语言包内已无效的值(使用频率:建议半年或一年以上,用于清理已无用的语言包)

跳过中文抓取

js文件和vue文件内被<i18n-ignore (跳过的内容) i18n-ignore> 包括的内容将不会进行抓取

使用实例 js语法

// <i18n-ignore
const str = "我的世界"
// i18n-ignore>

template内使用

<!-- <i18n-ignore -->
<div>我的世界</div>
<!-- i18n-ignore> -->

loader的使用

loader中需要对两个部分进行配置,一个是vue文件,一个是js文件。

const path = require('path');

module.exports = {
  //其他配置
  module:{
    rules:[
      {
        test: /\.vue$/,
        use: [
          {
            loader: 'vue-loader',
          },
          {
            loader: 'vue3-i18n-webpack', //一定要作为第一个loader
            options:{
              localeFile: path.join(process.cwd(), 'src/locale/zh.js') //与cli中相同,若生成的时候保持默认,则不需要配置
            }
          }
        ]
      },
      {
        test: /\.(jsx|js|ts|tsx)?$/,
        include: [resolve('src')],
        use:[
          {
            loader: 'babel-loader',
          },
          {
            loader: 'vue3-i18n-webpack', //一定要作为第一个loader
            options:{
              localeFile: path.join(process.cwd(), 'src/locale/zh.js') //与cli中相同,若生成的时候保持默认,则不需要配置
            }
          }
        ]
      },
    ]
  }
}

国际化资源配置

此处一定要注意!!!否则是个坑。

国际化的配置一定一定要在所有逻辑之前,建议采用 i18n.js 文件单独配置,然后在入口文件最先引入这个文件即可! 要确保('@/i18n')这个路径可以取到i18n对象

vue2版本

//i18n.js 国际化配置文件
import Vue from 'vue';
import VueI18n from 'vue-i18n';
Vue.use(VueI18n);
const i18n = new VueI18n({
    locale: 'zh',
    messages:{
      'zh': ...require('./locale/zh'),
    },
});
export default i18n;

//main.js 入口文件
import i18n from '@/i18n';

vue3版本

//i18n.js 国际化配置文件
import { createI18n } from "vue-i18n";

const i18n = createI18n({
    locale: "zh", //默认显示的语言
    messages:{
      'zh': ...require('./locale/zh'),
    },
});

export default i18n;

//main.js 入口文件
import i18n from '@/i18n';

注意

  • 建议字符串的连接用模板字符串方式,这样其中涉及到的一些动态参数也会自动生成 {0} {1} 这样的参数注入