@pc-nexus/core 0.5.0-next.23 → 0.5.0-next.25
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/lib/i18n/main.d.ts +9 -5
- package/lib/i18n/main.d.ts.map +1 -1
- package/lib/i18n/main.js +43 -30
- package/lib/i18n/translation-loader.d.ts +2 -2
- package/lib/i18n/translation-loader.d.ts.map +1 -1
- package/lib/i18n/types.d.ts +10 -7
- package/lib/i18n/types.d.ts.map +1 -1
- package/lib/i18n/types.js +1 -1
- package/package.json +2 -2
package/lib/i18n/main.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { SupportedLocaleCode, GetTranslationsResult, Translator } from "./types.js";
|
|
2
|
+
declare class I18n {
|
|
3
|
+
#private;
|
|
4
|
+
constructor();
|
|
5
|
+
getTranslations(locale?: SupportedLocaleCode): Promise<GetTranslationsResult>;
|
|
6
|
+
createTranslator(locale?: SupportedLocaleCode): Promise<Translator>;
|
|
7
|
+
}
|
|
8
|
+
export declare const i18n: I18n;
|
|
9
|
+
export {};
|
|
6
10
|
//# sourceMappingURL=main.d.ts.map
|
package/lib/i18n/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/main.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/main.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAuD,qBAAqB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAGzI,cAAM,IAAI;;;IAMO,eAAe,CAAC,MAAM,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAK7E,gBAAgB,CAAC,MAAM,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,UAAU,CAAC;CA0DnF;AAED,eAAO,MAAM,IAAI,MAAa,CAAC"}
|
package/lib/i18n/main.js
CHANGED
|
@@ -1,40 +1,53 @@
|
|
|
1
1
|
import { NexusCode, NexusInnerError, NexusInnerErrorType } from "@pc-nexus/internal";
|
|
2
2
|
import { TranslationLoader } from "./translation-loader.js";
|
|
3
|
-
import {
|
|
3
|
+
import { SUPPORTED_LOCALE_CODES } from "./types.js";
|
|
4
4
|
import _ from "lodash";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
class I18n {
|
|
6
|
+
#translationsGetter;
|
|
7
|
+
constructor() {
|
|
8
|
+
this.#translationsGetter = new TranslationLoader();
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
let locale = undefined;
|
|
14
|
-
if (rawLocale) {
|
|
15
|
-
locale = ensureLocale(rawLocale);
|
|
10
|
+
async getTranslations(locale) {
|
|
11
|
+
this.#validateLocale(locale);
|
|
12
|
+
return await this.#translationsGetter.getTranslations(locale);
|
|
16
13
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
async createTranslator(locale) {
|
|
15
|
+
this.#validateLocale(locale);
|
|
16
|
+
const translationResult = await this.#translationsGetter.getTranslations(locale);
|
|
17
|
+
return {
|
|
18
|
+
translate: (key, params) => {
|
|
19
|
+
return this.#translate(key, translationResult.translations ?? {}, params);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
20
22
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
#translate(key, translations, params) {
|
|
24
|
+
if (!key) {
|
|
25
|
+
throw new NexusInnerError(NexusCode.InvalidInput, NexusInnerErrorType.I18nTranslationKeyRequired, `"key" is required in i18n translate.`);
|
|
26
|
+
}
|
|
27
|
+
let value = _.get(translations, key);
|
|
28
|
+
if (value == null) {
|
|
29
|
+
return key;
|
|
30
|
+
}
|
|
31
|
+
if (typeof value === "object") {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
if (typeof value !== "string") {
|
|
35
|
+
value = String(value);
|
|
36
|
+
}
|
|
37
|
+
if (!params) {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
return value.replace(/\{\{\s*(.*?)\s*\}\}/g, (x, k) => {
|
|
41
|
+
const paramValue = _.get(params, k.trim());
|
|
25
42
|
return paramValue != null ? String(paramValue) : `{{${k}}}`;
|
|
26
43
|
});
|
|
27
44
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
#validateLocale(locale) {
|
|
46
|
+
if (locale) {
|
|
47
|
+
if (!SUPPORTED_LOCALE_CODES.includes(locale)) {
|
|
48
|
+
throw new NexusInnerError(NexusCode.InvalidInput, NexusInnerErrorType.I18nTranslationLocaleInvalid, `locale "${locale}" is not supported`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
34
51
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
export const i18n = {
|
|
38
|
-
translate: translate,
|
|
39
|
-
getTranslations: getTranslations,
|
|
40
|
-
};
|
|
52
|
+
}
|
|
53
|
+
export const i18n = new I18n();
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SupportedLocaleCode, GetTranslationsResult } from "./types.js";
|
|
2
2
|
export declare class TranslationLoader {
|
|
3
3
|
#private;
|
|
4
4
|
constructor();
|
|
5
5
|
reset(): Promise<void>;
|
|
6
|
-
getTranslations(locale?:
|
|
6
|
+
getTranslations(locale?: SupportedLocaleCode): Promise<GetTranslationsResult>;
|
|
7
7
|
}
|
|
8
8
|
//# sourceMappingURL=translation-loader.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"translation-loader.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/translation-loader.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"translation-loader.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/translation-loader.ts"],"names":[],"mappings":"AACA,OAAO,EAA+B,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAWrG,qBAAa,iBAAiB;;;IA4Bb,KAAK;IAKL,eAAe,CAAC,MAAM,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,qBAAqB,CAAC;CAa7F"}
|
package/lib/i18n/types.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export declare interface
|
|
4
|
-
locale:
|
|
5
|
-
translations:
|
|
1
|
+
export declare const SUPPORTED_LOCALE_CODES: readonly ["zh-CN", "en-US"];
|
|
2
|
+
export type SupportedLocaleCode = (typeof SUPPORTED_LOCALE_CODES)[number];
|
|
3
|
+
export declare interface GetTranslationsResult {
|
|
4
|
+
locale: SupportedLocaleCode;
|
|
5
|
+
translations: TranslationsResourceContent | null;
|
|
6
6
|
}
|
|
7
|
-
export interface
|
|
8
|
-
[key: string]: string |
|
|
7
|
+
export interface TranslationsResourceContent {
|
|
8
|
+
[key: string]: string | TranslationsResourceContent;
|
|
9
|
+
}
|
|
10
|
+
export interface Translator {
|
|
11
|
+
translate: (key: string, params?: Record<string, any>) => TranslationsResourceContent | string;
|
|
9
12
|
}
|
|
10
13
|
//# sourceMappingURL=types.d.ts.map
|
package/lib/i18n/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/types.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,sBAAsB,6BAA8B,CAAC;AAClE,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE1E,MAAM,CAAC,OAAO,WAAW,qBAAqB;IAC1C,MAAM,EAAE,mBAAmB,CAAC;IAC5B,YAAY,EAAE,2BAA2B,GAAG,IAAI,CAAC;CACpD;AAED,MAAM,WAAW,2BAA2B;IACxC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,2BAA2B,CAAC;CACvD;AAED,MAAM,WAAW,UAAU;IACvB,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,2BAA2B,GAAG,MAAM,CAAC;CAClG"}
|
package/lib/i18n/types.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const
|
|
1
|
+
export const SUPPORTED_LOCALE_CODES = ["zh-CN", "en-US"];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pc-nexus/core",
|
|
3
|
-
"version": "0.5.0-next.
|
|
3
|
+
"version": "0.5.0-next.25",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"type": "module",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@pc-nexus/internal": "0.5.0-next.
|
|
32
|
+
"@pc-nexus/internal": "0.5.0-next.25",
|
|
33
33
|
"lodash": "^4.17.21"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {},
|