@midwayjs/i18n 3.0.1 → 3.0.4

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.
@@ -216,8 +216,12 @@ let MidwayI18nService = class MidwayI18nService {
216
216
  */
217
217
  saveRequestLocale(locale) {
218
218
  var _a, _b;
219
- const currentLocale = (_a = locale !== null && locale !== void 0 ? locale : this.ctx.getAttr(interface_1.I18N_ATTR_KEY)) !== null && _a !== void 0 ? _a : this.getDefaultLocale();
220
- (_b = this.ctx) === null || _b === void 0 ? void 0 : _b.setAttr(interface_1.I18N_SAVE_KEY, (0, utils_1.formatLocale)(currentLocale));
219
+ if (locale) {
220
+ (_a = this.ctx) === null || _a === void 0 ? void 0 : _a.setAttr(interface_1.I18N_ATTR_KEY, (0, utils_1.formatLocale)(locale));
221
+ }
222
+ else {
223
+ (_b = this.ctx) === null || _b === void 0 ? void 0 : _b.setAttr(interface_1.I18N_ATTR_KEY, (0, utils_1.formatLocale)(this.getDefaultLocale()));
224
+ }
221
225
  }
222
226
  /**
223
227
  * get locale string by find fallback and default, ignore match message
@@ -18,5 +18,4 @@ export interface I18nOptions {
18
18
  };
19
19
  }
20
20
  export declare const I18N_ATTR_KEY = "i18n:locale";
21
- export declare const I18N_SAVE_KEY = "i18n:need_save_locale";
22
21
  //# sourceMappingURL=interface.d.ts.map
package/dist/interface.js CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.I18N_SAVE_KEY = exports.I18N_ATTR_KEY = void 0;
3
+ exports.I18N_ATTR_KEY = void 0;
4
4
  exports.I18N_ATTR_KEY = 'i18n:locale';
5
- exports.I18N_SAVE_KEY = 'i18n:need_save_locale';
6
5
  //# sourceMappingURL=interface.js.map
@@ -2,6 +2,7 @@ import { IMiddleware, IMidwayApplication } from '@midwayjs/core';
2
2
  import { I18nOptions } from './interface';
3
3
  export declare class I18nFilter {
4
4
  resolverConfig: I18nOptions['resolver'];
5
+ i18nConfig: I18nOptions;
5
6
  match(value: any, req: any, res: any): any;
6
7
  }
7
8
  export declare class I18nMiddleware implements IMiddleware<any, any> {
@@ -16,8 +16,8 @@ const i18nService_1 = require("./i18nService");
16
16
  const utils_1 = require("./utils");
17
17
  let I18nFilter = class I18nFilter {
18
18
  match(value, req, res) {
19
- const saveLocale = req.getAttr(interface_1.I18N_SAVE_KEY);
20
- if (saveLocale) {
19
+ const saveLocale = req.getAttr(interface_1.I18N_ATTR_KEY);
20
+ if (this.i18nConfig.writeCookie && saveLocale) {
21
21
  const cookieOptions = {
22
22
  // make sure browser javascript can read the cookie
23
23
  httpOnly: false,
@@ -34,6 +34,10 @@ __decorate([
34
34
  (0, decorator_1.Config)('i18n.resolver'),
35
35
  __metadata("design:type", Object)
36
36
  ], I18nFilter.prototype, "resolverConfig", void 0);
37
+ __decorate([
38
+ (0, decorator_1.Config)('i18n'),
39
+ __metadata("design:type", Object)
40
+ ], I18nFilter.prototype, "i18nConfig", void 0);
37
41
  I18nFilter = __decorate([
38
42
  (0, decorator_1.Match)()
39
43
  ], I18nFilter);
@@ -72,12 +76,12 @@ let I18nMiddleware = class I18nMiddleware {
72
76
  }
73
77
  }
74
78
  }
75
- // set to current locale
76
- req.setAttr(interface_1.I18N_ATTR_KEY, requestLocale);
77
- // auto write locale to cookie
78
- if (this.i18nConfig.writeCookie) {
79
+ if (requestLocale) {
79
80
  i18nService.saveRequestLocale(requestLocale);
80
81
  }
82
+ else {
83
+ i18nService.saveRequestLocale();
84
+ }
81
85
  return next();
82
86
  };
83
87
  }
@@ -113,17 +117,18 @@ let I18nMiddleware = class I18nMiddleware {
113
117
  }
114
118
  }
115
119
  }
116
- // set to current locale
117
- ctx.setAttr(interface_1.I18N_ATTR_KEY, requestLocale);
118
- // auto write locale to cookie
119
- if (this.i18nConfig.writeCookie) {
120
+ // save current locale
121
+ if (requestLocale) {
120
122
  i18nService.saveRequestLocale(requestLocale);
121
123
  }
124
+ else {
125
+ i18nService.saveRequestLocale();
126
+ }
122
127
  // run next middleware and controller
123
128
  await next();
124
129
  // get need save locale
125
- const saveLocale = ctx.getAttr(interface_1.I18N_SAVE_KEY);
126
- if (saveLocale) {
130
+ const saveLocale = ctx.getAttr(interface_1.I18N_ATTR_KEY);
131
+ if (this.i18nConfig.writeCookie && saveLocale) {
127
132
  const cookieOptions = {
128
133
  // make sure browser javascript can read the cookie
129
134
  httpOnly: false,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@midwayjs/i18n",
3
3
  "description": "midway i18n component",
4
- "version": "3.0.1",
4
+ "version": "3.0.4",
5
5
  "main": "dist/index",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -13,11 +13,11 @@
13
13
  "picomatch": "2.3.1"
14
14
  },
15
15
  "devDependencies": {
16
- "@midwayjs/core": "^3.0.1",
17
- "@midwayjs/decorator": "^3.0.0",
18
- "@midwayjs/express": "^3.0.1",
19
- "@midwayjs/koa": "^3.0.1",
20
- "@midwayjs/mock": "^3.0.1"
16
+ "@midwayjs/core": "^3.0.4",
17
+ "@midwayjs/decorator": "^3.0.4",
18
+ "@midwayjs/express": "^3.0.4",
19
+ "@midwayjs/koa": "^3.0.4",
20
+ "@midwayjs/mock": "^3.0.4"
21
21
  },
22
22
  "keywords": [
23
23
  "midway",
@@ -39,5 +39,5 @@
39
39
  "type": "git",
40
40
  "url": "https://github.com/midwayjs/midway.git"
41
41
  },
42
- "gitHead": "f345b4ed0392e5c3b9e815438ef0a377ad6da076"
42
+ "gitHead": "c2a37dd026c7bf3e855f4498691f572ef61396e3"
43
43
  }