akanjs 2.2.7-rc.0 → 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
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
|
}
|
|
@@ -390,7 +390,6 @@ export class FetchClient {
|
|
|
390
390
|
return endpoint;
|
|
391
391
|
}
|
|
392
392
|
#registerModelBaseEndpoint(refName: string, signal: SerializedSignal) {
|
|
393
|
-
const cnst = ConstantRegistry.getDatabase(refName);
|
|
394
393
|
const capRefName = capitalize(refName);
|
|
395
394
|
const names = {
|
|
396
395
|
createModel: `create${capRefName}`,
|
|
@@ -416,6 +415,7 @@ export class FetchClient {
|
|
|
416
415
|
names.viewModel,
|
|
417
416
|
() =>
|
|
418
417
|
(async (id: string, option?: FetchPolicy) => {
|
|
418
|
+
const cnst = ConstantRegistry.getDatabase(refName);
|
|
419
419
|
const modelFn = this.#requireHandler(names.model, names.viewModel);
|
|
420
420
|
const modelObj = await modelFn(id, { ...option, crystalize: false });
|
|
421
421
|
const model = new cnst.full(modelObj as object);
|
|
@@ -438,6 +438,7 @@ export class FetchClient {
|
|
|
438
438
|
names.editModel,
|
|
439
439
|
() =>
|
|
440
440
|
(async (id: string, option?: FetchPolicy) => {
|
|
441
|
+
const cnst = ConstantRegistry.getDatabase(refName);
|
|
441
442
|
const modelFn = this.#requireHandler(names.model, names.editModel);
|
|
442
443
|
const modelObj = await modelFn(id, { ...option, crystalize: false });
|
|
443
444
|
const model = new cnst.full(modelObj as object);
|
|
@@ -516,7 +517,6 @@ export class FetchClient {
|
|
|
516
517
|
return endpoint;
|
|
517
518
|
}
|
|
518
519
|
#registerSlice(refName: string, suffix: string, slice: SerializedSlice, prefix?: string) {
|
|
519
|
-
const cnst = ConstantRegistry.getDatabase(refName);
|
|
520
520
|
const capSuffix = capitalize(suffix);
|
|
521
521
|
const sliceName = `${refName}${capSuffix}`;
|
|
522
522
|
const capRefName = capitalize(refName);
|
|
@@ -535,6 +535,7 @@ export class FetchClient {
|
|
|
535
535
|
const argLength = slice.args.length;
|
|
536
536
|
this.slice[sliceName] = { refName, sliceName, argLength };
|
|
537
537
|
this.#handlerFactory.set(names.init, () => async (...argData: unknown[]) => {
|
|
538
|
+
const cnst = ConstantRegistry.getDatabase(refName);
|
|
538
539
|
const queryArgs = normalizeQueryArgs(
|
|
539
540
|
Array.from({ length: Math.min(argData.length, argLength) }, (_, idx) => argData[idx]),
|
|
540
541
|
slice.args,
|
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>;
|