@akanjs/dictionary 0.9.44 → 0.9.45

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.
@@ -34,6 +34,12 @@ class ServerTranslator {
34
34
  return existingTrans;
35
35
  import_common.Logger.log("Initialize ServerTranslator");
36
36
  const dictionary = await import_signal.fetch.getDictionary(lang);
37
+ return this.setTranslator(lang, dictionary);
38
+ }
39
+ hasTranslator(lang) {
40
+ return ServerTranslator.#langTransMap.has(lang);
41
+ }
42
+ setTranslator(lang, dictionary) {
37
43
  const trans = {
38
44
  dictionary,
39
45
  translate: (dictKey) => {
@@ -46,18 +52,18 @@ class ServerTranslator {
46
52
  ServerTranslator.#langTransMap.set(lang, trans);
47
53
  return trans;
48
54
  }
49
- getTransSync(lang, key) {
55
+ getTransSync(lang, key, param) {
50
56
  const trans = ServerTranslator.#langTransMap.get(lang);
51
57
  if (!trans)
52
58
  return void 0;
53
59
  const { translate } = trans;
54
60
  const msg = translate(key);
55
- return msg;
61
+ return param ? msg.replace(/{([^}]+)}/g, (_, key2) => param[key2]) : msg;
56
62
  }
57
- async getTrans(lang, key) {
63
+ async getTrans(lang, key, param) {
58
64
  const { translate } = await this.#getTranslator(lang);
59
65
  const msg = translate(key);
60
- return msg;
66
+ return param ? msg.replace(/{([^}]+)}/g, (_, key2) => param[key2]) : msg;
61
67
  }
62
68
  async getDictionary(lang) {
63
69
  const { dictionary } = await this.#getTranslator(lang);
@@ -11,6 +11,12 @@ class ServerTranslator {
11
11
  return existingTrans;
12
12
  Logger.log("Initialize ServerTranslator");
13
13
  const dictionary = await fetch.getDictionary(lang);
14
+ return this.setTranslator(lang, dictionary);
15
+ }
16
+ hasTranslator(lang) {
17
+ return ServerTranslator.#langTransMap.has(lang);
18
+ }
19
+ setTranslator(lang, dictionary) {
14
20
  const trans = {
15
21
  dictionary,
16
22
  translate: (dictKey) => {
@@ -23,18 +29,18 @@ class ServerTranslator {
23
29
  ServerTranslator.#langTransMap.set(lang, trans);
24
30
  return trans;
25
31
  }
26
- getTransSync(lang, key) {
32
+ getTransSync(lang, key, param) {
27
33
  const trans = ServerTranslator.#langTransMap.get(lang);
28
34
  if (!trans)
29
35
  return void 0;
30
36
  const { translate } = trans;
31
37
  const msg = translate(key);
32
- return msg;
38
+ return param ? msg.replace(/{([^}]+)}/g, (_, key2) => param[key2]) : msg;
33
39
  }
34
- async getTrans(lang, key) {
40
+ async getTrans(lang, key, param) {
35
41
  const { translate } = await this.#getTranslator(lang);
36
42
  const msg = translate(key);
37
- return msg;
43
+ return param ? msg.replace(/{([^}]+)}/g, (_, key2) => param[key2]) : msg;
38
44
  }
39
45
  async getDictionary(lang) {
40
46
  const { dictionary } = await this.#getTranslator(lang);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/dictionary",
3
- "version": "0.9.44",
3
+ "version": "0.9.45",
4
4
  "sourceType": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,8 +1,13 @@
1
1
  export declare class ServerTranslator {
2
2
  #private;
3
3
  init(lang: string): Promise<void>;
4
- getTransSync(lang: string, key: string): string | undefined;
5
- getTrans(lang: string, key: string): Promise<string>;
4
+ hasTranslator(lang: string): boolean;
5
+ setTranslator(lang: string, dictionary: Record<string, Record<string, string>>): {
6
+ dictionary: Record<string, Record<string, string>>;
7
+ translate: (dictKey: `${string}.${string}`) => string;
8
+ };
9
+ getTransSync(lang: string, key: string, param?: Record<string, string | number>): string | undefined;
10
+ getTrans(lang: string, key: string, param?: Record<string, string | number>): Promise<string>;
6
11
  getDictionary(lang: string): Promise<Record<string, Record<string, string>>>;
7
12
  }
8
13
  export declare const serverTranslator: ServerTranslator;