@midwayjs/i18n 4.0.0-alpha.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/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]((http://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'])
@@ -3,6 +3,7 @@ import { TranslateOptions } from './interface';
3
3
  export declare class MidwayI18nServiceSingleton {
4
4
  private i18nConfig;
5
5
  private localeTextMap;
6
+ private localeJSONMap;
6
7
  private defaultLocale;
7
8
  private fallbackMatch;
8
9
  private localeMatchCache;
@@ -35,7 +36,17 @@ export declare class MidwayI18nServiceSingleton {
35
36
  * @param locale
36
37
  * @param group
37
38
  */
38
- getLocaleMapping(locale: string, group?: string): any;
39
+ getLocaleMapping(locale: string, group?: string): Map<string, any> | undefined;
40
+ /**
41
+ * get locale list by group
42
+ * @since 4.0.0
43
+ */
44
+ getLocaleList(group?: string): any[];
45
+ /**
46
+ * get origin locale json
47
+ * @since 4.0.0
48
+ */
49
+ getOriginLocaleJSON(locale: string, group?: string): any;
39
50
  /**
40
51
  * get current default language
41
52
  */
@@ -57,7 +68,7 @@ export declare class MidwayI18nService {
57
68
  * @param locale
58
69
  * @param group
59
70
  */
60
- getLocaleMapping(locale: any, group?: string): any;
71
+ getLocaleMapping(locale: any, group?: string): Map<string, any>;
61
72
  /**
62
73
  * get current default language
63
74
  */
@@ -15,11 +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.fallbackMatch = [];
21
- this.localeMatchCache = {};
22
- }
18
+ i18nConfig;
19
+ localeTextMap = new Map();
20
+ localeJSONMap = new Map();
21
+ defaultLocale;
22
+ fallbackMatch = [];
23
+ localeMatchCache = {};
23
24
  async init() {
24
25
  this.defaultLocale = (0, utils_1.formatLocale)(this.i18nConfig.defaultLocale);
25
26
  for (const lang in this.i18nConfig.localeTable) {
@@ -39,7 +40,9 @@ let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
39
40
  * @param localeTextMapping
40
41
  */
41
42
  addLocale(locale, localeTextMapping) {
42
- const currentLangMap = getMap(this.localeTextMap, locale, true);
43
+ locale = (0, utils_1.formatLocale)(locale);
44
+ const currentLangMap = getMap(this.localeTextMap, locale);
45
+ // set to text map
43
46
  for (const key in localeTextMapping) {
44
47
  if (typeof localeTextMapping[key] === 'string') {
45
48
  // set to default
@@ -52,6 +55,8 @@ let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
52
55
  }
53
56
  }
54
57
  }
58
+ // set to origin json map
59
+ this.localeJSONMap.set(locale, localeTextMapping);
55
60
  }
56
61
  /**
57
62
  * translate a message
@@ -78,6 +83,10 @@ let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
78
83
  msg = this.getLocaleMappingText(this.defaultLocale, message, group, args);
79
84
  }
80
85
  }
86
+ // If still no message found, use missingKeyHandler
87
+ if (!msg && this.i18nConfig.missingKeyHandler) {
88
+ msg = this.i18nConfig.missingKeyHandler(message, options);
89
+ }
81
90
  return msg;
82
91
  }
83
92
  /**
@@ -143,10 +152,31 @@ let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
143
152
  getLocaleMapping(locale, group = 'default') {
144
153
  locale = (0, utils_1.formatLocale)(locale);
145
154
  const langMap = this.localeTextMap.get(locale);
146
- if (langMap) {
155
+ if (langMap && langMap.size > 0) {
147
156
  return langMap.get(group);
148
157
  }
149
158
  }
159
+ /**
160
+ * get locale list by group
161
+ * @since 4.0.0
162
+ */
163
+ getLocaleList(group = 'default') {
164
+ const list = [];
165
+ for (const key of this.localeTextMap.keys()) {
166
+ if (this.localeTextMap.get(key).has(group)) {
167
+ list.push(key);
168
+ }
169
+ }
170
+ return list;
171
+ }
172
+ /**
173
+ * get origin locale json
174
+ * @since 4.0.0
175
+ */
176
+ getOriginLocaleJSON(locale, group = 'default') {
177
+ locale = (0, utils_1.formatLocale)(locale);
178
+ return this.localeJSONMap.get(locale)?.[group];
179
+ }
150
180
  /**
151
181
  * get current default language
152
182
  */
@@ -182,6 +212,8 @@ exports.MidwayI18nServiceSingleton = MidwayI18nServiceSingleton = __decorate([
182
212
  (0, core_1.Scope)(core_1.ScopeEnum.Singleton)
183
213
  ], MidwayI18nServiceSingleton);
184
214
  let MidwayI18nService = class MidwayI18nService {
215
+ i18nServiceSingleton;
216
+ ctx;
185
217
  translate(message, options = {}) {
186
218
  if (!options.locale) {
187
219
  options.locale = this.ctx.getAttr && this.ctx.getAttr(interface_1.I18N_ATTR_KEY);
@@ -265,8 +297,7 @@ function getES6Object(o) {
265
297
  }
266
298
  return o;
267
299
  }
268
- function getMap(o, key, formatKey = false) {
269
- key = formatKey ? (0, utils_1.formatLocale)(key) : key;
300
+ function getMap(o, key) {
270
301
  if (!o.has(key)) {
271
302
  o.set(key, new Map());
272
303
  }
@@ -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,10 +25,10 @@ function formatWithObject(text, values) {
24
25
  return original;
25
26
  });
26
27
  }
27
- exports.formatWithObject = formatWithObject;
28
28
  function formatLocale(locale) {
29
- // support zh_CN, en_US => zh-CN, en-US
29
+ if (!locale)
30
+ return locale;
31
+ // support zh_CN, en_US => zh-cn, en-us
30
32
  return locale.replace('_', '-').toLowerCase();
31
33
  }
32
- exports.formatLocale = formatLocale;
33
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-alpha.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-alpha.1",
17
- "@midwayjs/express": "^4.0.0-alpha.1",
18
- "@midwayjs/koa": "^4.0.0-alpha.1",
19
- "@midwayjs/mock": "^4.0.0-alpha.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": "14bb4da91805a1cf52f190c0d37a74b395dd6372"
41
+ "gitHead": "1b1856629913703f67304155aaf611ec936a81ac"
42
42
  }