@midwayjs/i18n 4.0.0-alpha.1 → 4.0.0-beta.2

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))
@@ -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
@@ -143,10 +148,31 @@ let MidwayI18nServiceSingleton = class MidwayI18nServiceSingleton {
143
148
  getLocaleMapping(locale, group = 'default') {
144
149
  locale = (0, utils_1.formatLocale)(locale);
145
150
  const langMap = this.localeTextMap.get(locale);
146
- if (langMap) {
151
+ if (langMap && langMap.size > 0) {
147
152
  return langMap.get(group);
148
153
  }
149
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
+ }
150
176
  /**
151
177
  * get current default language
152
178
  */
@@ -265,8 +291,7 @@ function getES6Object(o) {
265
291
  }
266
292
  return o;
267
293
  }
268
- function getMap(o, key, formatKey = false) {
269
- key = formatKey ? (0, utils_1.formatLocale)(key) : key;
294
+ function getMap(o, key) {
270
295
  if (!o.has(key)) {
271
296
  o.set(key, new Map());
272
297
  }
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": "4.0.0-alpha.1",
4
+ "version": "4.0.0-beta.2",
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.2",
17
+ "@midwayjs/express": "^4.0.0-beta.2",
18
+ "@midwayjs/koa": "^4.0.0-beta.2",
19
+ "@midwayjs/mock": "^4.0.0-beta.2"
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": "53bfef4c5279da5f09025e4610bdbf64f94f60bd"
42
42
  }