@zwa73/utils 1.0.140 → 1.0.142

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.
@@ -74,7 +74,7 @@ export declare class SI18n {
74
74
  * @param i18nDataDir - 国际化数据的路径。
75
75
  * @param lang - 要设置的语言。
76
76
  */
77
- static init(i18nDataDir: string, lang: LangFlag): void;
77
+ static init(i18nDataDir: string, lang?: LangFlag): Promise<void>;
78
78
  /**根据提供的字符串,返回对应的国际化字符串
79
79
  * @param text - 字符串数组
80
80
  * @param markStr - 特殊标记文本
package/dist/UtilI18n.js CHANGED
@@ -19,6 +19,7 @@ const LangFlagList = Object.entries(I18nFlagTable)
19
19
  .concat(Object.keys(I18nFlagTable));
20
20
  const MarkRegex = /%%%([^%]+)%%%$/;
21
21
  const BaseFile = 'base_lang.json';
22
+ const TemplateFile = 'template.json';
22
23
  const LangDir = 'lang';
23
24
  /**运行时I18n工具
24
25
  * 需先调用 init初始化
@@ -52,10 +53,10 @@ class SI18n {
52
53
  * @param i18nDataDir - 国际化数据的路径。
53
54
  * @param lang - 要设置的语言。
54
55
  */
55
- static init(i18nDataDir, lang) {
56
+ static async init(i18nDataDir, lang) {
56
57
  const date = new Date().toISOString();
57
58
  const mergePath = pathe_1.default.join(i18nDataDir, BaseFile);
58
- const table = UtilFileTools_1.UtilFT.loadJSONFileSync(mergePath, { default: {
59
+ const table = await UtilFileTools_1.UtilFT.loadJSONFile(mergePath, { default: {
59
60
  base_lang: 'zh-CN',
60
61
  texts: [],
61
62
  } });
@@ -64,11 +65,11 @@ class SI18n {
64
65
  ...rest,
65
66
  text_table: texts.reduce((acc, cur) => ({ ...acc, [SI18n.formatI18nKey(cur.original, cur.mark)]: { ...cur, lang_table: {} } }), {}),
66
67
  };
67
- SI18n._vaildLang = [];
68
+ SI18n._vaildLang = ['template'];
68
69
  //覆盖入单语言文件
69
70
  const singleDir = pathe_1.default.join(i18nDataDir, LangDir);
70
- UtilFileTools_1.UtilFT.fileSearchGlobSync(singleDir, `**/${lang}*.json`)
71
- .map(p => UtilFileTools_1.UtilFT.loadJSONFileSync(p))
71
+ (await Promise.all((await UtilFileTools_1.UtilFT.fileSearchGlob(singleDir, `**/${lang}*.json`))
72
+ .map(async (p) => await UtilFileTools_1.UtilFT.loadJSONFile(p))))
72
73
  .forEach(t => {
73
74
  SI18n._vaildLang?.push(t.target_lang);
74
75
  return Object.entries(t.translate_table)
@@ -86,7 +87,7 @@ class SI18n {
86
87
  ltable[t.target_lang] ??= v;
87
88
  });
88
89
  });
89
- if (!SI18n._vaildLang.includes(lang))
90
+ if (lang != undefined && !SI18n._vaildLang.includes(lang))
90
91
  SI18n._vaildLang.push(lang);
91
92
  SI18n._lang = lang;
92
93
  SI18n._dataDir = i18nDataDir;
@@ -194,6 +195,10 @@ class SI18n {
194
195
  });
195
196
  });
196
197
  await Promise.all(Object.values(tbMap).map(async (t) => {
198
+ if (t.target_lang == 'template') {
199
+ const tmppath = pathe_1.default.join(SI18n._dataDir, TemplateFile);
200
+ return await UtilFileTools_1.UtilFT.writeJSONFile(tmppath, t);
201
+ }
197
202
  const singlePath = pathe_1.default.join(SI18n._dataDir, LangDir, t.target_lang);
198
203
  return await UtilFileTools_1.UtilFT.writeJSONFile(singlePath, t);
199
204
  }));
package/dist/index.d.ts CHANGED
@@ -10,3 +10,4 @@ export * from './UtilFileTools';
10
10
  export * from './UtilLogger';
11
11
  export * from './UtilFP';
12
12
  export * from './QuickFunction';
13
+ export * from './UtilI18n';
package/dist/index.js CHANGED
@@ -26,3 +26,4 @@ __exportStar(require("./UtilFileTools"), exports);
26
26
  __exportStar(require("./UtilLogger"), exports);
27
27
  __exportStar(require("./UtilFP"), exports);
28
28
  __exportStar(require("./QuickFunction"), exports);
29
+ __exportStar(require("./UtilI18n"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.140",
3
+ "version": "1.0.142",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/UtilI18n.ts CHANGED
@@ -68,6 +68,7 @@ export type I18nTable = Omit<I18nOrigTable,'texts'>&{
68
68
 
69
69
  const MarkRegex = /%%%([^%]+)%%%$/;
70
70
  const BaseFile = 'base_lang.json';
71
+ const TemplateFile = 'template.json';
71
72
  const LangDir = 'lang';
72
73
  /**运行时I18n工具
73
74
  * 需先调用 init初始化
@@ -76,7 +77,7 @@ export class SI18n {
76
77
  private static _table?:I18nTable;
77
78
  private static _lang?:LangFlag;
78
79
  private static _dataDir?:string;
79
- private static _vaildLang?:LangFlag[];
80
+ private static _vaildLang?:(LangFlag|'template')[];
80
81
  /**解析i18n索引 */
81
82
  static parseI18nKey(i18nKey:string){
82
83
  const match = i18nKey.match(/([\s\S]+?)(%%%[^%]+%%%)?$/)!;
@@ -101,10 +102,10 @@ export class SI18n {
101
102
  * @param i18nDataDir - 国际化数据的路径。
102
103
  * @param lang - 要设置的语言。
103
104
  */
104
- static init(i18nDataDir:string,lang:LangFlag){
105
+ static async init(i18nDataDir:string,lang?:LangFlag){
105
106
  const date = new Date().toISOString();
106
107
  const mergePath = path.join(i18nDataDir,BaseFile);
107
- const table = UtilFT.loadJSONFileSync<I18nOrigTable>(mergePath,{default:{
108
+ const table = await UtilFT.loadJSONFile<I18nOrigTable>(mergePath,{default:{
108
109
  base_lang:'zh-CN',
109
110
  texts:[],
110
111
  }});
@@ -115,11 +116,11 @@ export class SI18n {
115
116
  ({...acc,[SI18n.formatI18nKey(cur.original,cur.mark)]:{...cur,lang_table:{}}}),
116
117
  {} as Record<string,I18nTextData>),
117
118
  }
118
- SI18n._vaildLang=[];
119
+ SI18n._vaildLang=['template'];
119
120
  //覆盖入单语言文件
120
121
  const singleDir = path.join(i18nDataDir,LangDir);
121
- UtilFT.fileSearchGlobSync(singleDir,`**/${lang}*.json`)
122
- .map(p=>UtilFT.loadJSONFileSync(p) as I18nSingleTable)
122
+ (await Promise.all((await UtilFT.fileSearchGlob(singleDir,`**/${lang}*.json`))
123
+ .map(async p=>await UtilFT.loadJSONFile(p) as I18nSingleTable)))
123
124
  .forEach(t=>{
124
125
  SI18n._vaildLang?.push(t.target_lang);
125
126
  return Object.entries(t.translate_table)
@@ -137,9 +138,9 @@ export class SI18n {
137
138
  })
138
139
  });
139
140
 
140
-
141
- if(!SI18n._vaildLang.includes(lang))
141
+ if(lang!=undefined && !SI18n._vaildLang.includes(lang))
142
142
  SI18n._vaildLang.push(lang);
143
+
143
144
  SI18n._lang = lang;
144
145
  SI18n._dataDir = i18nDataDir;
145
146
  }
@@ -262,6 +263,10 @@ export class SI18n {
262
263
  })
263
264
  });
264
265
  await Promise.all(Object.values(tbMap).map(async (t)=>{
266
+ if(t.target_lang == 'template' as LangFlag){
267
+ const tmppath = path.join(SI18n._dataDir!,TemplateFile);
268
+ return await UtilFT.writeJSONFile(tmppath,t);
269
+ }
265
270
  const singlePath = path.join(SI18n._dataDir!,LangDir,t.target_lang);
266
271
  return await UtilFT.writeJSONFile(singlePath,t);
267
272
  }))
package/src/index.ts CHANGED
@@ -9,4 +9,5 @@ export * from './UtilDecorators';
9
9
  export * from './UtilFileTools';
10
10
  export * from './UtilLogger';
11
11
  export * from './UtilFP';
12
- export * from './QuickFunction';
12
+ export * from './QuickFunction';
13
+ export * from './UtilI18n';