Skip to content

Commit d4c21e5

Browse files
feat(core)!: make translator property in i18n service private. use set() to update it
1 parent 83cd30e commit d4c21e5

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

core/src/components/cat-i18n/cat-i18n-registry.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import log from 'loglevel';
22

3+
export type CatI18nTranslationFn = (key: string, params?: unknown) => string;
4+
35
export class CatI18nRegistry {
46
private static instance: CatI18nRegistry;
57

68
private readonly id = (Math.random() + 1).toString(36).substring(2);
79
private readonly i18n: Map<string, string> = new Map();
8-
private _locale?: string;
910

10-
_translator?: (key: string, params?: unknown) => string;
11+
private _locale?: string;
12+
private _translator?: CatI18nTranslationFn;
1113

1214
private constructor() {
1315
// hide constructor
@@ -60,10 +62,15 @@ export class CatI18nRegistry {
6062
}
6163
}
6264

63-
set(i18n: { [key: string]: string }, silent = false): void {
64-
const i18nEntries = Object.entries(i18n);
65-
i18nEntries.forEach(([key, message]) => this.i18n.set(key, message));
66-
log.info(`[CatI18nRegistry] Registered ${i18nEntries.length !== 1 ? 'messages' : 'message'}`);
65+
set(i18n: { [key: string]: string } | CatI18nTranslationFn, silent = false): void {
66+
if (typeof i18n === 'function') {
67+
this._translator = i18n;
68+
log.info(`[CatI18nRegistry] Registered translator`);
69+
} else {
70+
const i18nEntries = Object.entries(i18n);
71+
i18nEntries.forEach(([key, message]) => this.i18n.set(key, message));
72+
log.info(`[CatI18nRegistry] Registered ${i18nEntries.length !== 1 ? 'messages' : 'message'}`);
73+
}
6774
!silent && window.dispatchEvent(this.buildEvent('cat-i18n-set', { i18n, id: this.id }));
6875
}
6976

@@ -74,12 +81,14 @@ export class CatI18nRegistry {
7481
}
7582

7683
t(key: string, params?: { [key: string]: unknown }): string {
77-
const message = this._translator?.(key, params) ?? this.i18n.get(key);
84+
const message =
85+
this._translator?.(key, params) ??
86+
this.i18n.get(key)?.replace(/{{\s*([-a-zA-Z._]+)\s*}}/g, (_match, key) => `${params?.[key] ?? ''}`);
7887
if (message === undefined) {
7988
log.error(`[CatI18nRegistry] Unknown message key: ${key}`);
8089
return key;
8190
}
82-
return message.replace(/{{\s*([-a-zA-Z._]+)\s*}}/g, (_match, key) => `${params?.[key] ?? ''}`);
91+
return message;
8392
}
8493

8594
private buildEvent<T>(name: string, detail?: T) {

core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { Components, JSX } from './components';
2-
export { CatI18nRegistry, catI18nRegistry } from './components/cat-i18n/cat-i18n-registry';
2+
export { CatI18nRegistry, CatI18nTranslationFn, catI18nRegistry } from './components/cat-i18n/cat-i18n-registry';
33
export { CatIconRegistry, catIconRegistry } from './components/cat-icon/cat-icon-registry';
44
export {
55
CatNotificationService,

0 commit comments

Comments
 (0)