@midwayjs/i18n 3.0.0-beta.3 → 3.0.0-beta.7

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/CHANGELOG.md CHANGED
@@ -3,6 +3,30 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.0.0-beta.7](https://github.com/midwayjs/midway/compare/v3.0.0-beta.6...v3.0.0-beta.7) (2021-12-03)
7
+
8
+ **Note:** Version bump only for package @midwayjs/i18n
9
+
10
+
11
+
12
+
13
+
14
+ # [3.0.0-beta.6](https://github.com/midwayjs/midway/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2021-11-26)
15
+
16
+ **Note:** Version bump only for package @midwayjs/i18n
17
+
18
+
19
+
20
+
21
+
22
+ # [3.0.0-beta.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
23
+
24
+ **Note:** Version bump only for package @midwayjs/i18n
25
+
26
+
27
+
28
+
29
+
6
30
  # [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
7
31
 
8
32
 
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.
@@ -1,5 +1,9 @@
1
1
  export declare const i18n: {
2
- currentLocale: string;
3
- localeMap: {};
2
+ defaultLanguage: string;
3
+ languageTable: {
4
+ en: {};
5
+ };
6
+ fallbackLanguage: string;
7
+ fallbacks: {};
4
8
  };
5
9
  //# sourceMappingURL=config.default.d.ts.map
@@ -2,7 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.i18n = void 0;
4
4
  exports.i18n = {
5
- currentLocale: 'en',
6
- localeMap: {},
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
@@ -1,10 +1,10 @@
1
1
  import { TranslateOptions } from './interface';
2
2
  export declare class MidwayI18nService {
3
3
  i18nConfig: any;
4
- private localeTextMap;
5
- private currentLocale;
4
+ private languageTextMap;
5
+ private defaultLanguage;
6
6
  init(): Promise<void>;
7
- addLocale(lang: string, langTextMapping: Record<string, string>): void;
8
- translate(message: string, options: TranslateOptions): string;
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
@@ -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 util_1 = require("util");
15
+ const utils_1 = require("./utils");
16
16
  let MidwayI18nService = class MidwayI18nService {
17
17
  constructor() {
18
- this.localeTextMap = new Map();
18
+ this.languageTextMap = new Map();
19
19
  }
20
20
  async init() {
21
- this.currentLocale = this.i18nConfig.currentLocale;
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
- addLocale(lang, langTextMapping) {
24
- if (!this.localeTextMap.has(lang)) {
25
- this.localeTextMap.set(lang, new Map());
26
+ addLanguage(lang, langTextMapping) {
27
+ if (!this.languageTextMap.has(lang)) {
28
+ this.languageTextMap.set(lang, {});
26
29
  }
27
- const currentMap = this.localeTextMap.get(lang);
30
+ const currentMap = this.languageTextMap.get(lang);
28
31
  for (const key in langTextMapping) {
29
- currentMap.set(key, langTextMapping[key]);
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.currentLocale;
35
- const langMap = this.localeTextMap.get(useLang);
36
- if (langMap && langMap.has(message)) {
37
- return (0, util_1.format)(langMap.get(message), options.args);
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('locale mapping not found');
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
@@ -1,7 +1,7 @@
1
1
  import { i18n } from './config.default';
2
2
  export interface TranslateOptions {
3
3
  lang?: string;
4
- args: any;
4
+ args?: any;
5
5
  }
6
6
  declare module '@midwayjs/core/dist/interface' {
7
7
  interface MidwayConfig {
@@ -0,0 +1,3 @@
1
+ export declare function formatWithArray(text: any, values: any): any;
2
+ export declare function formatWithObject(text: any, values: any): any;
3
+ //# sourceMappingURL=utils.d.ts.map
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.3",
4
+ "version": "3.0.0-beta.7",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
7
7
  "files": [
@@ -9,9 +9,9 @@
9
9
  "dist/**/*.d.ts"
10
10
  ],
11
11
  "devDependencies": {
12
- "@midwayjs/core": "^3.0.0-beta.4",
13
- "@midwayjs/decorator": "^3.0.0-beta.4",
14
- "@midwayjs/mock": "^3.0.0-beta.4"
12
+ "@midwayjs/core": "^3.0.0-beta.7",
13
+ "@midwayjs/decorator": "^3.0.0-beta.7",
14
+ "@midwayjs/mock": "^3.0.0-beta.7"
15
15
  },
16
16
  "keywords": [
17
17
  "midway",
@@ -32,5 +32,6 @@
32
32
  "repository": {
33
33
  "type": "git",
34
34
  "url": "https://github.com/midwayjs/midway.git"
35
- }
35
+ },
36
+ "gitHead": "24590729121d9110e2d960db5b5e587cf55a1efc"
36
37
  }