Skip to content

Commit c42461e

Browse files
committed
refactor: i18n implementation
1 parent 9b786af commit c42461e

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

packages/runtime-core/src/i18n.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@ export type LanguageChangeHandler = (this: void, language: Language) => void;
3131
/** @public */
3232
export class Translator<T extends { [key: string]: unknown } = { [key: string]: unknown }> {
3333
isInitialized: boolean;
34-
#fallbackLanguage: Language;
35-
#handleLanguageChange: LanguageChangeHandler | null;
36-
#resolvedLanguage: Language;
37-
#translations: T;
34+
private currentDocumentLanguage: Language | null;
35+
private fallbackLanguage: Language;
36+
private handleLanguageChange: LanguageChangeHandler | null;
37+
private translations: T;
3838

3939
constructor(options: { fallbackLanguage?: Language; translations: T }) {
4040
this.isInitialized = false;
41-
this.#fallbackLanguage = options.fallbackLanguage ?? 'en';
42-
this.#handleLanguageChange = null;
43-
this.#resolvedLanguage = this.#fallbackLanguage;
44-
this.#translations = options.translations;
41+
this.currentDocumentLanguage = null;
42+
this.fallbackLanguage = options.fallbackLanguage ?? 'en';
43+
this.handleLanguageChange = null;
44+
this.translations = options.translations;
4545
}
4646

4747
@InitializedOnly
4848
set onLanguageChange(handler: LanguageChangeHandler) {
49-
this.#handleLanguageChange = handler;
49+
this.handleLanguageChange = handler;
5050
}
5151

5252
@InitializedOnly
5353
get resolvedLanguage() {
54-
return this.#resolvedLanguage;
54+
return this.currentDocumentLanguage ?? this.fallbackLanguage;
5555
}
5656

5757
@InitializedOnly
@@ -67,7 +67,7 @@ export class Translator<T extends { [key: string]: unknown } = { [key: string]:
6767
}
6868

6969
this.isInitialized = true;
70-
this.#resolvedLanguage = this.extractLanguageProperty(window.frameElement);
70+
this.currentDocumentLanguage = this.extractLanguageProperty(window.frameElement);
7171

7272
if (options?.onLanguageChange) {
7373
this.onLanguageChange = options.onLanguageChange;
@@ -76,8 +76,8 @@ export class Translator<T extends { [key: string]: unknown } = { [key: string]:
7676
const languageAttributeObserver = new MutationObserver((mutations) => {
7777
mutations.forEach((mutation) => {
7878
if (mutation.attributeName === 'lang') {
79-
this.#resolvedLanguage = this.extractLanguageProperty(mutation.target as Element);
80-
this.#handleLanguageChange?.(this.#resolvedLanguage);
79+
this.currentDocumentLanguage = this.extractLanguageProperty(mutation.target as Element);
80+
this.handleLanguageChange?.(this.resolvedLanguage);
8181
}
8282
});
8383
});
@@ -87,11 +87,11 @@ export class Translator<T extends { [key: string]: unknown } = { [key: string]:
8787

8888
@InitializedOnly
8989
t(key: TranslationKey<T>) {
90-
const value = get(this.#translations, key) as { [key: string]: string } | string | undefined;
90+
const value = get(this.translations, key) as { [key: string]: string } | string | undefined;
9191
if (typeof value === 'string') {
9292
return value;
9393
}
94-
return value?.[this.resolvedLanguage] ?? value?.[this.#fallbackLanguage] ?? key;
94+
return value?.[this.resolvedLanguage] ?? value?.[this.fallbackLanguage] ?? key;
9595
}
9696

9797
@InitializedOnly
@@ -101,6 +101,6 @@ export class Translator<T extends { [key: string]: unknown } = { [key: string]:
101101
return lang;
102102
}
103103
console.error(`Unexpected value for 'lang' attribute: '${lang}'`);
104-
return this.#fallbackLanguage;
104+
return null;
105105
}
106106
}

0 commit comments

Comments
 (0)