From 813ac0a83f7e2fdf9433627beee20114b00aa414 Mon Sep 17 00:00:00 2001 From: Eyelly Wu Date: Tue, 2 Apr 2024 21:59:42 +0800 Subject: [PATCH] feat(bin): add support for i18nrc.ts --- docs/dist/CHANGELOG.md | 12 + docs/dist/CHANGELOG_zh-CN.md | 12 + docs/dist/COMMAND_LINE.md | 6 +- docs/dist/COMMAND_LINE_zh-CN.md | 6 +- docs/dist/USAGE.md | 15 +- docs/dist/USAGE_zh-CN.md | 15 +- docs/src/changelog/index.tsx | 22 + docs/src/commandline/index.tsx | 4 +- docs/src/i18nrc.ts | 14 + docs/src/usage/index.tsx | 10 +- package.json | 1 + pnpm-lock.yaml | 6056 ++++++++++++------------------- src/bin/config.ts | 83 +- src/bin/constants.ts | 6 +- src/bin/fetch.ts | 3 +- src/bin/index.ts | 2 +- src/lib/index.ts | 2 +- src/lib/type.ts | 2 +- src/type.ts | 15 +- template/i18nrc.ts | 21 + test/bin/config.test.ts | 140 +- vitest.config.ts | 1 + 22 files changed, 2701 insertions(+), 3747 deletions(-) create mode 100644 docs/src/i18nrc.ts create mode 100644 template/i18nrc.ts diff --git a/docs/dist/CHANGELOG.md b/docs/dist/CHANGELOG.md index cd2e36a..026cc6c 100644 --- a/docs/dist/CHANGELOG.md +++ b/docs/dist/CHANGELOG.md @@ -8,6 +8,9 @@ English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/
Table of Contents +   [[2.2.0] - 2024-04-xx](#220---2024-04-xx)
+     [Command Line Tool](#220-command-line-tool)
+       [Added](#220-command-line-tool-added)
  [[2.1.0] - 2023-10-12](#210---2023-10-12)
    [Command Line Tool](#210-command-line-tool)
      [Added](#210-command-line-tool-added)
@@ -68,6 +71,15 @@ English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/
+## [2.2.0] - 2024-04-xx + +

Command Line Tool

+ +

Added

+ +* 命令行配置文件支持用 `TypeScript` 来编写,从版本 `v2.2.0` 开始,初始化命令执行生成的配置文件默认是 `i18nrc.ts` ,但老版的 `i18nrc.js` 依旧是兼容的 + + ## [2.1.0] - 2023-10-12

Command Line Tool

diff --git a/docs/dist/CHANGELOG_zh-CN.md b/docs/dist/CHANGELOG_zh-CN.md index 8fdcc59..92d2811 100644 --- a/docs/dist/CHANGELOG_zh-CN.md +++ b/docs/dist/CHANGELOG_zh-CN.md @@ -8,6 +8,9 @@
目录 +   [[2.2.0] - 2024-04-xx](#220---2024-04-xx)
+     [命令行工具](#220-命令行工具)
+       [Added](#220-命令行工具-added)
  [[2.1.0] - 2023-10-12](#210---2023-10-12)
    [命令行工具](#210-命令行工具)
      [Added](#210-命令行工具-added)
@@ -68,6 +71,15 @@
+## [2.2.0] - 2024-04-xx + +

命令行工具

+ +

Added

+ +* 命令行配置文件支持用 `TypeScript` 来编写,从版本 `v2.2.0` 开始,初始化命令执行生成的配置文件默认是 `i18nrc.ts` ,但老版的 `i18nrc.js` 依旧是兼容的 + + ## [2.1.0] - 2023-10-12

命令行工具

diff --git a/docs/dist/COMMAND_LINE.md b/docs/dist/COMMAND_LINE.md index 685037b..c55d56f 100644 --- a/docs/dist/COMMAND_LINE.md +++ b/docs/dist/COMMAND_LINE.md @@ -4,7 +4,7 @@
Table of Contents -   [1. `i18nrc.js` Configuration](#1--i18nrcjs--configuration)
+   [1. `i18nrc.ts` Configuration](#1--i18nrcts--configuration)
    [Basic Configuration](#basic-configuration)
    [Output](#output)
    [GooglexConfig](#googlexconfig)
@@ -21,7 +21,7 @@
-## 1. `i18nrc.js` Configuration +## 1. `i18nrc.ts` Configuration ### Basic Configuration @@ -161,4 +161,4 @@ Configuration of Alibaba Cloud Translation |:-:|:-:|:-:|:-|:-|:-| |--locale|-L|`en` \| `zh`|ALL|`npx i18n h -L en`
`npx i18n h --locale en`|Specify the command line display language

The available languages are Chinese (zh)/English (en). The default is English (en)| |--non-incremental|-|-|`t`
`translate`|`npx i18n t --non-incremental`|Turn off incremental translation mode

⚠️⚠️⚠️: After turning off incremental translation mode, all texts will be re-translated, which may cause the loss of non-platform translated texts (**Manual translation**). Please use with caution!!!| -|--path|-P|-|`init`
`t`
`translate`|`npx i18n init -P /xxx/xxx/xxx`
`npx i18n t -P /xxx/xxx/xxx`|Specify the configuration file path (parameter is relative path)

You only need to specify the path name. The default configuration filename is `i18nrc.js`| +|--path|-P|-|`init`
`t`
`translate`|`npx i18n init -P /xxx/xxx/xxx`
`npx i18n t -P /xxx/xxx/xxx`|Specify the configuration file path (parameter is relative path)

You only need to specify the path name. The default configuration filename is `i18nrc.ts`| diff --git a/docs/dist/COMMAND_LINE_zh-CN.md b/docs/dist/COMMAND_LINE_zh-CN.md index 7c56720..160a138 100644 --- a/docs/dist/COMMAND_LINE_zh-CN.md +++ b/docs/dist/COMMAND_LINE_zh-CN.md @@ -4,7 +4,7 @@
目录 -   [1. `i18nrc.js` 配置](#1--i18nrcjs-配置)
+   [1. `i18nrc.ts` 配置](#1--i18nrcts-配置)
    [基础配置](#基础配置)
    [Output](#output)
    [GooglexConfig](#googlexconfig)
@@ -21,7 +21,7 @@
-## 1. `i18nrc.js` 配置 +## 1. `i18nrc.ts` 配置 ### 基础配置 @@ -161,4 +161,4 @@ OpenAI翻译的配置 |:-:|:-:|:-:|:-|:-|:-| |--locale|-L|`en` \| `zh`|ALL|`npx i18n h -L en`
`npx i18n h --locale en`|指定命令行显示语言

可选语言有中文(zh)/ 英文(en), 默认为英文(en)| |--non-incremental|-|-|`t`
`translate`|`npx i18n t --non-incremental`|关闭增量翻译模式

⚠️⚠️⚠️:关闭增量翻译模式后,所有的文案会重新翻译,会导致**手工翻译**(非翻译平台翻译的)的文案丢失,需慎重考虑使用!!!| -|--path|-P|-|`init`
`t`
`translate`|`npx i18n init -P /xxx/xxx/xxx`
`npx i18n t -P /xxx/xxx/xxx`|指定配置文件路径(参数为相对路径)

只需要指定路径名,配置文件名默认为 `i18nrc.js`| +|--path|-P|-|`init`
`t`
`translate`|`npx i18n init -P /xxx/xxx/xxx`
`npx i18n t -P /xxx/xxx/xxx`|指定配置文件路径(参数为相对路径)

只需要指定路径名,配置文件名默认为 `i18nrc.ts`| diff --git a/docs/dist/USAGE.md b/docs/dist/USAGE.md index 775f149..bdf1572 100644 --- a/docs/dist/USAGE.md +++ b/docs/dist/USAGE.md @@ -10,7 +10,7 @@     [Importing i18n.js in the Project Entry File](#importing-i18njs-in-the-project-entry-file)
    [Wrap with `t` `Translation Text` ](#wrap-with--t---translation-text)
  [3. Initialize Command Line Configuration File](#3-initialize-command-line-configuration-file)
-   [4. Adjust `i18nrc.js` Configuration](#4-adjust--i18nrcjs--configuration)
+   [4. Adjust `i18nrc.ts` Configuration](#4-adjust--i18nrcts--configuration)
  [5. Execute Translation Command](#5-execute-translation-command)
  [6. Importing Language Pack](#6-importing-language-pack)
  [7. Switch Language](#7-switch-language)
@@ -86,11 +86,12 @@ Enter the following command at the command line terminal, [more commands](https: ```bash npx i18n init ``` -After the command execution is successful, a `i18nrc.js` file will be generated in the current directory. The default configuration will be as follows: +After the command execution is successful, a `i18nrc.ts` file will be generated in the current directory. The default configuration will be as follows: ```js -const { join } = require('path') +import { join } from 'path' +import { Config } from 'i18n-pro' -module.exports = { +export default { funcName: 't', entry: join(__dirname, './src/'), fileRegExp: /\.[jt]s$/, @@ -107,12 +108,12 @@ module.exports = { }, // proxy: 'http://127.0.0.1:1087', }, -} +} as Config ``` -## 4. Adjust `i18nrc.js` Configuration -Adjust the configuration items in the configuration file according to the requirements, [Description](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE.md#1--i18nrcjs--configuration) of configuration items +## 4. Adjust `i18nrc.ts` Configuration +Adjust the configuration items in the configuration file according to the requirements, [Description](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE.md#1--i18nrcts--configuration) of configuration items ## 5. Execute Translation Command diff --git a/docs/dist/USAGE_zh-CN.md b/docs/dist/USAGE_zh-CN.md index 7dd11b8..838054e 100644 --- a/docs/dist/USAGE_zh-CN.md +++ b/docs/dist/USAGE_zh-CN.md @@ -10,7 +10,7 @@     [项目入口文件引入 i18n.js](#项目入口文件引入-i18njs)
    [用 `t` 包裹 `翻译文案` ](#用-t-包裹-翻译文案)
  [3. 初始化命令行配置文件](#3-初始化命令行配置文件)
-   [4. 调整 `i18nrc.js` 配置](#4-调整-i18nrcjs-配置)
+   [4. 调整 `i18nrc.ts` 配置](#4-调整-i18nrcts-配置)
  [5. 执行翻译命令](#5-执行翻译命令)
  [6. 引入语言包](#6-引入语言包)
  [7. 切换语言](#7-切换语言)
@@ -86,11 +86,12 @@ const text = t('hello world') ```bash npx i18n init ``` -命令执行成功后会在当前目录下生成一个 `i18nrc.js` 的文件,默认配置如下: +命令执行成功后会在当前目录下生成一个 `i18nrc.ts` 的文件,默认配置如下: ```js -const { join } = require('path') +import { join } from 'path' +import { Config } from 'i18n-pro' -module.exports = { +export default { funcName: 't', entry: join(__dirname, './src/'), fileRegExp: /\.[jt]s$/, @@ -107,12 +108,12 @@ module.exports = { }, // proxy: 'http://127.0.0.1:1087', }, -} +} as Config ``` -## 4. 调整 `i18nrc.js` 配置 -根据需求自行调整配置文件中的配置项,配置项的[说明](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE_zh-CN.md#1--i18nrcjs-配置) +## 4. 调整 `i18nrc.ts` 配置 +根据需求自行调整配置文件中的配置项,配置项的[说明](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE_zh-CN.md#1--i18nrcts-配置) ## 5. 执行翻译命令 diff --git a/docs/src/changelog/index.tsx b/docs/src/changelog/index.tsx index d3052ab..3a93741 100644 --- a/docs/src/changelog/index.tsx +++ b/docs/src/changelog/index.tsx @@ -435,6 +435,27 @@ function V_2_1_0() { ) } +function V_2_2_0() { + return ( +