zmdms-utils 0.0.73 → 0.0.74
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/dist/es/locales/i18n.d.ts +15 -0
- package/dist/es/locales/t-in-non-react.d.ts +17 -0
- package/dist/es/locales/t-options.d.ts +7 -0
- package/dist/es/locales/use-translation.d.ts +42 -0
- package/dist/es/microAppCreator.d.ts +25 -2
- package/dist/es/microAppCreator.js +28 -6
- package/dist/es/node_modules/html-parse-stringify/dist/html-parse-stringify.module.js +5 -0
- package/dist/es/node_modules/i18next/dist/esm/i18next.js +2242 -0
- package/dist/es/node_modules/react-i18next/dist/es/Trans.js +47 -0
- package/dist/es/node_modules/react-i18next/dist/es/TransWithoutContext.js +312 -0
- package/dist/es/node_modules/react-i18next/dist/es/context.js +18 -0
- package/dist/es/node_modules/react-i18next/dist/es/defaults.js +21 -0
- package/dist/es/node_modules/react-i18next/dist/es/i18nInstance.js +7 -0
- package/dist/es/node_modules/react-i18next/dist/es/initReactI18next.js +12 -0
- package/dist/es/node_modules/react-i18next/dist/es/unescape.js +27 -0
- package/dist/es/node_modules/react-i18next/dist/es/useTranslation.js +112 -0
- package/dist/es/node_modules/react-i18next/dist/es/utils.js +63 -0
- package/dist/es/node_modules/void-elements/index.js +23 -0
- package/dist/es/src/locales/i18n.js +16 -0
- package/dist/es/src/locales/t-in-non-react.js +54 -0
- package/dist/es/src/locales/t-options.js +20 -0
- package/dist/es/src/locales/use-translation.js +60 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/package.json +4 -2
- package/src/index.ts +3 -0
- package/src/locales/i18n.ts +18 -0
- package/src/locales/index.ts +8 -0
- package/src/locales/t-in-non-react.ts +65 -0
- package/src/locales/t-options.ts +24 -0
- package/src/locales/use-translation.ts +76 -0
- package/src/locales//345/244/232/350/257/255/350/250/200.md +1 -0
- package/src/microAppCreator.ts +47 -8
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import instance from '../../node_modules/i18next/dist/esm/i18next.js';
|
|
2
|
+
import { getTFuncArgs } from './t-options.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* t 函数封装:
|
|
6
|
+
* - 本文件的几个 t 函数,主要用于弥补一些在 React 组件之外使用的场景
|
|
7
|
+
* - 常规情况下,在 react 组件中,更加推荐的是使用 react-i18next 包中的 t 函数,即 const { t } = useTranslate(); 形式
|
|
8
|
+
*/
|
|
9
|
+
/** 包裹一层 try-catch 的 t 函数 */
|
|
10
|
+
var tryT = function (key, defaultValueOrOptions) {
|
|
11
|
+
var _a = getTFuncArgs(defaultValueOrOptions), defaultValue = _a.defaultValue, options = _a.options;
|
|
12
|
+
if (!key) {
|
|
13
|
+
// 情况1:键名为空,返回默认值或键名
|
|
14
|
+
console.error("tryT: \u8BF7\u4E0D\u8981\u4F20\u5165\u7A7A key \u503C\u8FDB\u884C i18n.t \u51FD\u6570\u6267\u884C\uFF0C\u8FD9\u5C06\u4F1A\u5BFC\u81F4\u76F4\u63A5\u8FD4\u56DE\u7A7A\u5B57\u7B26\u4E32\uFF01");
|
|
15
|
+
return defaultValue || key;
|
|
16
|
+
}
|
|
17
|
+
if (!instance.exists(key)) {
|
|
18
|
+
// 情况2:键名不存在,返回默认值或键名
|
|
19
|
+
console.error("tryT: i18n-key '".concat(key, "' \u4E0D\u5B58\u5728\uFF0C\u8BF7\u68C0\u67E5\u5BF9\u5E94\u7684 @/locales/resources/locales/xx.json \u4E2D\u662F\u5426\u5B58\u5728\u5BF9\u5E94\u503C"));
|
|
20
|
+
return defaultValue || key;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
// 情况3:尝试执行 i18n.t 函数
|
|
24
|
+
return instance.t(key, options);
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
// 情况4:执行 i18n.t 函数异常,返回默认值或键名
|
|
28
|
+
console.error("tryT: i18n.t key=".concat(key, " \u51FD\u6570\u6267\u884C\u5F02\u5E38\uFF1Aerr="), err);
|
|
29
|
+
return defaultValue || key;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* 同步版本的 i18n.t 函数(应对在 react 组件外使用的翻译函数)
|
|
34
|
+
* - 如果 i18n 未初始化,则返回默认值或键名
|
|
35
|
+
* - 如果 i18n 初始化后,键名不存在,则返回默认值或键名
|
|
36
|
+
* - 其他正常情况(已经初始化+键名存在),则返回翻译值
|
|
37
|
+
* @description 本函数为在非 react 组件中使用的翻译函数;(在react中请求使用 useTranslate() 返回的 t 函数)
|
|
38
|
+
* @param {string} key
|
|
39
|
+
* @param {TOpionsEx|string} defaultValue 必填值
|
|
40
|
+
* @returns {string}
|
|
41
|
+
* @example
|
|
42
|
+
* const txt = t('someLocaleKey', '兜底默认值');
|
|
43
|
+
*/
|
|
44
|
+
function safeT(key, defaultValueOrOptions) {
|
|
45
|
+
if (instance.isInitialized) {
|
|
46
|
+
return tryT(key, defaultValueOrOptions);
|
|
47
|
+
}
|
|
48
|
+
// 异常情况:没有初始化完成时,返回默认值
|
|
49
|
+
var defaultValue = getTFuncArgs(defaultValueOrOptions).defaultValue;
|
|
50
|
+
console.warn("safeT: i18n is not initialized. Returning defaultValue(".concat(defaultValue, ") or origin key (").concat(key, ")."));
|
|
51
|
+
return defaultValue || key;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { safeT };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取 t 函数的参数
|
|
3
|
+
* @param {string | TOpionsEx} defaultValueOrOptions 默认值或者选项
|
|
4
|
+
* @returns { {defaultValue: string, options: TOpionsEx} } 默认值和选项
|
|
5
|
+
*/
|
|
6
|
+
function getTFuncArgs(defaultValueOrOptions) {
|
|
7
|
+
var options = null;
|
|
8
|
+
var defaultValue = "";
|
|
9
|
+
if (typeof defaultValueOrOptions === "string") {
|
|
10
|
+
defaultValue = defaultValueOrOptions;
|
|
11
|
+
options = { defaultValue: defaultValue };
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
options = defaultValueOrOptions;
|
|
15
|
+
defaultValue = options.defaultValue;
|
|
16
|
+
}
|
|
17
|
+
return { defaultValue: defaultValue, options: options };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { getTFuncArgs };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { getTFuncArgs } from './t-options.js';
|
|
2
|
+
import { useTranslation } from '../../node_modules/react-i18next/dist/es/useTranslation.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 这里只是简单代理一下useTranslation,方便后续扩展或添加其他逻辑
|
|
6
|
+
* @param ns 命名空间 大多数情况下都不需要传入这个参数,除非你用到了命名空间。
|
|
7
|
+
* @param options 其他参数 大多数情况下,都不需要关注这个参数。
|
|
8
|
+
* options.keyPrefix 为所有翻译键添加前缀
|
|
9
|
+
* // JSON 文件:{ "greeting": { "welcome": "Welcome" } }
|
|
10
|
+
* const { t } = useTranslation('common', { keyPrefix: 'greeting' });
|
|
11
|
+
* // 使用时省略前缀
|
|
12
|
+
* t('welcome'); // 等价于 t('greeting:welcome')
|
|
13
|
+
* @returns { t, i18n, ready }
|
|
14
|
+
* t: 翻译方法
|
|
15
|
+
* i18n: i18next实例 访问 i18next 的完整 API(如切换语言、加载命名空间)
|
|
16
|
+
* i18n.changeLanguage('zh-CN'); 使用它切换语言,可以触发组件渲染。但是当前项目中是使用全局刷新的模式,来更新语言的。
|
|
17
|
+
* ready: 是否加载完成
|
|
18
|
+
*/
|
|
19
|
+
function useZtTranslation(ns, options) {
|
|
20
|
+
var _a = useTranslation(ns, options), _tOld = _a.t, i18n = _a.i18n, ready = _a.ready;
|
|
21
|
+
// 扩展t方法,用于处理嵌套的翻译键
|
|
22
|
+
var tEnhance = function (key, defaultValueOrOptions) {
|
|
23
|
+
var _a = getTFuncArgs(defaultValueOrOptions), defaultValue = _a.defaultValue, options = _a.options;
|
|
24
|
+
if (!key) {
|
|
25
|
+
// 情况1:键名为空,返回默认值或键名
|
|
26
|
+
console.error("tEnhance: \u8BF7\u4E0D\u8981\u4F20\u5165\u7A7A key \u503C\u8FDB\u884C t \u51FD\u6570\u6267\u884C\uFF0C\u8FD9\u5C06\u4F1A\u5BFC\u81F4\u76F4\u63A5\u8FD4\u56DE\u7A7A\u5B57\u7B26\u4E32\uFF01");
|
|
27
|
+
return defaultValue || key;
|
|
28
|
+
}
|
|
29
|
+
if (!i18n.exists(key)) {
|
|
30
|
+
// 情况2:键名不存在,返回默认值或键名
|
|
31
|
+
console.error("tEnhance: i18n-key '".concat(key, "' \u4E0D\u5B58\u5728\uFF0C\u8BF7\u68C0\u67E5\u5BF9\u5E94\u7684 @/locales/resources/locales/xx.json \u4E2D\u662F\u5426\u5B58\u5728\u5BF9\u5E94\u503C"));
|
|
32
|
+
return defaultValue || key;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
return _tOld(key, options);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.warn("tEnhance: t \u51FD\u6570\u6267\u884C\u5F02\u5E38\uFF0C\u8BF7\u68C0\u67E5! key=".concat(key, ", err="), error);
|
|
39
|
+
return defaultValue || key || "unknown";
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
/**
|
|
44
|
+
* t函数的使用方式:基本上使用1、2两种方式就可以了。
|
|
45
|
+
* 1、t('key') // 基础用法
|
|
46
|
+
* 2、t('greeting', { name: 'John' }); // 对应 JSON: "greeting": "Hello {{name}}"
|
|
47
|
+
* 3、t('auth:login'); // 显式指定命名空间
|
|
48
|
+
*
|
|
49
|
+
* 关于t函数的第二个参数的说明。
|
|
50
|
+
* 为了防止一些异常情况,翻译失败。可以传入一个默认值。这个默认值就是原本的中文就行。
|
|
51
|
+
* 1、t('key', '默认值');
|
|
52
|
+
* 2、t('key', { defaultValue: '默认值', ...otherOptions });
|
|
53
|
+
*/
|
|
54
|
+
t: tEnhance,
|
|
55
|
+
i18n: i18n,
|
|
56
|
+
ready: ready,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { useZtTranslation as useTranslation };
|
package/dist/index.d.ts
CHANGED
|
@@ -18,4 +18,9 @@ export { imgToPdf } from './es/pdf.js';
|
|
|
18
18
|
export { htmlToPdf } from './es/htmlToPdf.js';
|
|
19
19
|
export { htmlPrint, imgPrint, pdfPrint, windowPrint } from './es/print.js';
|
|
20
20
|
export { createWater, getDefaultContent, mergeWaterConfig } from './es/water.js';
|
|
21
|
+
export { initLocale } from './es/locales/i18n.js';
|
|
22
|
+
export { useTranslation } from './es/locales/use-translation.js';
|
|
23
|
+
export { safeT } from './es/locales/t-in-non-react.js';
|
|
21
24
|
export { Base64 } from 'js-base64';
|
|
25
|
+
export { default as i18n } from 'i18next';
|
|
26
|
+
export { Trans } from 'react-i18next';
|
package/dist/index.js
CHANGED
|
@@ -18,4 +18,9 @@ export { imgToPdf } from './es/pdf.js';
|
|
|
18
18
|
export { htmlToPdf } from './es/htmlToPdf.js';
|
|
19
19
|
export { htmlPrint, imgPrint, pdfPrint, windowPrint } from './es/print.js';
|
|
20
20
|
export { createWater, getDefaultContent, mergeWaterConfig } from './es/water.js';
|
|
21
|
+
export { initLocale } from './es/src/locales/i18n.js';
|
|
22
|
+
export { useTranslation } from './es/src/locales/use-translation.js';
|
|
23
|
+
export { safeT } from './es/src/locales/t-in-non-react.js';
|
|
21
24
|
export { Base64 } from './es/node_modules/js-base64/base64.js';
|
|
25
|
+
export { default as i18n } from './es/node_modules/i18next/dist/esm/i18next.js';
|
|
26
|
+
export { Trans } from './es/node_modules/react-i18next/dist/es/Trans.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zmdms-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.74",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -34,12 +34,14 @@
|
|
|
34
34
|
"@types/crypto-js": "^4.1.1",
|
|
35
35
|
"axios": "^1.4.0",
|
|
36
36
|
"crypto-js": "^4.1.1",
|
|
37
|
-
"js-base64": "^3.7.7",
|
|
38
37
|
"dayjs": "^1.11.10",
|
|
39
38
|
"html2canvas": "^1.4.1",
|
|
39
|
+
"i18next": "^25.1.2",
|
|
40
|
+
"js-base64": "^3.7.7",
|
|
40
41
|
"jspdf": "^2.5.1",
|
|
41
42
|
"print-js": "^1.6.0",
|
|
42
43
|
"qiankun": "^2.10.8",
|
|
44
|
+
"react-i18next": "^15.5.1",
|
|
43
45
|
"rimraf": "^4.4.1",
|
|
44
46
|
"rollup": "^3.20.2",
|
|
45
47
|
"rollup-plugin-dts": "^5.3.0",
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import i18next from "i18next";
|
|
2
|
+
import { initReactI18next } from "react-i18next";
|
|
3
|
+
|
|
4
|
+
type InitOptions = Parameters<typeof i18next.init>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 初始化逻辑
|
|
8
|
+
* - 本函数主要用于初始化 i18next 实例,同时使用 react-i18next 插件进行 react 组件的支持
|
|
9
|
+
* - 本函数的调用时机,一般是在项目的入口文件中进行调用,如 src/main.tsx 中
|
|
10
|
+
* @param options 参数 同i18next.init 第一个参数
|
|
11
|
+
* @param callback 参数 同i18next.init 第二个参数
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
function initLocale(options: InitOptions[0], callback?: InitOptions[1]) {
|
|
15
|
+
return i18next.use(initReactI18next).init(options, callback);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { initLocale, i18next as i18n };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* t 函数封装:
|
|
3
|
+
* - 本文件的几个 t 函数,主要用于弥补一些在 React 组件之外使用的场景
|
|
4
|
+
* - 常规情况下,在 react 组件中,更加推荐的是使用 react-i18next 包中的 t 函数,即 const { t } = useTranslate(); 形式
|
|
5
|
+
*/
|
|
6
|
+
import { i18n } from "./i18n";
|
|
7
|
+
import type { TOpionsEx } from "./t-options";
|
|
8
|
+
import { getTFuncArgs } from "./t-options";
|
|
9
|
+
|
|
10
|
+
/** 包裹一层 try-catch 的 t 函数 */
|
|
11
|
+
const tryT = (
|
|
12
|
+
key: string,
|
|
13
|
+
defaultValueOrOptions: string | TOpionsEx
|
|
14
|
+
): string => {
|
|
15
|
+
const { defaultValue, options } = getTFuncArgs(defaultValueOrOptions);
|
|
16
|
+
if (!key) {
|
|
17
|
+
// 情况1:键名为空,返回默认值或键名
|
|
18
|
+
console.error(
|
|
19
|
+
`tryT: 请不要传入空 key 值进行 i18n.t 函数执行,这将会导致直接返回空字符串!`
|
|
20
|
+
);
|
|
21
|
+
return defaultValue || key;
|
|
22
|
+
}
|
|
23
|
+
if (!i18n.exists(key)) {
|
|
24
|
+
// 情况2:键名不存在,返回默认值或键名
|
|
25
|
+
console.error(
|
|
26
|
+
`tryT: i18n-key '${key}' 不存在,请检查对应的 @/locales/resources/locales/xx.json 中是否存在对应值`
|
|
27
|
+
);
|
|
28
|
+
return defaultValue || key;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
// 情况3:尝试执行 i18n.t 函数
|
|
32
|
+
return i18n.t(key, options);
|
|
33
|
+
} catch (err) {
|
|
34
|
+
// 情况4:执行 i18n.t 函数异常,返回默认值或键名
|
|
35
|
+
console.error(`tryT: i18n.t key=${key} 函数执行异常:err=`, err);
|
|
36
|
+
return defaultValue || key;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 同步版本的 i18n.t 函数(应对在 react 组件外使用的翻译函数)
|
|
42
|
+
* - 如果 i18n 未初始化,则返回默认值或键名
|
|
43
|
+
* - 如果 i18n 初始化后,键名不存在,则返回默认值或键名
|
|
44
|
+
* - 其他正常情况(已经初始化+键名存在),则返回翻译值
|
|
45
|
+
* @description 本函数为在非 react 组件中使用的翻译函数;(在react中请求使用 useTranslate() 返回的 t 函数)
|
|
46
|
+
* @param {string} key
|
|
47
|
+
* @param {TOpionsEx|string} defaultValue 必填值
|
|
48
|
+
* @returns {string}
|
|
49
|
+
* @example
|
|
50
|
+
* const txt = t('someLocaleKey', '兜底默认值');
|
|
51
|
+
*/
|
|
52
|
+
export function safeT(
|
|
53
|
+
key: string,
|
|
54
|
+
defaultValueOrOptions: string | TOpionsEx
|
|
55
|
+
): string {
|
|
56
|
+
if (i18n.isInitialized) {
|
|
57
|
+
return tryT(key, defaultValueOrOptions);
|
|
58
|
+
}
|
|
59
|
+
// 异常情况:没有初始化完成时,返回默认值
|
|
60
|
+
const { defaultValue } = getTFuncArgs(defaultValueOrOptions);
|
|
61
|
+
console.warn(
|
|
62
|
+
`safeT: i18n is not initialized. Returning defaultValue(${defaultValue}) or origin key (${key}).`
|
|
63
|
+
);
|
|
64
|
+
return defaultValue || key;
|
|
65
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import type { TOptions } from "i18next";
|
|
3
|
+
|
|
4
|
+
export interface TOpionsEx extends TOptions {
|
|
5
|
+
defaultValue: string; // 必须填值
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 获取 t 函数的参数
|
|
10
|
+
* @param {string | TOpionsEx} defaultValueOrOptions 默认值或者选项
|
|
11
|
+
* @returns { {defaultValue: string, options: TOpionsEx} } 默认值和选项
|
|
12
|
+
*/
|
|
13
|
+
export function getTFuncArgs(defaultValueOrOptions: string | TOpionsEx) {
|
|
14
|
+
let options: TOpionsEx | null = null;
|
|
15
|
+
let defaultValue = "";
|
|
16
|
+
if (typeof defaultValueOrOptions === "string") {
|
|
17
|
+
defaultValue = defaultValueOrOptions;
|
|
18
|
+
options = { defaultValue };
|
|
19
|
+
} else {
|
|
20
|
+
options = defaultValueOrOptions;
|
|
21
|
+
defaultValue = options.defaultValue;
|
|
22
|
+
}
|
|
23
|
+
return { defaultValue, options };
|
|
24
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { useTranslation, Trans } from "react-i18next";
|
|
2
|
+
import type { TOpionsEx } from "./t-options";
|
|
3
|
+
import { getTFuncArgs } from "./t-options";
|
|
4
|
+
|
|
5
|
+
type Prams = Parameters<typeof useTranslation>;
|
|
6
|
+
type NS = Prams[0];
|
|
7
|
+
type Options = Prams[1];
|
|
8
|
+
|
|
9
|
+
type EnhanceTFunc = (
|
|
10
|
+
key: string,
|
|
11
|
+
defaultValueOrOptions: string | TOpionsEx
|
|
12
|
+
) => string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 这里只是简单代理一下useTranslation,方便后续扩展或添加其他逻辑
|
|
16
|
+
* @param ns 命名空间 大多数情况下都不需要传入这个参数,除非你用到了命名空间。
|
|
17
|
+
* @param options 其他参数 大多数情况下,都不需要关注这个参数。
|
|
18
|
+
* options.keyPrefix 为所有翻译键添加前缀
|
|
19
|
+
* // JSON 文件:{ "greeting": { "welcome": "Welcome" } }
|
|
20
|
+
* const { t } = useTranslation('common', { keyPrefix: 'greeting' });
|
|
21
|
+
* // 使用时省略前缀
|
|
22
|
+
* t('welcome'); // 等价于 t('greeting:welcome')
|
|
23
|
+
* @returns { t, i18n, ready }
|
|
24
|
+
* t: 翻译方法
|
|
25
|
+
* i18n: i18next实例 访问 i18next 的完整 API(如切换语言、加载命名空间)
|
|
26
|
+
* i18n.changeLanguage('zh-CN'); 使用它切换语言,可以触发组件渲染。但是当前项目中是使用全局刷新的模式,来更新语言的。
|
|
27
|
+
* ready: 是否加载完成
|
|
28
|
+
*/
|
|
29
|
+
function useZtTranslation(ns?: NS, options?: Options) {
|
|
30
|
+
const { t: _tOld, i18n, ready } = useTranslation(ns, options);
|
|
31
|
+
|
|
32
|
+
// 扩展t方法,用于处理嵌套的翻译键
|
|
33
|
+
const tEnhance: EnhanceTFunc = (key, defaultValueOrOptions) => {
|
|
34
|
+
const { defaultValue, options } = getTFuncArgs(defaultValueOrOptions);
|
|
35
|
+
|
|
36
|
+
if (!key) {
|
|
37
|
+
// 情况1:键名为空,返回默认值或键名
|
|
38
|
+
console.error(
|
|
39
|
+
`tEnhance: 请不要传入空 key 值进行 t 函数执行,这将会导致直接返回空字符串!`
|
|
40
|
+
);
|
|
41
|
+
return defaultValue || key;
|
|
42
|
+
}
|
|
43
|
+
if (!i18n.exists(key)) {
|
|
44
|
+
// 情况2:键名不存在,返回默认值或键名
|
|
45
|
+
console.error(
|
|
46
|
+
`tEnhance: i18n-key '${key}' 不存在,请检查对应的 @/locales/resources/locales/xx.json 中是否存在对应值`
|
|
47
|
+
);
|
|
48
|
+
return defaultValue || key;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
return _tOld(key, options);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.warn(`tEnhance: t 函数执行异常,请检查! key=${key}, err=`, error);
|
|
54
|
+
return defaultValue || key || "unknown";
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
/**
|
|
60
|
+
* t函数的使用方式:基本上使用1、2两种方式就可以了。
|
|
61
|
+
* 1、t('key') // 基础用法
|
|
62
|
+
* 2、t('greeting', { name: 'John' }); // 对应 JSON: "greeting": "Hello {{name}}"
|
|
63
|
+
* 3、t('auth:login'); // 显式指定命名空间
|
|
64
|
+
*
|
|
65
|
+
* 关于t函数的第二个参数的说明。
|
|
66
|
+
* 为了防止一些异常情况,翻译失败。可以传入一个默认值。这个默认值就是原本的中文就行。
|
|
67
|
+
* 1、t('key', '默认值');
|
|
68
|
+
* 2、t('key', { defaultValue: '默认值', ...otherOptions });
|
|
69
|
+
*/
|
|
70
|
+
t: tEnhance,
|
|
71
|
+
i18n,
|
|
72
|
+
ready,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { useZtTranslation as useTranslation, Trans };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# React 项目多语言
|
package/src/microAppCreator.ts
CHANGED
|
@@ -16,11 +16,32 @@ export function update(props: any) {
|
|
|
16
16
|
* @param microAppOptions 微应用的一些配置
|
|
17
17
|
*/
|
|
18
18
|
export interface IMicroAppOptions {
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated 该属性已废弃,无意义
|
|
21
|
+
*/
|
|
19
22
|
microAppContainerId: string; // 微应用加载的dom容器 id
|
|
20
23
|
microAppKey: string; // 微应用的唯一key值 用途:1. qiankun下的路由前缀;2. 主、子通信时的标识
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated 该属性已废弃,无意义
|
|
26
|
+
*/
|
|
21
27
|
microAppRoot: any; // 微应用根节点实例
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
/**
|
|
29
|
+
* 只会在微应用初始化的时候调用一次,下次微应用重新进入时会直接调用 mount 钩子,不会再重复触发 bootstrap
|
|
30
|
+
* 通常我们可以在这里做一些全局变量的初始化,比如不会在 unmount 阶段被销毁的应用级别的缓存等。
|
|
31
|
+
**/
|
|
32
|
+
microAppBootstrapHandle?: () => void;
|
|
33
|
+
/**
|
|
34
|
+
* 应用每次进入都会调用 mount 方法,通常我们在这里触发应用的渲染方法
|
|
35
|
+
*/
|
|
36
|
+
microAppMountHandle: (props: any) => void;
|
|
37
|
+
/**
|
|
38
|
+
* 应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
|
|
39
|
+
*/
|
|
40
|
+
microAppUnMountHandle: (props: any) => void;
|
|
41
|
+
/**
|
|
42
|
+
* 可选生命周期钩子,仅使用 loadMicroApp 方式加载微应用时生效
|
|
43
|
+
*/
|
|
44
|
+
microAppUpdateHandle?: (props: any) => void;
|
|
24
45
|
}
|
|
25
46
|
export function initMicroApp(render: any, microAppOptions: IMicroAppOptions) {
|
|
26
47
|
// 初始加载应用
|
|
@@ -32,9 +53,10 @@ export function initMicroApp(render: any, microAppOptions: IMicroAppOptions) {
|
|
|
32
53
|
// 2. 进入微前端环境的运行逻辑
|
|
33
54
|
const {
|
|
34
55
|
microAppKey,
|
|
35
|
-
|
|
56
|
+
microAppBootstrapHandle,
|
|
36
57
|
microAppMountHandle,
|
|
37
58
|
microAppUnMountHandle,
|
|
59
|
+
microAppUpdateHandle,
|
|
38
60
|
} = microAppOptions;
|
|
39
61
|
|
|
40
62
|
// 唯一key没传 给出开发提示
|
|
@@ -47,15 +69,20 @@ export function initMicroApp(render: any, microAppOptions: IMicroAppOptions) {
|
|
|
47
69
|
* 通常我们可以在这里做一些全局变量的初始化,比如不会在 unmount 阶段被销毁的应用级别的缓存等。
|
|
48
70
|
*/
|
|
49
71
|
async function bootstrap() {
|
|
50
|
-
console.log(
|
|
72
|
+
console.log(`微应用==${microAppKey}==bootstraped`);
|
|
73
|
+
if (microAppBootstrapHandle) {
|
|
74
|
+
microAppBootstrapHandle();
|
|
75
|
+
}
|
|
51
76
|
}
|
|
52
77
|
|
|
53
78
|
/**
|
|
54
79
|
* 应用每次进入都会调用 mount 方法,通常我们在这里触发应用的渲染方法
|
|
55
80
|
*/
|
|
56
81
|
async function mount(props: any) {
|
|
57
|
-
console.log(
|
|
58
|
-
|
|
82
|
+
console.log(`微应用==${microAppKey}==mount`);
|
|
83
|
+
if (microAppMountHandle) {
|
|
84
|
+
microAppMountHandle(props);
|
|
85
|
+
}
|
|
59
86
|
render(props);
|
|
60
87
|
}
|
|
61
88
|
|
|
@@ -63,13 +90,25 @@ export function initMicroApp(render: any, microAppOptions: IMicroAppOptions) {
|
|
|
63
90
|
* 应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
|
|
64
91
|
*/
|
|
65
92
|
async function unmount(props: any) {
|
|
66
|
-
console.log(
|
|
67
|
-
|
|
93
|
+
console.log(`微应用==${microAppKey}==unmount`);
|
|
94
|
+
if (microAppUnMountHandle) {
|
|
95
|
+
microAppUnMountHandle(props);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
|
|
100
|
+
*/
|
|
101
|
+
async function update(props: any) {
|
|
102
|
+
console.log(`微应用==${microAppKey}==update`);
|
|
103
|
+
if (microAppUpdateHandle) {
|
|
104
|
+
microAppUpdateHandle(props);
|
|
105
|
+
}
|
|
68
106
|
}
|
|
69
107
|
|
|
70
108
|
return {
|
|
71
109
|
bootstrap,
|
|
72
110
|
mount,
|
|
73
111
|
unmount,
|
|
112
|
+
update,
|
|
74
113
|
};
|
|
75
114
|
}
|