@midwayjs/i18n 4.0.0-alpha.1 → 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.
- package/README.md +1 -1
- package/dist/i18nService.d.ts +13 -2
- package/dist/i18nService.js +29 -4
- package/dist/utils.js +3 -1
- package/package.json +6 -6
- package/LICENSE +0 -21
package/README.md
CHANGED
package/dist/i18nService.d.ts
CHANGED
|
@@ -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
|
*/
|
package/dist/i18nService.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
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-
|
|
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": "^4.0.0-
|
|
17
|
-
"@midwayjs/express": "^4.0.0-
|
|
18
|
-
"@midwayjs/koa": "^4.0.0-
|
|
19
|
-
"@midwayjs/mock": "^4.0.0-
|
|
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",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"type": "git",
|
|
39
39
|
"url": "https://github.com/midwayjs/midway.git"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
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.
|