mm_tpl 2.8.0 → 2.9.0

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.
Files changed (3) hide show
  1. package/lib/lang.js +45 -1
  2. package/lib/tpl.js +4 -11
  3. package/package.json +66 -66
package/lib/lang.js CHANGED
@@ -154,6 +154,50 @@ Lang.prototype.buildDict = function (force) {
154
154
  return Object.assign({}, dict);
155
155
  };
156
156
 
157
+ /**
158
+ * 将嵌套语言包字典规范化为 mm_lang 的扁平词条表
159
+ * mm_lang 的 Lang 只接受 `语言.层级键 → 文本` 的扁平入参,控制键(default、silent)原样保留
160
+ * @param {Object} dict 语言包字典,语言代码对应嵌套词条对象
161
+ * @returns {Object} 扁平词条表
162
+ */
163
+ function flattenLangPack(dict) {
164
+ var flat = {};
165
+ for (var key in dict) {
166
+ if (!Object.prototype.hasOwnProperty.call(dict, key)) {
167
+ continue;
168
+ }
169
+ var val = dict[key];
170
+ if (val && typeof (val) === 'object' && !Array.isArray(val)) {
171
+ flattenInto(val, key, flat);
172
+ } else {
173
+ flat[key] = val;
174
+ }
175
+ }
176
+ return flat;
177
+ }
178
+
179
+ /**
180
+ * 递归展开嵌套词条为点号路径键 - 内部函数
181
+ * @param {Object} obj 嵌套词条对象
182
+ * @param {string} prefix 键前缀
183
+ * @param {Object} flat 输出容器
184
+ * @returns {void}
185
+ */
186
+ function flattenInto(obj, prefix, flat) {
187
+ for (var key in obj) {
188
+ if (!Object.prototype.hasOwnProperty.call(obj, key)) {
189
+ continue;
190
+ }
191
+ var val = obj[key];
192
+ var path = prefix + '.' + key;
193
+ if (val && typeof (val) === 'object' && !Array.isArray(val)) {
194
+ flattenInto(val, path, flat);
195
+ } else {
196
+ flat[path] = val;
197
+ }
198
+ }
199
+ }
200
+
157
201
  /**
158
202
  * 创建语言实例
159
203
  * 语言包字典在实例间共享,仅当前语言各自独立
@@ -166,7 +210,7 @@ Lang.prototype.create = function (config, force) {
166
210
  dict.default = config.default_lang || dict.default || 'zh_cn';
167
211
  // 词条缺失时静默返回 undefined,日志由模板引擎自身的 log 体系统一输出
168
212
  dict.silent = true;
169
- var lang = new BaseLang(dict);
213
+ var lang = new BaseLang(flattenLangPack(dict));
170
214
  this.registerPlural(lang, config.plural_rules);
171
215
  return lang;
172
216
  };
package/lib/tpl.js CHANGED
@@ -537,22 +537,14 @@ Tpl.prototype.tc = function (key, count, ...args) {
537
537
 
538
538
  /**
539
539
  * 获取支持的语言列表
540
- * 过滤掉无词条的语言,Lang 内置的 zh_cn、en 空字典不计入
540
+ * 语言列表来自 mm_lang 的扁平词条表,无词条的语言不会出现,无需过滤空字典
541
541
  * @returns {Array<string>} 语言代码数组
542
542
  */
543
543
  Tpl.prototype.getLangs = function () {
544
544
  if (!this.lang) {
545
545
  return [];
546
546
  }
547
- var langs = this.lang.getLangs();
548
- var list = [];
549
- for (var i = 0; i < langs.length; i++) {
550
- var dict = this.lang.config[langs[i]];
551
- if (dict && Object.keys(dict).length) {
552
- list.push(langs[i]);
553
- }
554
- }
555
- return list;
547
+ return this.lang.getLangs();
556
548
  };
557
549
 
558
550
  /**
@@ -580,7 +572,8 @@ Tpl.prototype.loadLang = function (lang) {
580
572
  return null;
581
573
  }
582
574
  this.reloadLangs();
583
- var dict = this.lang ? this.lang.config[lang] : null;
575
+ // 通过 getDict 导出该语言的嵌套词条字典(形态与语言包文件一致)
576
+ var dict = this.lang ? this.lang.getDict(lang) : null;
584
577
  if (!dict) {
585
578
  this.log('warn', '语言包不存在:' + lang);
586
579
  return null;
package/package.json CHANGED
@@ -1,66 +1,66 @@
1
- {
2
- "name": "mm_tpl",
3
- "version": "2.8.0",
4
- "description": "Super Meimei Template Engine - High-performance template engine based on art-template with MVC pattern, hook system, theme switching and caching mechanism",
5
- "main": "index.js",
6
- "io": true,
7
- "priority": 100,
8
- "scripts": {
9
- "dev": "nodemon test.js",
10
- "demo": "node example/demo.js",
11
- "test": "node test.js",
12
- "coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://gitee.com/qiuwenwu91/mm_tpl.git"
17
- },
18
- "keywords": [
19
- "mm_mod",
20
- "mm_infra",
21
- "mm_common",
22
- "mm_web",
23
- "template",
24
- "engine",
25
- "mvc",
26
- "web",
27
- "view",
28
- "render",
29
- "art-template",
30
- "hook",
31
- "theme",
32
- "cache",
33
- "nodejs",
34
- "javascript"
35
- ],
36
- "layer": "infra",
37
- "group": "common",
38
- "author": "邱文武",
39
- "license": "MIT",
40
- "bugs": {
41
- "url": "https://gitee.com/qiuwenwu91/mm_tpl/issues"
42
- },
43
- "homepage": "https://gitee.com/qiuwenwu91/mm_tpl#readme",
44
- "dependencies": {
45
- "art-template": "4.13.4",
46
- "mm_corejs": "^1.1.6",
47
- "mm_lang": "^2.0.0"
48
- },
49
- "engines": {
50
- "node": ">=12.0.0"
51
- },
52
- "files": [
53
- "index.js",
54
- "hook.js",
55
- "tpl_rule.js",
56
- "lib/",
57
- "doc/",
58
- "README.md",
59
- "README_EN.md",
60
- "package.json"
61
- ],
62
- "devDependencies": {
63
- "eslint": "^9.39.5",
64
- "eslint-plugin-jsdoc": "^65.0.0"
65
- }
66
- }
1
+ {
2
+ "name": "mm_tpl",
3
+ "version": "2.9.0",
4
+ "description": "Super Meimei Template Engine - High-performance template engine based on art-template with MVC pattern, hook system, theme switching and caching mechanism",
5
+ "main": "index.js",
6
+ "io": true,
7
+ "priority": 100,
8
+ "scripts": {
9
+ "dev": "nodemon test.js",
10
+ "demo": "node example/demo.js",
11
+ "test": "node test.js",
12
+ "coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://gitee.com/qiuwenwu91/mm_tpl.git"
17
+ },
18
+ "keywords": [
19
+ "mm_mod",
20
+ "mm_infra",
21
+ "mm_common",
22
+ "mm_web",
23
+ "template",
24
+ "engine",
25
+ "mvc",
26
+ "web",
27
+ "view",
28
+ "render",
29
+ "art-template",
30
+ "hook",
31
+ "theme",
32
+ "cache",
33
+ "nodejs",
34
+ "javascript"
35
+ ],
36
+ "layer": "infra",
37
+ "group": "common",
38
+ "author": "邱文武",
39
+ "license": "MIT",
40
+ "bugs": {
41
+ "url": "https://gitee.com/qiuwenwu91/mm_tpl/issues"
42
+ },
43
+ "homepage": "https://gitee.com/qiuwenwu91/mm_tpl#readme",
44
+ "dependencies": {
45
+ "art-template": "4.13.4",
46
+ "mm_corejs": "^1.1.6",
47
+ "mm_lang": "^3.0.0"
48
+ },
49
+ "engines": {
50
+ "node": ">=12.0.0"
51
+ },
52
+ "files": [
53
+ "index.js",
54
+ "hook.js",
55
+ "tpl_rule.js",
56
+ "lib/",
57
+ "doc/",
58
+ "README.md",
59
+ "README_EN.md",
60
+ "package.json"
61
+ ],
62
+ "devDependencies": {
63
+ "eslint": "^9.39.5",
64
+ "eslint-plugin-jsdoc": "^65.0.0"
65
+ }
66
+ }