akanjs 2.2.7-rc.1 → 2.2.7-rc.2
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/client/translator.ts +35 -24
- package/package.json +1 -1
- package/types/client/translator.d.ts +2 -3
package/client/translator.ts
CHANGED
|
@@ -9,51 +9,62 @@ export interface AllDictionary {
|
|
|
9
9
|
[key: string]: Dictionary;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
interface TranslatorState {
|
|
13
|
+
langDictionaryMap: Map<string, Dictionary>;
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
seededDicts: WeakSet<object>;
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
activeLocale?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const TRANSLATOR_STATE_KEY = "__AKAN_TRANSLATOR_STATE__";
|
|
21
|
+
const getTranslatorState = (): TranslatorState => {
|
|
22
|
+
const globalScope = globalThis as typeof globalThis & {
|
|
23
|
+
[TRANSLATOR_STATE_KEY]?: TranslatorState;
|
|
24
|
+
};
|
|
25
|
+
globalScope[TRANSLATOR_STATE_KEY] ??= {
|
|
26
|
+
langDictionaryMap: new Map<string, Dictionary>(),
|
|
27
|
+
seededDicts: new WeakSet<object>(),
|
|
28
|
+
};
|
|
29
|
+
return globalScope[TRANSLATOR_STATE_KEY];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export class Translator {
|
|
24
33
|
constructor(dictionary: Record<string, Record<string, Record<string, unknown>>>) {
|
|
25
|
-
Object.entries(dictionary).forEach(([lang,
|
|
26
|
-
|
|
34
|
+
Object.entries(dictionary).forEach(([lang, dict]) => {
|
|
35
|
+
Translator.seed(lang, dict as Dictionary);
|
|
27
36
|
});
|
|
28
37
|
}
|
|
29
38
|
hasDictionary(lang: string) {
|
|
30
|
-
return
|
|
39
|
+
return getTranslatorState().langDictionaryMap.has(lang);
|
|
40
|
+
}
|
|
41
|
+
static setActiveLocale(lang: string | undefined) {
|
|
42
|
+
if (lang) getTranslatorState().activeLocale = lang;
|
|
43
|
+
}
|
|
44
|
+
static getActiveLocale(): string | undefined {
|
|
45
|
+
return getTranslatorState().activeLocale;
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
static seed(lang: string, dict: Dictionary | undefined) {
|
|
34
49
|
if (!dict) return;
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
|
|
38
|
-
const existingDictionary =
|
|
50
|
+
const state = getTranslatorState();
|
|
51
|
+
if (state.seededDicts.has(dict)) return;
|
|
52
|
+
state.seededDicts.add(dict);
|
|
53
|
+
const existingDictionary = state.langDictionaryMap.get(lang) ?? {};
|
|
39
54
|
Object.entries(dict).forEach(([key, modelDict]) => {
|
|
40
55
|
if (existingDictionary[key]) Object.assign(existingDictionary[key], modelDict);
|
|
41
56
|
else existingDictionary[key] = modelDict as Dictionary[string];
|
|
42
57
|
});
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
#setDictionary(lang: string, dict: Dictionary) {
|
|
46
|
-
Translator.seed(lang, dict);
|
|
47
|
-
return Translator.#langDictionaryMap.get(lang) as Dictionary;
|
|
58
|
+
state.langDictionaryMap.set(lang, existingDictionary);
|
|
48
59
|
}
|
|
49
60
|
translate(lang: string, key: string, param?: Record<string, string | number>): string {
|
|
50
|
-
const dictionary =
|
|
61
|
+
const dictionary = getTranslatorState().langDictionaryMap.get(lang);
|
|
51
62
|
if (!dictionary) return key;
|
|
52
63
|
const msg = (pathGet(key, dictionary, ".", { t: key }) as { t: string }).t;
|
|
53
64
|
return param ? msg.replace(/{([^}]+)}/g, (_, key: string) => param[key] as string) : msg;
|
|
54
65
|
}
|
|
55
66
|
async getDictionary(lang: string) {
|
|
56
|
-
const dictionary =
|
|
67
|
+
const dictionary = getTranslatorState().langDictionaryMap.get(lang);
|
|
57
68
|
if (!dictionary) throw new Error(`Dictionary for language ${lang} not found`);
|
|
58
69
|
return dictionary;
|
|
59
70
|
}
|
package/package.json
CHANGED
|
@@ -7,11 +7,10 @@ export interface AllDictionary {
|
|
|
7
7
|
[key: string]: Dictionary;
|
|
8
8
|
}
|
|
9
9
|
export declare class Translator {
|
|
10
|
-
#private;
|
|
11
|
-
static setActiveLocale(lang: string | undefined): void;
|
|
12
|
-
static getActiveLocale(): string | undefined;
|
|
13
10
|
constructor(dictionary: Record<string, Record<string, Record<string, unknown>>>);
|
|
14
11
|
hasDictionary(lang: string): boolean;
|
|
12
|
+
static setActiveLocale(lang: string | undefined): void;
|
|
13
|
+
static getActiveLocale(): string | undefined;
|
|
15
14
|
static seed(lang: string, dict: Dictionary | undefined): void;
|
|
16
15
|
translate(lang: string, key: string, param?: Record<string, string | number>): string;
|
|
17
16
|
getDictionary(lang: string): Promise<Dictionary>;
|