@midwayjs/i18n 3.20.5 → 4.0.0-beta.1

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.
@@ -22,11 +22,12 @@ let I18nConfiguration = class I18nConfiguration {
22
22
  });
23
23
  }
24
24
  };
25
+ exports.I18nConfiguration = I18nConfiguration;
25
26
  __decorate([
26
27
  (0, core_1.Inject)(),
27
28
  __metadata("design:type", core_1.MidwayApplicationManager)
28
29
  ], I18nConfiguration.prototype, "applicationManager", void 0);
29
- I18nConfiguration = __decorate([
30
+ exports.I18nConfiguration = I18nConfiguration = __decorate([
30
31
  (0, core_1.Configuration)({
31
32
  namespace: 'i18n',
32
33
  importConfigs: [
@@ -36,5 +37,4 @@ I18nConfiguration = __decorate([
36
37
  ],
37
38
  })
38
39
  ], I18nConfiguration);
39
- exports.I18nConfiguration = I18nConfiguration;
40
40
  //# sourceMappingURL=configuration.js.map
@@ -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
  */
@@ -17,6 +17,7 @@ const utils_1 = require("./utils");
17
17
  let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
18
18
  constructor() {
19
19
  this.localeTextMap = new Map();
20
+ this.localeJSONMap = new Map();
20
21
  this.fallbackMatch = [];
21
22
  this.localeMatchCache = {};
22
23
  }
@@ -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
@@ -59,10 +64,9 @@ let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
59
64
  * @param options
60
65
  */
61
66
  translate(message, options = {}) {
62
- var _a, _b, _c;
63
- const useLocale = (0, utils_1.formatLocale)((_a = options.locale) !== null && _a !== void 0 ? _a : this.defaultLocale);
64
- const args = (_b = options.args) !== null && _b !== void 0 ? _b : [];
65
- const group = (_c = options.group) !== null && _c !== void 0 ? _c : 'default';
67
+ const useLocale = (0, utils_1.formatLocale)(options.locale ?? this.defaultLocale);
68
+ const args = options.args ?? [];
69
+ const group = options.group ?? 'default';
66
70
  let msg = this.getLocaleMappingText(useLocale, message, group, args);
67
71
  if (!msg && useLocale !== this.defaultLocale) {
68
72
  if (this.fallbackMatch.length) {
@@ -144,10 +148,31 @@ let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
144
148
  getLocaleMapping(locale, group = 'default') {
145
149
  locale = (0, utils_1.formatLocale)(locale);
146
150
  const langMap = this.localeTextMap.get(locale);
147
- if (langMap) {
151
+ if (langMap && langMap.size > 0) {
148
152
  return langMap.get(group);
149
153
  }
150
154
  }
155
+ /**
156
+ * get locale list by group
157
+ * @since 4.0.0
158
+ */
159
+ getLocaleList(group = 'default') {
160
+ const list = [];
161
+ for (const key of this.localeTextMap.keys()) {
162
+ if (this.localeTextMap.get(key).has(group)) {
163
+ list.push(key);
164
+ }
165
+ }
166
+ return list;
167
+ }
168
+ /**
169
+ * get origin locale json
170
+ * @since 4.0.0
171
+ */
172
+ getOriginLocaleJSON(locale, group = 'default') {
173
+ locale = (0, utils_1.formatLocale)(locale);
174
+ return this.localeJSONMap.get(locale)?.[group];
175
+ }
151
176
  /**
152
177
  * get current default language
153
178
  */
@@ -167,6 +192,7 @@ let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
167
192
  }
168
193
  }
169
194
  };
195
+ exports.MidwayI18nServiceSingleton = MidwayI18nServiceSingleton;
170
196
  __decorate([
171
197
  (0, core_1.Config)('i18n'),
172
198
  __metadata("design:type", Object)
@@ -177,11 +203,10 @@ __decorate([
177
203
  __metadata("design:paramtypes", []),
178
204
  __metadata("design:returntype", Promise)
179
205
  ], MidwayI18nServiceSingleton.prototype, "init", null);
180
- MidwayI18nServiceSingleton = __decorate([
206
+ exports.MidwayI18nServiceSingleton = MidwayI18nServiceSingleton = __decorate([
181
207
  (0, core_1.Provide)(),
182
208
  (0, core_1.Scope)(core_1.ScopeEnum.Singleton)
183
209
  ], MidwayI18nServiceSingleton);
184
- exports.MidwayI18nServiceSingleton = MidwayI18nServiceSingleton;
185
210
  let MidwayI18nService = class MidwayI18nService {
186
211
  translate(message, options = {}) {
187
212
  if (!options.locale) {
@@ -215,12 +240,11 @@ let MidwayI18nService = class MidwayI18nService {
215
240
  * save current context lang to flag, middleware will be set it to cookie
216
241
  */
217
242
  saveRequestLocale(locale) {
218
- var _a, _b;
219
243
  if (locale) {
220
- (_a = this.ctx) === null || _a === void 0 ? void 0 : _a.setAttr(interface_1.I18N_ATTR_KEY, (0, utils_1.formatLocale)(locale));
244
+ this.ctx?.setAttr(interface_1.I18N_ATTR_KEY, (0, utils_1.formatLocale)(locale));
221
245
  }
222
246
  else {
223
- (_b = this.ctx) === null || _b === void 0 ? void 0 : _b.setAttr(interface_1.I18N_ATTR_KEY, (0, utils_1.formatLocale)(this.getDefaultLocale()));
247
+ this.ctx?.setAttr(interface_1.I18N_ATTR_KEY, (0, utils_1.formatLocale)(this.getDefaultLocale()));
224
248
  }
225
249
  }
226
250
  /**
@@ -239,6 +263,7 @@ let MidwayI18nService = class MidwayI18nService {
239
263
  return this.i18nServiceSingleton.hasAvailableLocale(locale);
240
264
  }
241
265
  };
266
+ exports.MidwayI18nService = MidwayI18nService;
242
267
  __decorate([
243
268
  (0, core_1.Inject)(),
244
269
  __metadata("design:type", MidwayI18nServiceSingleton)
@@ -247,10 +272,9 @@ __decorate([
247
272
  (0, core_1.Inject)(),
248
273
  __metadata("design:type", Object)
249
274
  ], MidwayI18nService.prototype, "ctx", void 0);
250
- MidwayI18nService = __decorate([
275
+ exports.MidwayI18nService = MidwayI18nService = __decorate([
251
276
  (0, core_1.Provide)()
252
277
  ], MidwayI18nService);
253
- exports.MidwayI18nService = MidwayI18nService;
254
278
  function formatText(message, args) {
255
279
  if (Array.isArray(args)) {
256
280
  return (0, utils_1.formatWithArray)(message, args);
@@ -267,8 +291,7 @@ function getES6Object(o) {
267
291
  }
268
292
  return o;
269
293
  }
270
- function getMap(o, key, formatKey = false) {
271
- key = formatKey ? (0, utils_1.formatLocale)(key) : key;
294
+ function getMap(o, key) {
272
295
  if (!o.has(key)) {
273
296
  o.set(key, new Map());
274
297
  }
@@ -34,6 +34,7 @@ let I18nFilter = class I18nFilter {
34
34
  return value;
35
35
  }
36
36
  };
37
+ exports.I18nFilter = I18nFilter;
37
38
  __decorate([
38
39
  (0, core_1.Config)('i18n.resolver'),
39
40
  __metadata("design:type", Object)
@@ -42,13 +43,12 @@ __decorate([
42
43
  (0, core_1.Config)('i18n'),
43
44
  __metadata("design:type", Object)
44
45
  ], I18nFilter.prototype, "i18nConfig", void 0);
45
- I18nFilter = __decorate([
46
+ exports.I18nFilter = I18nFilter = __decorate([
46
47
  (0, core_1.Match)()
47
48
  ], I18nFilter);
48
- exports.I18nFilter = I18nFilter;
49
49
  let I18nMiddleware = class I18nMiddleware {
50
50
  resolve(app) {
51
- if (app.getFrameworkType() === core_1.MidwayFrameworkType.WEB_EXPRESS) {
51
+ if ('express' === app.getNamespace()) {
52
52
  // add a filter for i18n cookie
53
53
  app.useFilter(I18nFilter);
54
54
  return async (req, res, next) => {
@@ -172,6 +172,7 @@ let I18nMiddleware = class I18nMiddleware {
172
172
  return 'i18n';
173
173
  }
174
174
  };
175
+ exports.I18nMiddleware = I18nMiddleware;
175
176
  __decorate([
176
177
  (0, core_1.Config)('i18n.resolver'),
177
178
  __metadata("design:type", Object)
@@ -180,8 +181,7 @@ __decorate([
180
181
  (0, core_1.Config)('i18n'),
181
182
  __metadata("design:type", Object)
182
183
  ], I18nMiddleware.prototype, "i18nConfig", void 0);
183
- I18nMiddleware = __decorate([
184
+ exports.I18nMiddleware = I18nMiddleware = __decorate([
184
185
  (0, core_1.Middleware)()
185
186
  ], I18nMiddleware);
186
- exports.I18nMiddleware = I18nMiddleware;
187
187
  //# sourceMappingURL=middleware.js.map
package/dist/utils.js CHANGED
@@ -26,7 +26,9 @@ function formatWithObject(text, values) {
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
34
  exports.formatLocale = formatLocale;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@midwayjs/i18n",
3
3
  "description": "midway i18n component",
4
- "version": "3.20.5",
4
+ "version": "4.0.0-beta.1",
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": "^3.20.4",
17
- "@midwayjs/express": "^3.20.4",
18
- "@midwayjs/koa": "^3.20.5",
19
- "@midwayjs/mock": "^3.20.4"
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"
20
20
  },
21
21
  "keywords": [
22
22
  "midway",
@@ -26,8 +26,8 @@
26
26
  "license": "MIT",
27
27
  "scripts": {
28
28
  "build": "tsc",
29
- "test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
30
- "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
29
+ "test": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand",
30
+ "cov": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand --coverage --forceExit",
31
31
  "ci": "npm run test",
32
32
  "lint": "mwts check"
33
33
  },
@@ -38,5 +38,5 @@
38
38
  "type": "git",
39
39
  "url": "https://github.com/midwayjs/midway.git"
40
40
  },
41
- "gitHead": "7ce57281bd3ef5d18dc50b47ff9bffb8a27c071e"
41
+ "gitHead": "832961ec3aff123c033197d8c00cb2bc9bad7ff8"
42
42
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
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.