rsbuild-auto-i18n-plugin 1.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,144 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var autoI18nPluginCore = require('auto-i18n-plugin-core');
6
+ var babel = require('@babel/core');
7
+
8
+ function _interopNamespaceDefault(e) {
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n.default = e;
22
+ return Object.freeze(n);
23
+ }
24
+
25
+ var babel__namespace = /*#__PURE__*/_interopNamespaceDefault(babel);
26
+
27
+ const allowedExtensions = ['.vue', '.ts', '.js', '.tsx', '.jsx'];
28
+ /**
29
+ * Rsbuild 自动翻译插件
30
+ * @param optionInfo 插件配置选项
31
+ * @returns Rsbuild 插件对象
32
+ */
33
+ function rsbuildPluginsAutoI18n(optionInfo) {
34
+ const name = 'rsbuild-auto-i18n-plugin';
35
+ // 初始化插件配置
36
+ autoI18nPluginCore.initOption(optionInfo);
37
+ // 配置验证失败时返回空插件
38
+ if (!autoI18nPluginCore.checkOption()) {
39
+ return {
40
+ name,
41
+ setup() {
42
+ // 空的 setup 函数
43
+ }
44
+ };
45
+ }
46
+ // 初始化语言文件和翻译对象
47
+ autoI18nPluginCore.fileUtils.initLangFile();
48
+ const originLangObj = autoI18nPluginCore.fileUtils.getLangObjByJSONFileWithLangKey(autoI18nPluginCore.option.originLang);
49
+ autoI18nPluginCore.translateUtils.languageConfigCompletion(originLangObj);
50
+ autoI18nPluginCore.translateUtils.initLangObj(originLangObj);
51
+ const plugin = {
52
+ name,
53
+ setup(api) {
54
+ // 存储配置信息
55
+ let isDevMode = false;
56
+ api.onBeforeStartDevServer(() => {
57
+ isDevMode = true;
58
+ });
59
+ // 创建动态正则表达式匹配所有支持的文件扩展名
60
+ const extensionsPattern = new RegExp(`\\.(${[...allowedExtensions, ...(autoI18nPluginCore.option.insertFileExtensions || [])].map(ext => ext.slice(1)).join('|')})$`);
61
+ api.transform({
62
+ test: extensionsPattern,
63
+ order: 'post'
64
+ }, async ({
65
+ code,
66
+ resourcePath
67
+ }) => {
68
+ // 白名单检查
69
+ if (autoI18nPluginCore.option.includePath.length && !autoI18nPluginCore.baseUtils.checkAgainstRegexArray(resourcePath, autoI18nPluginCore.option.includePath)) {
70
+ return {
71
+ code
72
+ };
73
+ }
74
+ // 黑名单检查
75
+ if (autoI18nPluginCore.option.excludedPath.length && autoI18nPluginCore.baseUtils.checkAgainstRegexArray(resourcePath, autoI18nPluginCore.option.excludedPath)) {
76
+ return {
77
+ code
78
+ };
79
+ }
80
+ // 设置源语言
81
+ autoI18nPluginCore.FunctionFactoryOption.originLang = autoI18nPluginCore.option.originLang;
82
+ // 处理扩展功能
83
+ let sourceObj;
84
+ if (autoI18nPluginCore.option.translateExtends) {
85
+ sourceObj = await autoI18nPluginCore.option.translateExtends?.handleInitFile(code, resourcePath);
86
+ } else {
87
+ sourceObj = {
88
+ source: code
89
+ };
90
+ }
91
+ try {
92
+ // 使用 Babel 进行 AST 转换
93
+ const result = await babel__namespace.transformAsync(sourceObj.source, {
94
+ configFile: false,
95
+ plugins: [autoI18nPluginCore.filter.default(sourceObj)]
96
+ });
97
+ // 开发模式下执行实时翻译
98
+ if (isDevMode) {
99
+ autoI18nPluginCore.translateUtils.autoTranslate(); // 执行前需要确保transformAsync已经完成
100
+ }
101
+ return {
102
+ code: result?.code || code
103
+ };
104
+ } catch (e) {
105
+ console.error('Rsbuild auto-i18n plugin transform error:', e);
106
+ return {
107
+ code
108
+ };
109
+ }
110
+ });
111
+ // 构建开始前
112
+ api.onBeforeBuild(() => {
113
+ console.info('Rsbuild 构建阶段批量翻译开始');
114
+ });
115
+ // 构建结束后
116
+ api.onAfterBuild(async () => {
117
+ console.info('构建阶段批量翻译');
118
+ await autoI18nPluginCore.translateUtils.autoTranslate();
119
+ });
120
+ // 构建完全结束
121
+ api.onCloseBuild(async () => {
122
+ autoI18nPluginCore.translateUtils.cleanupUnusedTranslations();
123
+ // 翻译配置写入主文件
124
+ await autoI18nPluginCore.fileUtils.buildSetLangConfigToIndexFile();
125
+ console.info('翻译完成✔');
126
+ });
127
+ // 开发服务器关闭时也需要清理
128
+ api.onCloseDevServer(async () => {
129
+ autoI18nPluginCore.translateUtils.cleanupUnusedTranslations();
130
+ await autoI18nPluginCore.fileUtils.buildSetLangConfigToIndexFile();
131
+ console.info('开发服务器关闭,翻译完成✔');
132
+ });
133
+ }
134
+ };
135
+ return plugin;
136
+ }
137
+
138
+ exports.default = rsbuildPluginsAutoI18n;
139
+ Object.keys(autoI18nPluginCore).forEach(function (k) {
140
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
141
+ enumerable: true,
142
+ get: function () { return autoI18nPluginCore[k]; }
143
+ });
144
+ });
@@ -0,0 +1,12 @@
1
+ import { OptionInfo } from 'auto-i18n-plugin-core';
2
+ export * from 'auto-i18n-plugin-core';
3
+ import { RsbuildPlugin } from '@rsbuild/core';
4
+
5
+ /**
6
+ * Rsbuild 自动翻译插件
7
+ * @param optionInfo 插件配置选项
8
+ * @returns Rsbuild 插件对象
9
+ */
10
+ declare function rsbuildPluginsAutoI18n(optionInfo: OptionInfo): RsbuildPlugin;
11
+
12
+ export { rsbuildPluginsAutoI18n as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,116 @@
1
+ import { initOption, checkOption, fileUtils, option, translateUtils, baseUtils, FunctionFactoryOption, filter } from 'auto-i18n-plugin-core';
2
+ export * from 'auto-i18n-plugin-core';
3
+ import * as babel from '@babel/core';
4
+
5
+ const allowedExtensions = ['.vue', '.ts', '.js', '.tsx', '.jsx'];
6
+ /**
7
+ * Rsbuild 自动翻译插件
8
+ * @param optionInfo 插件配置选项
9
+ * @returns Rsbuild 插件对象
10
+ */
11
+ function rsbuildPluginsAutoI18n(optionInfo) {
12
+ const name = 'rsbuild-auto-i18n-plugin';
13
+ // 初始化插件配置
14
+ initOption(optionInfo);
15
+ // 配置验证失败时返回空插件
16
+ if (!checkOption()) {
17
+ return {
18
+ name,
19
+ setup() {
20
+ // 空的 setup 函数
21
+ }
22
+ };
23
+ }
24
+ // 初始化语言文件和翻译对象
25
+ fileUtils.initLangFile();
26
+ const originLangObj = fileUtils.getLangObjByJSONFileWithLangKey(option.originLang);
27
+ translateUtils.languageConfigCompletion(originLangObj);
28
+ translateUtils.initLangObj(originLangObj);
29
+ const plugin = {
30
+ name,
31
+ setup(api) {
32
+ // 存储配置信息
33
+ let isDevMode = false;
34
+ api.onBeforeStartDevServer(() => {
35
+ isDevMode = true;
36
+ });
37
+ // 创建动态正则表达式匹配所有支持的文件扩展名
38
+ const extensionsPattern = new RegExp(`\\.(${[...allowedExtensions, ...(option.insertFileExtensions || [])].map(ext => ext.slice(1)).join('|')})$`);
39
+ api.transform({
40
+ test: extensionsPattern,
41
+ order: 'post'
42
+ }, async ({
43
+ code,
44
+ resourcePath
45
+ }) => {
46
+ // 白名单检查
47
+ if (option.includePath.length && !baseUtils.checkAgainstRegexArray(resourcePath, option.includePath)) {
48
+ return {
49
+ code
50
+ };
51
+ }
52
+ // 黑名单检查
53
+ if (option.excludedPath.length && baseUtils.checkAgainstRegexArray(resourcePath, option.excludedPath)) {
54
+ return {
55
+ code
56
+ };
57
+ }
58
+ // 设置源语言
59
+ FunctionFactoryOption.originLang = option.originLang;
60
+ // 处理扩展功能
61
+ let sourceObj;
62
+ if (option.translateExtends) {
63
+ sourceObj = await option.translateExtends?.handleInitFile(code, resourcePath);
64
+ } else {
65
+ sourceObj = {
66
+ source: code
67
+ };
68
+ }
69
+ try {
70
+ // 使用 Babel 进行 AST 转换
71
+ const result = await babel.transformAsync(sourceObj.source, {
72
+ configFile: false,
73
+ plugins: [filter.default(sourceObj)]
74
+ });
75
+ // 开发模式下执行实时翻译
76
+ if (isDevMode) {
77
+ translateUtils.autoTranslate(); // 执行前需要确保transformAsync已经完成
78
+ }
79
+ return {
80
+ code: result?.code || code
81
+ };
82
+ } catch (e) {
83
+ console.error('Rsbuild auto-i18n plugin transform error:', e);
84
+ return {
85
+ code
86
+ };
87
+ }
88
+ });
89
+ // 构建开始前
90
+ api.onBeforeBuild(() => {
91
+ console.info('Rsbuild 构建阶段批量翻译开始');
92
+ });
93
+ // 构建结束后
94
+ api.onAfterBuild(async () => {
95
+ console.info('构建阶段批量翻译');
96
+ await translateUtils.autoTranslate();
97
+ });
98
+ // 构建完全结束
99
+ api.onCloseBuild(async () => {
100
+ translateUtils.cleanupUnusedTranslations();
101
+ // 翻译配置写入主文件
102
+ await fileUtils.buildSetLangConfigToIndexFile();
103
+ console.info('翻译完成✔');
104
+ });
105
+ // 开发服务器关闭时也需要清理
106
+ api.onCloseDevServer(async () => {
107
+ translateUtils.cleanupUnusedTranslations();
108
+ await fileUtils.buildSetLangConfigToIndexFile();
109
+ console.info('开发服务器关闭,翻译完成✔');
110
+ });
111
+ }
112
+ };
113
+ return plugin;
114
+ }
115
+
116
+ export { rsbuildPluginsAutoI18n as default };
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "rsbuild-auto-i18n-plugin",
3
+ "version": "1.1.10",
4
+ "description": "Rsbuild automatic translation plugin compatible with Rspack",
5
+ "keywords": [
6
+ "i18n",
7
+ "auto i18n",
8
+ "rsbuild i18n",
9
+ "rspack i18n",
10
+ "vue i18n",
11
+ "react i18n"
12
+ ],
13
+ "exports": {
14
+ "import": "./dist/index.mjs",
15
+ "require": "./dist/index.cjs",
16
+ "types": "./dist/index.d.ts"
17
+ },
18
+ "types": "./dist/index.d.ts",
19
+ "files": [
20
+ "dist",
21
+ "README*"
22
+ ],
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git@github.com:wenps/auto-i18n-translation-plugins.git"
26
+ },
27
+ "author": "callqh",
28
+ "license": "ISC",
29
+ "bugs": {
30
+ "url": "https://github.com/wenps/auto-i18n-translation-plugins/issues"
31
+ },
32
+ "homepage": "https://github.com/wenps/auto-i18n-translation-plugins#readme",
33
+ "dependencies": {
34
+ "@babel/core": "^7.23.2",
35
+ "@babel/preset-env": "^7.23.2",
36
+ "@babel/preset-typescript": "^7.23.2",
37
+ "auto-i18n-plugin-core": "1.1.10"
38
+ },
39
+ "devDependencies": {
40
+ "@babel/types": "^7.23.0",
41
+ "@rollup/plugin-babel": "^6.0.4",
42
+ "rollup-plugin-terser": "^7.0.2",
43
+ "@rollup/plugin-typescript": "^11.1.5",
44
+ "@types/babel__core": "^7.20.3",
45
+ "@types/node": "^20.3.3",
46
+ "babel-plugin-import": "^1.13.6",
47
+ "rollup": "^4.3.0",
48
+ "rollup-plugin-dts": "^6.1.0",
49
+ "rollup-plugin-node-externals": "^7.1.2",
50
+ "typescript": "^5.1.6",
51
+ "@rsbuild/core": "^1.0.0"
52
+ },
53
+ "peerDependencies": {
54
+ "@rsbuild/core": ">=1.0.0"
55
+ },
56
+ "scripts": {
57
+ "build": "rollup -c ./rollup.config.ts --configPlugin typescript"
58
+ }
59
+ }