@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.
@@ -1,6 +1,10 @@
1
- import { TranslationResourceContent, TranslationResult } from "./types.js";
2
- export declare const i18n: {
3
- translate: (i18nKey: string, args?: Record<string, number | string>, rawLocale?: string) => Promise<string | TranslationResourceContent>;
4
- getTranslations: (rawLocale?: string) => Promise<TranslationResult>;
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
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/main.ts"],"names":[],"mappings":"AAEA,OAAO,EAA+B,0BAA0B,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAkDxG,eAAO,MAAM,IAAI;yBAhCJ,MAAM,SACR,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,cAC1B,MAAM,KACnB,OAAO,CAAC,MAAM,GAAG,0BAA0B,CAAC;kCAqBJ,MAAM,KAAG,OAAO,CAAC,iBAAiB,CAAC;CAW7E,CAAC"}
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 { NexusSupportLocales } from "./types.js";
3
+ import { SUPPORTED_LOCALE_CODES } from "./types.js";
4
4
  import _ from "lodash";
5
- const translationsGetter = new TranslationLoader();
6
- const ensureLocale = (rawLocale) => {
7
- if (!NexusSupportLocales.includes(rawLocale)) {
8
- throw new NexusInnerError(NexusCode.invalidInput, NexusInnerErrorType.i18nTranslationLocaleInvalid, `locale "${rawLocale}" is not supported`);
5
+ class I18n {
6
+ #translationsGetter;
7
+ constructor() {
8
+ this.#translationsGetter = new TranslationLoader();
9
9
  }
10
- return rawLocale;
11
- };
12
- const translate = async (i18nKey, args, rawLocale) => {
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
- const translationResult = await translationsGetter.getTranslations(locale);
18
- if (!translationResult.translations) {
19
- return i18nKey;
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
- const result = _.get(translationResult.translations, i18nKey);
22
- if (_.isString(result) && args) {
23
- return result.replace(/\{\{\s*(.*?)\s*\}\}/g, (x, k) => {
24
- const paramValue = _.get(args, k.trim());
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
- return result || i18nKey;
29
- };
30
- const getTranslations = async (rawLocale) => {
31
- let locale = undefined;
32
- if (rawLocale) {
33
- locale = ensureLocale(rawLocale);
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
- return await translationsGetter.getTranslations(locale);
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 { Locale, TranslationResult } from "./types.js";
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?: Locale): Promise<TranslationResult>;
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,EAA8B,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAWnF,qBAAa,iBAAiB;;;IA4Bb,KAAK;IAKL,eAAe,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;CAa5E"}
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"}
@@ -1,10 +1,13 @@
1
- export type Locale = "en-US" | "zh-CN";
2
- export declare const NexusSupportLocales: Locale[];
3
- export declare interface TranslationResult {
4
- locale: Locale;
5
- translations: TranslationResourceContent | null;
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 TranslationResourceContent {
8
- [key: string]: string | TranslationResourceContent;
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
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;AAEvC,eAAO,MAAM,mBAAmB,EAAE,MAAM,EAAuB,CAAC;AAEhE,MAAM,CAAC,OAAO,WAAW,iBAAiB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,0BAA0B,GAAG,IAAI,CAAC;CACnD;AAED,MAAM,WAAW,0BAA0B;IACvC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,0BAA0B,CAAC;CACtD"}
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 NexusSupportLocales = ["en-US", "zh-CN"];
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.23",
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.23",
32
+ "@pc-nexus/internal": "0.5.0-next.25",
33
33
  "lodash": "^4.17.21"
34
34
  },
35
35
  "devDependencies": {},