@midwayjs/i18n 4.0.0-beta.7 → 4.0.0-beta.8
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/config/config.default.js +1 -0
- package/dist/configuration.js +1 -0
- package/dist/i18nService.js +12 -6
- package/dist/interface.d.ts +1 -0
- package/dist/middleware.js +4 -0
- package/dist/utils.js +3 -4
- package/package.json +6 -6
package/dist/configuration.js
CHANGED
|
@@ -14,6 +14,7 @@ const core_1 = require("@midwayjs/core");
|
|
|
14
14
|
const DefaultConfig = require("./config/config.default");
|
|
15
15
|
const middleware_1 = require("./middleware");
|
|
16
16
|
let I18nConfiguration = class I18nConfiguration {
|
|
17
|
+
applicationManager;
|
|
17
18
|
async onReady() {
|
|
18
19
|
this.applicationManager
|
|
19
20
|
.getApplications(['koa', 'egg', 'faas', 'express'])
|
package/dist/i18nService.js
CHANGED
|
@@ -15,12 +15,12 @@ const interface_1 = require("./interface");
|
|
|
15
15
|
const pm = require("picomatch");
|
|
16
16
|
const utils_1 = require("./utils");
|
|
17
17
|
let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
18
|
+
i18nConfig;
|
|
19
|
+
localeTextMap = new Map();
|
|
20
|
+
localeJSONMap = new Map();
|
|
21
|
+
defaultLocale;
|
|
22
|
+
fallbackMatch = [];
|
|
23
|
+
localeMatchCache = {};
|
|
24
24
|
async init() {
|
|
25
25
|
this.defaultLocale = (0, utils_1.formatLocale)(this.i18nConfig.defaultLocale);
|
|
26
26
|
for (const lang in this.i18nConfig.localeTable) {
|
|
@@ -83,6 +83,10 @@ let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
|
|
|
83
83
|
msg = this.getLocaleMappingText(this.defaultLocale, message, group, args);
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
+
// If still no message found, use missingKeyHandler
|
|
87
|
+
if (!msg && this.i18nConfig.missingKeyHandler) {
|
|
88
|
+
msg = this.i18nConfig.missingKeyHandler(message, options);
|
|
89
|
+
}
|
|
86
90
|
return msg;
|
|
87
91
|
}
|
|
88
92
|
/**
|
|
@@ -208,6 +212,8 @@ exports.MidwayI18nServiceSingleton = MidwayI18nServiceSingleton = __decorate([
|
|
|
208
212
|
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
|
|
209
213
|
], MidwayI18nServiceSingleton);
|
|
210
214
|
let MidwayI18nService = class MidwayI18nService {
|
|
215
|
+
i18nServiceSingleton;
|
|
216
|
+
ctx;
|
|
211
217
|
translate(message, options = {}) {
|
|
212
218
|
if (!options.locale) {
|
|
213
219
|
options.locale = this.ctx.getAttr && this.ctx.getAttr(interface_1.I18N_ATTR_KEY);
|
package/dist/interface.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ export interface I18nOptions {
|
|
|
18
18
|
writeCookie: boolean;
|
|
19
19
|
resolver: RequestResolver | false;
|
|
20
20
|
localsField: string;
|
|
21
|
+
missingKeyHandler?: (message: string, options?: TranslateOptions) => string;
|
|
21
22
|
}
|
|
22
23
|
export declare const I18N_ATTR_KEY = "i18n:locale";
|
|
23
24
|
//# sourceMappingURL=interface.d.ts.map
|
package/dist/middleware.js
CHANGED
|
@@ -15,6 +15,8 @@ const interface_1 = require("./interface");
|
|
|
15
15
|
const i18nService_1 = require("./i18nService");
|
|
16
16
|
const utils_1 = require("./utils");
|
|
17
17
|
let I18nFilter = class I18nFilter {
|
|
18
|
+
resolverConfig;
|
|
19
|
+
i18nConfig;
|
|
18
20
|
match(value, req, res) {
|
|
19
21
|
const saveLocale = req.getAttr(interface_1.I18N_ATTR_KEY);
|
|
20
22
|
if (this.resolverConfig) {
|
|
@@ -47,6 +49,8 @@ exports.I18nFilter = I18nFilter = __decorate([
|
|
|
47
49
|
(0, core_1.Match)()
|
|
48
50
|
], I18nFilter);
|
|
49
51
|
let I18nMiddleware = class I18nMiddleware {
|
|
52
|
+
resolverConfig;
|
|
53
|
+
i18nConfig;
|
|
50
54
|
resolve(app) {
|
|
51
55
|
if ('express' === app.getNamespace()) {
|
|
52
56
|
// add a filter for i18n cookie
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.formatWithArray = formatWithArray;
|
|
4
|
+
exports.formatWithObject = formatWithObject;
|
|
5
|
+
exports.formatLocale = formatLocale;
|
|
4
6
|
const ARRAY_INDEX_RE = /\{(\d+)\}/g;
|
|
5
7
|
function formatWithArray(text, values) {
|
|
6
8
|
return text.replace(ARRAY_INDEX_RE, (original, matched) => {
|
|
@@ -12,7 +14,6 @@ function formatWithArray(text, values) {
|
|
|
12
14
|
return original;
|
|
13
15
|
});
|
|
14
16
|
}
|
|
15
|
-
exports.formatWithArray = formatWithArray;
|
|
16
17
|
const Object_INDEX_RE = /\{(.+?)\}/g;
|
|
17
18
|
function formatWithObject(text, values) {
|
|
18
19
|
return text.replace(Object_INDEX_RE, (original, matched) => {
|
|
@@ -24,12 +25,10 @@ function formatWithObject(text, values) {
|
|
|
24
25
|
return original;
|
|
25
26
|
});
|
|
26
27
|
}
|
|
27
|
-
exports.formatWithObject = formatWithObject;
|
|
28
28
|
function formatLocale(locale) {
|
|
29
29
|
if (!locale)
|
|
30
30
|
return locale;
|
|
31
31
|
// support zh_CN, en_US => zh-cn, en-us
|
|
32
32
|
return locale.replace('_', '-').toLowerCase();
|
|
33
33
|
}
|
|
34
|
-
exports.formatLocale = formatLocale;
|
|
35
34
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/i18n",
|
|
3
3
|
"description": "midway i18n component",
|
|
4
|
-
"version": "4.0.0-beta.
|
|
4
|
+
"version": "4.0.0-beta.8",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
"picomatch": "2.3.1"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
17
|
-
"@midwayjs/express": "^4.0.0-beta.
|
|
18
|
-
"@midwayjs/koa": "^4.0.0-beta.
|
|
19
|
-
"@midwayjs/mock": "^4.0.0-beta.
|
|
16
|
+
"@midwayjs/core": "^4.0.0-beta.8",
|
|
17
|
+
"@midwayjs/express": "^4.0.0-beta.8",
|
|
18
|
+
"@midwayjs/koa": "^4.0.0-beta.8",
|
|
19
|
+
"@midwayjs/mock": "^4.0.0-beta.8"
|
|
20
20
|
},
|
|
21
21
|
"keywords": [
|
|
22
22
|
"midway",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"type": "git",
|
|
39
39
|
"url": "https://github.com/midwayjs/midway.git"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "355e55949fdd132b0bdcb4830222a0a027e92ded"
|
|
42
42
|
}
|