GithubHelp home page GithubHelp logo

sjx1995 / unplugin-i18n-translation Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 46 KB

🔠 一个支持多打包工具的自动翻译国际化文件的插件 / A auto translate i18n files for unified plugin

License: MIT License

JavaScript 12.44% TypeScript 87.56%

unplugin-i18n-translation's Introduction

unplugin-i18n-translation

简体中文 | English

介绍

一个支持多打包工具的自动翻译国际化文件的插件,使用 unplugin 支持ViteWebpackRollupESBuildRspack等打包工具

将国际化插件的本地语言文件翻译并生成其它语言的翻译文件

Vue2项目推荐使用vue-18n

Vue3项目推荐使用vue-i18n-next

在线翻译由 腾讯机器翻译 提供

必需

在使用之前请确保已经安装并配置了国际化插件(比如vue-18n),而且已经有一个默认语言的本地化文件作为翻译的源文件

此外,还需要申请一个腾讯云API密匙

安装

# npm
npm install @sunly95/unplugin-i18n-translation --save-dev

# yarn
yarn add @sunly95/unplugin-i18n-translation --dev

使用

webpack

webpack.config.js中配置插件,传入的配置参数详情见配置

// webpack.config.js

// 引入插件
const { UnpluginAutoTranslation } = require("@sunly95/unplugin-i18n-translation");

module.exports = {
  plugins: [
    // 注册插件
    UnpluginAutoTranslation.webpack({
      id: secretId, // 腾讯云API密钥ID
      key: secretKey,  // 腾讯云API密钥Key
      originLang: "zh",  // 源语言
      targetLangs: [
        { lang: "en", filename: "en-US" },
      ], // 要翻译的语言列表
      originFilePath: "./src/locales/zh-CN.json", // 翻译原始文本文件,即vue-i18n的本地化文件
      targetDirPath: "./src/locales"  // 翻译后的文件存放目录
    })
  ]
}

vue-cli

vue.config.js中配置插件,传入的配置参数详情见配置

// vue.config.js

// 引入插件
const { UnpluginAutoTranslation } = require("@sunly95/unplugin-i18n-translation");

module.exports = {
  // 注册插件
  config.plugin("AutoTranslation").use(
    UnpluginAutoTranslation.webpack({
      id: secretId, // 腾讯云API密钥ID
      key: secretKey,  // 腾讯云API密钥Key
      originLang: "zh",  // 源语言
      targetLangs: [
        { lang: "en", filename: "en-US" },
      ], // 要翻译的语言列表
      originFilePath: "./src/locales/zh-CN.json", // 翻译原始文本文件,即vue-i18n的本地化文件
      targetDirPath: "./src/locales"  // 翻译后的文件存放目录
    })
  );
}

Vite

vite.config.js中配置插件,传入的配置参数详情见配置

// vite.config.js

// 引入插件
import { UnpluginAutoTranslation } from "@sunly95/unplugin-i18n-translation";

export default defineConfig({
  plugins: [
    UnpluginAutoTranslation.vite({
      id: secretId, // 腾讯云API密钥ID
      key: secretKey,  // 腾讯云API密钥Key
      originLang: "zh",  // 源语言
      targetLangs: [
        { lang: "en", filename: "en-US" },
      ], // 要翻译的语言列表
      originFilePath: "./src/locales/zh-CN.json", // 翻译原始文本文件,即vue-i18n的本地化文件
      targetDirPath: "./src/locales"  // 翻译后的文件存放目录
    })
  ]
});

配置完成后,在打包时会自动读取originFilePath指定的文件作为翻译的原始文本,根据传入的targetLangs自动翻译并生成翻译文件,存放在targetDirPath指定的目录下

配置

参数 类型 是否必需 说明
id string 腾讯云API密匙ID
key string 腾讯云API密匙key
originLang string 原始翻译文本语言,取值见输入参数的Source字段
targetLangs { lang: string, filename: string }[] 需要翻译成的目标语言列表,lang字段表示要翻译成的语言,取值见输入参数的Target字段filename字段表示翻译后生成的i18n文件的文件名
originFilePath string 原始翻译文本的绝对路径
targetDirPath string 翻译后生成的目标翻译文件的存储目录
spaceWidth number? 翻译后后生成的json文件的缩进空格数,默认为2

更新日志

CHANGELOG

unplugin-i18n-translation's People

Contributors

sjx1995 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.