vite-auto-i18n-plugin 0.0.14

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,107 @@
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
+ var fs = require('node:fs');
8
+
9
+ function _interopNamespaceDefault(e) {
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n.default = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var babel__namespace = /*#__PURE__*/_interopNamespaceDefault(babel);
27
+
28
+ /*
29
+ * @Author: 小山
30
+ * @Date: 2023-08-10 17:12:17
31
+ * @LastEditTime: 2025-02-10 22:17:18
32
+ * @FilePath: /i18n_translation_vite/packages/vitePluginsAutoI18n/src/index.ts
33
+ */
34
+ const allowedExtensions = ['.vue', '.ts', '.js', '.tsx', '.jsx'];
35
+ function vitePluginsAutoI18n(optionInfo) {
36
+ const name = 'vite-auto-i18n-plugin';
37
+ let config;
38
+ let disabled = true;
39
+ let tempDir = '';
40
+ autoI18nPluginCore.initOption(optionInfo);
41
+ if (!autoI18nPluginCore.checkOption())
42
+ return { name };
43
+ autoI18nPluginCore.fileUtils.initLangFile();
44
+ const originLangObj = autoI18nPluginCore.fileUtils.getLangObjByJSONFileWithLangKey(autoI18nPluginCore.option.originLang);
45
+ autoI18nPluginCore.translateUtils.languageConfigCompletion(originLangObj);
46
+ autoI18nPluginCore.translateUtils.initLangObj(originLangObj);
47
+ const plugin = {
48
+ name,
49
+ configResolved(resolvedConfig) {
50
+ // 存储最终解析的配置
51
+ config = resolvedConfig;
52
+ if (process.argv.includes('i18n')) {
53
+ disabled = false;
54
+ config.build.outDir = tempDir = `dist/vite-i18n-output_${+new Date()}`;
55
+ }
56
+ },
57
+ async transform(code, path) {
58
+ // todo 没有目标语言直接返回
59
+ if (allowedExtensions.some(ext => path.endsWith(ext))) {
60
+ if (autoI18nPluginCore.option.includePath.length &&
61
+ !autoI18nPluginCore.baseUtils.checkAgainstRegexArray(path, autoI18nPluginCore.option.includePath))
62
+ return code;
63
+ if (autoI18nPluginCore.option.excludedPath.length &&
64
+ autoI18nPluginCore.baseUtils.checkAgainstRegexArray(path, autoI18nPluginCore.option.excludedPath))
65
+ return code;
66
+ autoI18nPluginCore.FunctionFactoryOption.originLang = autoI18nPluginCore.option.originLang;
67
+ try {
68
+ let result = babel__namespace.transformSync(code, {
69
+ configFile: false,
70
+ plugins: [autoI18nPluginCore.filter.default]
71
+ });
72
+ if (config?.command === 'serve') {
73
+ await autoI18nPluginCore.translateUtils.autoTranslate();
74
+ }
75
+ return result?.code;
76
+ }
77
+ catch (e) {
78
+ console.error(e);
79
+ }
80
+ }
81
+ },
82
+ async buildEnd() {
83
+ if (disabled)
84
+ return;
85
+ console.info('构建阶段批量翻译');
86
+ await autoI18nPluginCore.translateUtils.autoTranslate();
87
+ },
88
+ async closeBundle() {
89
+ if (disabled)
90
+ return;
91
+ // 翻译配置写入主文件
92
+ await autoI18nPluginCore.fileUtils.buildSetLangConfigToIndexFile();
93
+ await new Promise(resolve => setTimeout(resolve, 1000)); // 等待文件夹解除锁定
94
+ fs.readdirSync(tempDir, { recursive: true });
95
+ console.info('翻译完成✔');
96
+ }
97
+ };
98
+ return plugin;
99
+ }
100
+
101
+ exports.default = vitePluginsAutoI18n;
102
+ Object.keys(autoI18nPluginCore).forEach(function (k) {
103
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
104
+ enumerable: true,
105
+ get: function () { return autoI18nPluginCore[k]; }
106
+ });
107
+ });
@@ -0,0 +1,6 @@
1
+ import { OptionInfo } from 'auto-i18n-plugin-core';
2
+ export * from 'auto-i18n-plugin-core';
3
+
4
+ declare function vitePluginsAutoI18n(optionInfo: OptionInfo): any;
5
+
6
+ export { vitePluginsAutoI18n as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,79 @@
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
+ import fs from 'node:fs';
5
+
6
+ /*
7
+ * @Author: 小山
8
+ * @Date: 2023-08-10 17:12:17
9
+ * @LastEditTime: 2025-02-10 22:17:18
10
+ * @FilePath: /i18n_translation_vite/packages/vitePluginsAutoI18n/src/index.ts
11
+ */
12
+ const allowedExtensions = ['.vue', '.ts', '.js', '.tsx', '.jsx'];
13
+ function vitePluginsAutoI18n(optionInfo) {
14
+ const name = 'vite-auto-i18n-plugin';
15
+ let config;
16
+ let disabled = true;
17
+ let tempDir = '';
18
+ initOption(optionInfo);
19
+ if (!checkOption())
20
+ return { name };
21
+ fileUtils.initLangFile();
22
+ const originLangObj = fileUtils.getLangObjByJSONFileWithLangKey(option.originLang);
23
+ translateUtils.languageConfigCompletion(originLangObj);
24
+ translateUtils.initLangObj(originLangObj);
25
+ const plugin = {
26
+ name,
27
+ configResolved(resolvedConfig) {
28
+ // 存储最终解析的配置
29
+ config = resolvedConfig;
30
+ if (process.argv.includes('i18n')) {
31
+ disabled = false;
32
+ config.build.outDir = tempDir = `dist/vite-i18n-output_${+new Date()}`;
33
+ }
34
+ },
35
+ async transform(code, path) {
36
+ // todo 没有目标语言直接返回
37
+ if (allowedExtensions.some(ext => path.endsWith(ext))) {
38
+ if (option.includePath.length &&
39
+ !baseUtils.checkAgainstRegexArray(path, option.includePath))
40
+ return code;
41
+ if (option.excludedPath.length &&
42
+ baseUtils.checkAgainstRegexArray(path, option.excludedPath))
43
+ return code;
44
+ FunctionFactoryOption.originLang = option.originLang;
45
+ try {
46
+ let result = babel.transformSync(code, {
47
+ configFile: false,
48
+ plugins: [filter.default]
49
+ });
50
+ if (config?.command === 'serve') {
51
+ await translateUtils.autoTranslate();
52
+ }
53
+ return result?.code;
54
+ }
55
+ catch (e) {
56
+ console.error(e);
57
+ }
58
+ }
59
+ },
60
+ async buildEnd() {
61
+ if (disabled)
62
+ return;
63
+ console.info('构建阶段批量翻译');
64
+ await translateUtils.autoTranslate();
65
+ },
66
+ async closeBundle() {
67
+ if (disabled)
68
+ return;
69
+ // 翻译配置写入主文件
70
+ await fileUtils.buildSetLangConfigToIndexFile();
71
+ await new Promise(resolve => setTimeout(resolve, 1000)); // 等待文件夹解除锁定
72
+ fs.readdirSync(tempDir, { recursive: true });
73
+ console.info('翻译完成✔');
74
+ }
75
+ };
76
+ return plugin;
77
+ }
78
+
79
+ export { vitePluginsAutoI18n as default };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "vite-auto-i18n-plugin",
3
+ "version": "0.0.14",
4
+ "description": "Vite automatic translation plugin compatible with Rollup",
5
+ "keywords": [
6
+ "i18n",
7
+ "auto i18n",
8
+ "vite i18n",
9
+ "vue i18n",
10
+ "react i18n"
11
+ ],
12
+ "exports": {
13
+ "require": "./dist/index.cjs",
14
+ "default": "./dist/index.mjs"
15
+ },
16
+ "types": "./dist/index.d.ts",
17
+ "files": [
18
+ "dist",
19
+ "README*"
20
+ ],
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/wenps/i18n_translation_vite.git"
24
+ },
25
+ "author": "wenps,Winfans,caleb-xu,xu-code",
26
+ "license": "ISC",
27
+ "bugs": {
28
+ "url": "https://github.com/wenps/i18n_translation_vite/issues"
29
+ },
30
+ "homepage": "https://github.com/wenps/i18n_translation_vite#readme",
31
+ "dependencies": {
32
+ "@babel/core": "^7.23.2",
33
+ "@babel/preset-env": "^7.23.2",
34
+ "@babel/preset-typescript": "^7.23.2",
35
+ "auto-i18n-plugin-core": "0.0.14"
36
+ },
37
+ "devDependencies": {
38
+ "@babel/types": "^7.23.0",
39
+ "rollup": "^4.3.0",
40
+ "rollup-plugin-dts": "^6.1.0",
41
+ "@rollup/plugin-typescript": "^11.1.5",
42
+ "@types/babel__core": "^7.20.3",
43
+ "@types/node": "^20.3.3",
44
+ "babel-plugin-import": "^1.13.6",
45
+ "rollup-plugin-node-externals": "^7.1.2",
46
+ "typescript": "^5.1.6",
47
+ "vite": "^2.8.6",
48
+ "vite-plugin-legacy": "^2.1.0"
49
+ },
50
+ "scripts": {
51
+ "build": "rollup -c ./rollup.config.ts --configPlugin typescript"
52
+ }
53
+ }