@midwayjs/i18n 3.0.0-beta.3 → 3.0.0-beta.4
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 +21 -0
- package/dist/config.default.d.ts +6 -2
- package/dist/config.default.js +11 -2
- package/dist/i18nService.d.ts +4 -4
- package/dist/i18nService.js +47 -15
- package/dist/interface.d.ts +1 -1
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +28 -0
- package/package.json +3 -2
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/dist/config.default.d.ts
CHANGED
package/dist/config.default.js
CHANGED
|
@@ -2,7 +2,16 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.i18n = void 0;
|
|
4
4
|
exports.i18n = {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
defaultLanguage: 'en',
|
|
6
|
+
languageTable: {
|
|
7
|
+
en: {},
|
|
8
|
+
},
|
|
9
|
+
fallbackLanguage: 'en',
|
|
10
|
+
fallbacks: {
|
|
11
|
+
// 'en-CA': 'fr',
|
|
12
|
+
// 'en-*': 'en',
|
|
13
|
+
// 'fr-*': 'fr',
|
|
14
|
+
// pt: 'pt-BR',
|
|
15
|
+
},
|
|
7
16
|
};
|
|
8
17
|
//# sourceMappingURL=config.default.js.map
|
package/dist/i18nService.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { TranslateOptions } from './interface';
|
|
2
2
|
export declare class MidwayI18nService {
|
|
3
3
|
i18nConfig: any;
|
|
4
|
-
private
|
|
5
|
-
private
|
|
4
|
+
private languageTextMap;
|
|
5
|
+
private defaultLanguage;
|
|
6
6
|
init(): Promise<void>;
|
|
7
|
-
|
|
8
|
-
translate(message: string, options
|
|
7
|
+
addLanguage(lang: string, langTextMapping: Record<string, string>): void;
|
|
8
|
+
translate(message: string, options?: TranslateOptions): any;
|
|
9
9
|
}
|
|
10
10
|
//# sourceMappingURL=i18nService.d.ts.map
|
package/dist/i18nService.js
CHANGED
|
@@ -12,32 +12,48 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.MidwayI18nService = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
14
|
const core_1 = require("@midwayjs/core");
|
|
15
|
-
const
|
|
15
|
+
const utils_1 = require("./utils");
|
|
16
16
|
let MidwayI18nService = class MidwayI18nService {
|
|
17
17
|
constructor() {
|
|
18
|
-
this.
|
|
18
|
+
this.languageTextMap = new Map();
|
|
19
19
|
}
|
|
20
20
|
async init() {
|
|
21
|
-
this.
|
|
21
|
+
this.defaultLanguage = this.i18nConfig.defaultLanguage;
|
|
22
|
+
for (const lang in this.i18nConfig.languageTable) {
|
|
23
|
+
this.addLanguage(lang, getES6Object(this.i18nConfig.languageTable[lang]));
|
|
24
|
+
}
|
|
22
25
|
}
|
|
23
|
-
|
|
24
|
-
if (!this.
|
|
25
|
-
this.
|
|
26
|
+
addLanguage(lang, langTextMapping) {
|
|
27
|
+
if (!this.languageTextMap.has(lang)) {
|
|
28
|
+
this.languageTextMap.set(lang, {});
|
|
26
29
|
}
|
|
27
|
-
const currentMap = this.
|
|
30
|
+
const currentMap = this.languageTextMap.get(lang);
|
|
28
31
|
for (const key in langTextMapping) {
|
|
29
|
-
currentMap
|
|
32
|
+
currentMap[key] = langTextMapping[key];
|
|
30
33
|
}
|
|
31
34
|
}
|
|
32
|
-
translate(message, options) {
|
|
33
|
-
var _a;
|
|
34
|
-
const useLang = (_a = options.lang) !== null && _a !== void 0 ? _a : this.
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
translate(message, options = {}) {
|
|
36
|
+
var _a, _b;
|
|
37
|
+
const useLang = (_a = options.lang) !== null && _a !== void 0 ? _a : this.defaultLanguage;
|
|
38
|
+
const args = (_b = options.args) !== null && _b !== void 0 ? _b : [];
|
|
39
|
+
const langMap = this.languageTextMap.get(useLang);
|
|
40
|
+
if (langMap) {
|
|
41
|
+
const msg = (0, core_1.safelyGet)(message, langMap);
|
|
42
|
+
if (msg) {
|
|
43
|
+
return formatText(msg, args);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// fallback
|
|
47
|
+
const fallbackLanguage = this.i18nConfig.fallbackLanguage;
|
|
48
|
+
const fallbackMap = this.languageTextMap.get(fallbackLanguage);
|
|
49
|
+
const msg = (0, core_1.safelyGet)(message, fallbackMap);
|
|
50
|
+
if (msg) {
|
|
51
|
+
return formatText(msg, args);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
38
54
|
}
|
|
39
55
|
else {
|
|
40
|
-
throw new core_1.MidwayCommonError(
|
|
56
|
+
throw new core_1.MidwayCommonError(`Language ${useLang} mapping not found`);
|
|
41
57
|
}
|
|
42
58
|
}
|
|
43
59
|
};
|
|
@@ -56,4 +72,20 @@ MidwayI18nService = __decorate([
|
|
|
56
72
|
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
|
|
57
73
|
], MidwayI18nService);
|
|
58
74
|
exports.MidwayI18nService = MidwayI18nService;
|
|
75
|
+
function formatText(message, args) {
|
|
76
|
+
if (Array.isArray(args)) {
|
|
77
|
+
return (0, utils_1.formatWithArray)(message, args);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return (0, utils_1.formatWithObject)(message, args);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function getES6Object(o) {
|
|
84
|
+
for (const key in o) {
|
|
85
|
+
if (o[key]['default'] && Object.keys(o[key]).length === 1) {
|
|
86
|
+
o[key] = o[key].default;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return o;
|
|
90
|
+
}
|
|
59
91
|
//# sourceMappingURL=i18nService.js.map
|
package/dist/interface.d.ts
CHANGED
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatWithObject = exports.formatWithArray = void 0;
|
|
4
|
+
const ARRAY_INDEX_RE = /\{(\d+)\}/g;
|
|
5
|
+
function formatWithArray(text, values) {
|
|
6
|
+
return text.replace(ARRAY_INDEX_RE, (orignal, matched) => {
|
|
7
|
+
const index = parseInt(matched);
|
|
8
|
+
if (index < values.length) {
|
|
9
|
+
return values[index];
|
|
10
|
+
}
|
|
11
|
+
// not match index, return original text
|
|
12
|
+
return orignal;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
exports.formatWithArray = formatWithArray;
|
|
16
|
+
const Object_INDEX_RE = /\{(.+?)\}/g;
|
|
17
|
+
function formatWithObject(text, values) {
|
|
18
|
+
return text.replace(Object_INDEX_RE, (orignal, matched) => {
|
|
19
|
+
const value = values[matched];
|
|
20
|
+
if (value) {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
// not match index, return original text
|
|
24
|
+
return orignal;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.formatWithObject = formatWithObject;
|
|
28
|
+
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/i18n",
|
|
3
3
|
"description": "midway i18n compoennet",
|
|
4
|
-
"version": "3.0.0-beta.
|
|
4
|
+
"version": "3.0.0-beta.4",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -32,5 +32,6 @@
|
|
|
32
32
|
"repository": {
|
|
33
33
|
"type": "git",
|
|
34
34
|
"url": "https://github.com/midwayjs/midway.git"
|
|
35
|
-
}
|
|
35
|
+
},
|
|
36
|
+
"gitHead": "02e2144e302f807770b512b0d89da3145b1cbf2e"
|
|
36
37
|
}
|