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

i18n-auto-react

v0.0.10

Published

基于百度翻译API服务的自动翻译插件

Downloads

41

Readme

i18n-auto-react

  • 基于 百度翻译API服务 的自动翻译工具
  • 只需要书写中文,即可自动翻译成其他语言
  • 使用前请去 百度翻译开放平台 申请 通用文本翻译服务

安装

npm install i18n-auto-react
# or
yarn add i18n-auto-react
# or
pnpm install i18n-auto-react

使用

1. 生成语言包

  1. 执行npx i18n init 初始化配置 i18n.config.js
  2. 申请百度翻译API服务后,在 i18n.config.js 配置appId和密钥
  3. 执行 npm run i18n:translate 扫描文件中的中文并翻译成语言包
  4. 执行 npm run i18n:genExport 生成语言包导出文件

2. 添加插件

webpack 项目

module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        loader: 'i18n-auto-react/webpack',
        options: require('../i18n.config.js') // 路径以实际情况为准
      },
      // ...other loader
    ]
  }
}

vite 项目

// vite.config.js
import { i18nAutoPlugin } from 'i18n-auto-react/vite'

export default defineConfig({
  plugins: [
    // ...other plugin
    i18nAutoPlugin()
  ]
})

效果展示

  • 插件的作用是把文件中的中文自动替换为翻译函数调用
  • 带有注释的 (aa, bb),或者本来就有翻译函数包裹 (cc) 的会忽略翻译

转换前

import React from 'react'
import { i18n as _i18n } from 'i18n-auto-react'

let world = '世界'
// i18n-disable-next
let aa = '我是被忽略翻译的中文'
let bb = '我也是被忽略翻译的中文' // i18n-disable
let cc = _i18n('我也也是被忽略翻译的中文')

export default function App() {
  return (
    <div>
      <h3 title="花飘万家雪">你好{world}</h3>
      <h3>{aa + bb + cc}</h3>
    </div>
  )
}

转换后

import React from 'react';
// 如当前页面有需要翻译的中文时,会自动引入
import { i18n as _i18n } from "i18n-auto-react";

let world = _i18n("c086b3008aca0efa8f2ded065d6afb50");
// i18n-disable-next
let aa = '我是被忽略翻译的中文';
let bb = '我也是被忽略翻译的中文'; // i18n-disable
let cc = _i18n('我也也是被忽略翻译的中文')

export default function App() {
  return (
    <div>
      <h3 title={_i18n("29fd4016d2b8d06be750109579b7301e")}>
        {_i18n("7eca689f0d3389d9dea66ae112e5cfd7")}{world}
      </h3>
      <h3>{aa + bb + cc}</h3>
    </div>
  )
}

3. 切换语言

import React from 'react'
import { changeLanguage, currentLanguage } from 'i18n-auto-react'

export default function App() {
  return (
    <div>
      <button
        onClick={() => {
          // 获取当前语言
          let currLng = currentLanguage()
          // 切换语言
          changeLanguage(currLng == 'zh' ? 'en' : 'zh')
        }}
      >
        切换语言
      </button>
    </div>
  )
}

4. 其他未完成项

  • 运行时代码需要分包