mm_lang 3.0.0 → 3.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.
@@ -1,4 +1,4 @@
1
- const Admin = require('mm_corejs').Admin;
1
+ const { reg } = require('mm_admin');
2
2
  const Langs = require('./langs.js');
3
3
 
4
4
  /**
@@ -7,7 +7,7 @@ const Langs = require('./langs.js');
7
7
  * Lang 配置 file 时以文件路径为作用域键从管理器取实例,相同文件的 Lang 复用同一存储
8
8
  * @type {Admin}
9
9
  */
10
- const langs_admin = new Admin();
10
+ const langs_admin = reg('lang.langs');
11
11
  langs_admin.setClass(Langs);
12
12
 
13
13
  module.exports = langs_admin;
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ /** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
5
+ module.exports = new Proxy({}, {
6
+ get(_t, key) {
7
+ if (typeof key !== 'string') return undefined;
8
+ if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
9
+ return require('./' + key);
10
+ },
11
+ has(_t, key) {
12
+ return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
13
+ },
14
+ ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
15
+ getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
16
+ });
@@ -0,0 +1,59 @@
1
+ 'use strict';
2
+ /**
3
+ * lang —— 多语言词条工具
4
+ * 复用模块 Lang 实例:层级键解析、回退链与格式化都由模块维护。
5
+ */
6
+ const { langAdmin } = require('../../../index.js');
7
+
8
+ async function main(params, ctx, options) {
9
+ void options;
10
+ const p = params || {};
11
+ const action = p.action || 't';
12
+ const done = (data) => { ctx.output = data; return data; };
13
+
14
+ try {
15
+ const lang = langAdmin(p.scope || 'os');
16
+
17
+ if (action === 't') {
18
+ if (!p.key) throw new TypeError('key 不能为空');
19
+ const text = lang.t(p.key, p.params || {}, p.lang);
20
+ return done({ code: 0, msg: '成功', key: p.key, text: String(text) });
21
+ }
22
+
23
+ if (action === 'get') {
24
+ return done({ code: 0, msg: '成功', lang: p.lang || '', value: lang.get(p.lang || undefined) });
25
+ }
26
+
27
+ if (action === 'set') {
28
+ if (!p.key) throw new TypeError('key 不能为空');
29
+ if (typeof p.value !== 'string') throw new TypeError('value 必须是字符串');
30
+ lang.set(p.key, p.value, p.lang);
31
+ if (typeof lang.save === 'function') lang.save();
32
+ return done({ code: 0, msg: '写入成功', key: p.key });
33
+ }
34
+
35
+ if (action === 'del') {
36
+ if (!p.key) throw new TypeError('key 不能为空');
37
+ const deleted = lang.del(p.key, p.lang);
38
+ if (typeof lang.save === 'function') lang.save();
39
+ return done({ code: 0, msg: deleted ? '删除成功' : '词条不存在', key: p.key, deleted: !!deleted });
40
+ }
41
+
42
+ if (action === 'langs') {
43
+ const langs = lang.getLangs();
44
+ const list = Array.isArray(langs) ? langs : Object.keys(langs || {});
45
+ return done({ code: 0, msg: '成功', count: list.length, langs: list });
46
+ }
47
+
48
+ if (action === 'save') {
49
+ lang.save();
50
+ return done({ code: 0, msg: '已落盘', ok: true });
51
+ }
52
+
53
+ return done({ code: 10001, msg: '不支持的操作: ' + action });
54
+ } catch (error) {
55
+ return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
56
+ }
57
+ }
58
+
59
+ module.exports = { main };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_lang",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "超级美眉多语言模块:融合 i18n 行为(Lang)与语言存储持久化(Langs),支持层级键、回退链、复数、格式化与 JSON 文件落盘",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -18,8 +18,8 @@
18
18
  "多语言",
19
19
  "语言包"
20
20
  ],
21
- "layer": "infra",
22
- "group": "data",
21
+ "layer": "infrastructure",
22
+ "group": "state",
23
23
  "author": "邱文武",
24
24
  "license": "ISC",
25
25
  "scripts": {
@@ -27,7 +27,7 @@
27
27
  "example": "node example/index.js"
28
28
  },
29
29
  "dependencies": {
30
- "mm_corejs": "^1.1.6",
30
+ "mm_admin": "^1.1.0",
31
31
  "mm_kv_store": "^1.0.1"
32
32
  },
33
33
  "files": [
@@ -35,6 +35,7 @@
35
35
  "lib",
36
36
  "example",
37
37
  "README.md",
38
- "package.json"
38
+ "package.json",
39
+ "tool"
39
40
  ]
40
41
  }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * lang —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/lang/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_lang').tools.lang 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/lang');
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "lang",
3
+ "title": "多语言词条",
4
+ "description": "基于 mm_lang 的多语言能力:按语言取词条(支持层级键与格式化)、写入/删除词条、列出支持语言、落盘。适用场景:生成多语言文案、补词条、核对某语言是否支持。工作流提示:t 按 key 取词,key 用层级点号(如 common.save);params 用于 {name} 之类占位。",
5
+ "category": "多语言",
6
+ "group": "i18n",
7
+ "type": "function",
8
+ "main": "./index.js",
9
+ "params": {
10
+ "type": "object",
11
+ "properties": {
12
+ "action": {
13
+ "type": "string",
14
+ "description": "操作类型: t(取词条)/get(读整表)/set(写词条)/del(删词条)/langs(支持语言)/save(落盘)",
15
+ "enum": [
16
+ "t",
17
+ "get",
18
+ "set",
19
+ "del",
20
+ "langs",
21
+ "save"
22
+ ],
23
+ "default": "t"
24
+ },
25
+ "key": {
26
+ "type": "string",
27
+ "description": "词条键(get/del/t 必填,支持 a.b.c 层级)"
28
+ },
29
+ "value": {
30
+ "type": "string",
31
+ "description": "词条文案(set 必填)"
32
+ },
33
+ "lang": {
34
+ "type": "string",
35
+ "description": "语言标识,缺省取实例默认语言"
36
+ },
37
+ "params": {
38
+ "type": "object",
39
+ "description": "占位参数(t 可选)"
40
+ },
41
+ "scope": {
42
+ "type": "string",
43
+ "description": "实例作用域,缺省 os",
44
+ "default": "os"
45
+ }
46
+ },
47
+ "required": [
48
+ "action"
49
+ ]
50
+ },
51
+ "returns": {
52
+ "type": "object",
53
+ "properties": {
54
+ "code": {
55
+ "type": "number",
56
+ "description": "0 表示成功,1xxxx 表示业务/执行异常"
57
+ },
58
+ "msg": {
59
+ "type": "string",
60
+ "description": "结果描述"
61
+ },
62
+ "text": {
63
+ "type": "string",
64
+ "description": "词条文案(t)"
65
+ },
66
+ "value": {
67
+ "description": "整表或单条值(get)"
68
+ },
69
+ "langs": {
70
+ "type": "array",
71
+ "description": "支持语言列表(langs)"
72
+ },
73
+ "count": {
74
+ "type": "number",
75
+ "description": "数量"
76
+ },
77
+ "error": {
78
+ "type": "object",
79
+ "description": "异常对象 { name, message }"
80
+ }
81
+ }
82
+ },
83
+ "handler": "manager"
84
+ }