@yh-ui/hooks 0.1.56 → 1.0.1

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.
@@ -45,17 +45,25 @@ const useLocale = localeOverrides => {
45
45
  }, {
46
46
  immediate: true
47
47
  });
48
- const t = (path, options) => {
48
+ const resolveLocaleValue = path => {
49
49
  const keys = path.split(".");
50
- let result = locale.value.yh;
51
- for (const key of keys) {
52
- if (result && typeof result === "object") {
53
- result = result[key];
54
- } else {
55
- result = void 0;
50
+ const sources = [locale.value.yh, _locale.zhCn.yh, _locale.en.yh];
51
+ for (const source of sources) {
52
+ let result = source;
53
+ for (const key of keys) {
54
+ if (result && typeof result === "object") {
55
+ result = result[key];
56
+ } else {
57
+ result = void 0;
58
+ break;
59
+ }
56
60
  }
57
- if (result === void 0) return path;
61
+ if (result !== void 0) return result;
58
62
  }
63
+ return void 0;
64
+ };
65
+ const t = (path, options) => {
66
+ const result = resolveLocaleValue(path);
59
67
  if (typeof result !== "string") return path;
60
68
  if (options) {
61
69
  return result.replace(/\{(\w+)\}/g, (_match, key) => {
@@ -66,16 +74,8 @@ const useLocale = localeOverrides => {
66
74
  return result;
67
75
  };
68
76
  const tRaw = path => {
69
- const keys = path.split(".");
70
- let result = locale.value.yh;
71
- for (const key of keys) {
72
- if (result && typeof result === "object") {
73
- result = result[key];
74
- } else {
75
- result = void 0;
76
- }
77
- if (result === void 0) return path;
78
- }
77
+ const result = resolveLocaleValue(path);
78
+ if (result === void 0) return path;
79
79
  return result;
80
80
  };
81
81
  return {
@@ -2,11 +2,6 @@ import type { Ref } from 'vue';
2
2
  import type { Language } from '@yh-ui/locale';
3
3
  export type { Language };
4
4
  export { setDayjsLocale, getDayjsLocale, setDayjsLocaleSync, updateDayjsMonths } from './dayjs-locale';
5
- /**
6
- * useLocale - 国际化
7
- * @param localeOverrides - 可选的自定义语言包
8
- * @returns 国际化相关方法
9
- */
10
5
  export declare const useLocale: (localeOverrides?: Ref<Language>) => {
11
6
  locale: import("vue").ComputedRef<Language>;
12
7
  lang: import("vue").ComputedRef<string>;
@@ -1,5 +1,5 @@
1
1
  import { computed, unref, watch } from "vue";
2
- import { zhCn } from "@yh-ui/locale";
2
+ import { en, zhCn } from "@yh-ui/locale";
3
3
  import { useConfig } from "../use-config/index.mjs";
4
4
  import { setDayjsLocale } from "./dayjs-locale.mjs";
5
5
  export {
@@ -21,17 +21,25 @@ export const useLocale = (localeOverrides) => {
21
21
  },
22
22
  { immediate: true }
23
23
  );
24
- const t = (path, options) => {
24
+ const resolveLocaleValue = (path) => {
25
25
  const keys = path.split(".");
26
- let result = locale.value.yh;
27
- for (const key of keys) {
28
- if (result && typeof result === "object") {
29
- result = result[key];
30
- } else {
31
- result = void 0;
26
+ const sources = [locale.value.yh, zhCn.yh, en.yh];
27
+ for (const source of sources) {
28
+ let result = source;
29
+ for (const key of keys) {
30
+ if (result && typeof result === "object") {
31
+ result = result[key];
32
+ } else {
33
+ result = void 0;
34
+ break;
35
+ }
32
36
  }
33
- if (result === void 0) return path;
37
+ if (result !== void 0) return result;
34
38
  }
39
+ return void 0;
40
+ };
41
+ const t = (path, options) => {
42
+ const result = resolveLocaleValue(path);
35
43
  if (typeof result !== "string") return path;
36
44
  if (options) {
37
45
  return result.replace(/\{(\w+)\}/g, (_match, key) => {
@@ -42,16 +50,8 @@ export const useLocale = (localeOverrides) => {
42
50
  return result;
43
51
  };
44
52
  const tRaw = (path) => {
45
- const keys = path.split(".");
46
- let result = locale.value.yh;
47
- for (const key of keys) {
48
- if (result && typeof result === "object") {
49
- result = result[key];
50
- } else {
51
- result = void 0;
52
- }
53
- if (result === void 0) return path;
54
- }
53
+ const result = resolveLocaleValue(path);
54
+ if (result === void 0) return path;
55
55
  return result;
56
56
  };
57
57
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yh-ui/hooks",
3
- "version": "0.1.56",
3
+ "version": "1.0.1",
4
4
  "description": "YH-UI composition hooks",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -23,8 +23,8 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@yh-ui/locale": "^0.1.56",
27
- "@yh-ui/utils": "^0.1.56",
26
+ "@yh-ui/locale": "^1.0.1",
27
+ "@yh-ui/utils": "^1.0.1",
28
28
  "dayjs": "^1.11.19"
29
29
  },
30
30
  "devDependencies": {