@eggjs/i18n 4.0.0-beta.35 → 4.0.0-beta.36
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/app/extend/application.d.ts +10 -6
- package/dist/app/extend/application.js +41 -69
- package/dist/app/extend/context.d.ts +75 -71
- package/dist/app/extend/context.js +149 -173
- package/dist/app.d.ts +57 -52
- package/dist/app.js +94 -95
- package/dist/config/config.default.d.ts +51 -49
- package/dist/config/config.default.js +15 -14
- package/dist/index.d.ts +20 -16
- package/dist/index.js +23 -20
- package/dist/locales.d.ts +7 -3
- package/dist/locales.js +48 -61
- package/dist/types.d.ts +29 -27
- package/dist/types.js +1 -2
- package/dist/utils.d.ts +5 -2
- package/dist/utils.js +8 -6
- package/package.json +25 -32
package/dist/locales.js
CHANGED
|
@@ -1,65 +1,52 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { debuglog } from 'node:util';
|
|
4
|
-
import { importModule } from '@eggjs/utils';
|
|
5
|
-
import ini from 'ini';
|
|
6
|
-
import yaml from 'js-yaml';
|
|
7
|
-
import { exists, readJSON } from 'utility';
|
|
8
1
|
import { formatLocale, isObject } from "./utils.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
debug('Init locales with %j, got %j resources', options, Object.keys(resources));
|
|
46
|
-
app._I18N_RESOURCES = resources;
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { debuglog } from "node:util";
|
|
4
|
+
import { exists, readJSON } from "utility";
|
|
5
|
+
import fs from "node:fs/promises";
|
|
6
|
+
import { importModule } from "@eggjs/utils";
|
|
7
|
+
import ini from "ini";
|
|
8
|
+
import yaml from "js-yaml";
|
|
9
|
+
|
|
10
|
+
//#region src/locales.ts
|
|
11
|
+
const debug = debuglog("egg/i18n/locales");
|
|
12
|
+
async function loadLocaleResources(app, options) {
|
|
13
|
+
const localeDirs = options.dirs;
|
|
14
|
+
const resources = {};
|
|
15
|
+
if (options.dir && !localeDirs.includes(options.dir)) {
|
|
16
|
+
app.deprecate("[@eggjs/i18n] `config.i18n.dir` is deprecated, please use `config.i18n.dirs` instead");
|
|
17
|
+
localeDirs.push(options.dir);
|
|
18
|
+
}
|
|
19
|
+
for (const dir of localeDirs) {
|
|
20
|
+
if (!await exists(dir)) continue;
|
|
21
|
+
const names = await fs.readdir(dir);
|
|
22
|
+
for (const name of names) {
|
|
23
|
+
const filepath = path.join(dir, name);
|
|
24
|
+
const locale = formatLocale(name.split(".")[0]);
|
|
25
|
+
let resource = {};
|
|
26
|
+
if (name.endsWith(".js") || name.endsWith(".ts")) resource = flattening(await importModule(filepath, { importDefaultOnly: true }));
|
|
27
|
+
else if (name.endsWith(".json")) resource = flattening(await readJSON(filepath));
|
|
28
|
+
else if (name.endsWith(".properties")) resource = ini.parse(await fs.readFile(filepath, "utf8"));
|
|
29
|
+
else if (name.endsWith(".yml") || name.endsWith(".yaml")) resource = flattening(yaml.load(await fs.readFile(filepath, "utf8")));
|
|
30
|
+
resources[locale] = resources[locale] || {};
|
|
31
|
+
Object.assign(resources[locale], resource);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
debug("Init locales with %j, got %j resources", options, Object.keys(resources));
|
|
35
|
+
app._I18N_RESOURCES = resources;
|
|
47
36
|
}
|
|
48
37
|
function flattening(data) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
deepFlat(data, '');
|
|
63
|
-
return result;
|
|
38
|
+
const result = {};
|
|
39
|
+
function deepFlat(data$1, prefix) {
|
|
40
|
+
for (const key in data$1) {
|
|
41
|
+
const value = data$1[key];
|
|
42
|
+
const k = prefix ? prefix + "." + key : key;
|
|
43
|
+
if (isObject(value)) deepFlat(value, k);
|
|
44
|
+
else result[k] = String(value);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
deepFlat(data, "");
|
|
48
|
+
return result;
|
|
64
49
|
}
|
|
65
|
-
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
export { loadLocaleResources };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,27 +1,29 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
import { I18nConfig } from "./config/config.default.js";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
declare module "egg" {
|
|
5
|
+
interface EggAppConfig {
|
|
6
|
+
/**
|
|
7
|
+
* I18n options
|
|
8
|
+
* @member Config#i18n
|
|
9
|
+
*/
|
|
10
|
+
i18n: I18nConfig;
|
|
11
|
+
}
|
|
12
|
+
interface Application {
|
|
13
|
+
isSupportLocale(locale: string): boolean;
|
|
14
|
+
gettext(locale: string, key: string, value?: any, ...args: any[]): string;
|
|
15
|
+
__(locale: string, key: string, value?: any, ...args: any[]): string;
|
|
16
|
+
}
|
|
17
|
+
interface Context {
|
|
18
|
+
/**
|
|
19
|
+
* get and set current request locale
|
|
20
|
+
* @member Context#locale
|
|
21
|
+
* @return {String} lower case locale string, e.g.: 'zh-cn', 'en-us'
|
|
22
|
+
*/
|
|
23
|
+
locale: string;
|
|
24
|
+
gettext(key: string, value?: any, ...args: any[]): string;
|
|
25
|
+
__(key: string, value?: any, ...args: any[]): string;
|
|
26
|
+
__getLocale(): string;
|
|
27
|
+
__setLocale(l: string): void;
|
|
28
|
+
}
|
|
29
|
+
}
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export {};
|
|
2
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdHlwZXMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9
|
|
1
|
+
export { };
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/utils.d.ts
|
|
2
|
+
declare function isObject(obj: any): boolean;
|
|
3
|
+
declare function formatLocale(locale: string): string;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { formatLocale, isObject };
|
package/dist/utils.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/utils.ts
|
|
2
|
+
function isObject(obj) {
|
|
3
|
+
return Object.prototype.toString.call(obj) === "[object Object]";
|
|
3
4
|
}
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return locale.replaceAll('_', '-').toLowerCase();
|
|
5
|
+
function formatLocale(locale) {
|
|
6
|
+
return locale.replaceAll("_", "-").toLowerCase();
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
export { formatLocale, isObject };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/i18n",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.36",
|
|
4
4
|
"description": "i18n plugin for egg",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"egg",
|
|
@@ -8,21 +8,24 @@
|
|
|
8
8
|
"eggPlugin",
|
|
9
9
|
"i18n"
|
|
10
10
|
],
|
|
11
|
-
"
|
|
12
|
-
"type": "git",
|
|
13
|
-
"url": "git+https://github.com/eggjs/egg.git",
|
|
14
|
-
"directory": "plugins/i18n"
|
|
15
|
-
},
|
|
11
|
+
"homepage": "https://github.com/eggjs/egg/tree/next/plugins/i18n#readme",
|
|
16
12
|
"bugs": {
|
|
17
13
|
"url": "https://github.com/eggjs/egg/issues"
|
|
18
14
|
},
|
|
19
|
-
"homepage": "https://github.com/eggjs/egg/tree/next/plugins/i18n#readme",
|
|
20
|
-
"author": "gxcsoccer <gxcsoccer@126.com>",
|
|
21
15
|
"license": "MIT",
|
|
22
|
-
"
|
|
23
|
-
|
|
16
|
+
"author": "gxcsoccer <gxcsoccer@126.com>",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/eggjs/egg.git",
|
|
20
|
+
"directory": "plugins/i18n"
|
|
24
21
|
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
25
|
"type": "module",
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
26
29
|
"exports": {
|
|
27
30
|
".": "./dist/index.js",
|
|
28
31
|
"./app": "./dist/app.js",
|
|
@@ -42,34 +45,24 @@
|
|
|
42
45
|
"ini": "^6.0.0",
|
|
43
46
|
"js-yaml": "^4.1.1",
|
|
44
47
|
"utility": "^2.5.0",
|
|
45
|
-
"@eggjs/utils": "5.0.0-beta.
|
|
46
|
-
},
|
|
47
|
-
"peerDependencies": {
|
|
48
|
-
"egg": "4.1.0-beta.35"
|
|
48
|
+
"@eggjs/utils": "5.0.0-beta.36"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@types/ini": "^4.1.1",
|
|
52
|
-
"@types/js-yaml": "4",
|
|
53
|
-
"@types/node": "^24.10.
|
|
52
|
+
"@types/js-yaml": "^4.0.9",
|
|
53
|
+
"@types/node": "^24.10.2",
|
|
54
54
|
"egg-view-nunjucks": "^2.3.0",
|
|
55
|
-
"tsdown": "^0.17.0",
|
|
56
55
|
"typescript": "^5.9.3",
|
|
57
|
-
"
|
|
58
|
-
"egg": "4.1.0-beta.
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
"@eggjs/mock": "7.0.0-beta.36",
|
|
57
|
+
"egg": "4.1.0-beta.36"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"egg": "4.1.0-beta.36"
|
|
61
|
+
},
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">= 22.18.0"
|
|
61
64
|
},
|
|
62
|
-
"files": [
|
|
63
|
-
"dist"
|
|
64
|
-
],
|
|
65
|
-
"main": "./dist/index.js",
|
|
66
|
-
"module": "./dist/index.js",
|
|
67
|
-
"types": "./dist/index.d.ts",
|
|
68
65
|
"scripts": {
|
|
69
|
-
"
|
|
70
|
-
"typecheck": "tsc --noEmit",
|
|
71
|
-
"lint": "oxlint --type-aware",
|
|
72
|
-
"lint:fix": "npm run lint -- --fix",
|
|
73
|
-
"test": "vitest run"
|
|
66
|
+
"typecheck": "tsgo --noEmit"
|
|
74
67
|
}
|
|
75
68
|
}
|