@midwayjs/i18n 4.0.0-beta.1 → 4.0.0-beta.10

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013 - Now midwayjs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -172,4 +172,4 @@ In the egg/koa/express/faas, current locale will be obtained based on query, hea
172
172
 
173
173
  ## License
174
174
 
175
- [MIT]((https://github.com/midwayjs/midway/blob/master/LICENSE))
175
+ [MIT](https://github.com/midwayjs/midway/blob/master/LICENSE)
@@ -21,5 +21,6 @@ exports.i18n = {
21
21
  },
22
22
  },
23
23
  localsField: 'i18n',
24
+ missingKeyHandler: message => message,
24
25
  };
25
26
  //# sourceMappingURL=config.default.js.map
@@ -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'])
@@ -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
- constructor() {
19
- this.localeTextMap = new Map();
20
- this.localeJSONMap = new Map();
21
- this.fallbackMatch = [];
22
- this.localeMatchCache = {};
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);
@@ -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
@@ -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.formatLocale = exports.formatWithObject = exports.formatWithArray = void 0;
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.1",
4
+ "version": "4.0.0-beta.10",
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.1",
17
- "@midwayjs/express": "^4.0.0-beta.1",
18
- "@midwayjs/koa": "^4.0.0-beta.1",
19
- "@midwayjs/mock": "^4.0.0-beta.1"
16
+ "@midwayjs/core": "^4.0.0-beta.10",
17
+ "@midwayjs/express": "^4.0.0-beta.10",
18
+ "@midwayjs/koa": "^4.0.0-beta.10",
19
+ "@midwayjs/mock": "^4.0.0-beta.10"
20
20
  },
21
21
  "keywords": [
22
22
  "midway",
@@ -32,11 +32,11 @@
32
32
  "lint": "mwts check"
33
33
  },
34
34
  "engines": {
35
- "node": ">=12"
35
+ "node": ">=20"
36
36
  },
37
37
  "repository": {
38
38
  "type": "git",
39
39
  "url": "https://github.com/midwayjs/midway.git"
40
40
  },
41
- "gitHead": "832961ec3aff123c033197d8c00cb2bc9bad7ff8"
41
+ "gitHead": "1b1856629913703f67304155aaf611ec936a81ac"
42
42
  }