Skip to content

Commit ad0d93d

Browse files
committed
refactor: i18n
1 parent 27c8486 commit ad0d93d

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

packages/runtime-core/src/i18n.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,25 @@ export abstract class BaseTranslator<T extends { [key: string]: unknown } = { [k
8585
return null;
8686
}
8787

88-
abstract init(options?: TranslatorInitOptions): void;
88+
init(options: TranslatorInitOptions, targetElement: Element) {
89+
this.isInitialized = true;
90+
this.currentDocumentLanguage = this.extractLanguageProperty(targetElement);
91+
92+
if (options?.onLanguageChange) {
93+
this.onLanguageChange = options.onLanguageChange;
94+
}
95+
96+
const languageAttributeObserver = new MutationObserver((mutations) => {
97+
mutations.forEach((mutation) => {
98+
if (mutation.attributeName === 'lang') {
99+
this.currentDocumentLanguage = this.extractLanguageProperty(mutation.target as Element);
100+
this.handleLanguageChange?.(this.resolvedLanguage);
101+
}
102+
});
103+
});
104+
105+
languageAttributeObserver.observe(targetElement, { attributes: true });
106+
}
89107

90108
@InitializedOnly
91109
t(key: TranslationKey<T>) {
@@ -103,37 +121,25 @@ export class SynchronizedTranslator<T extends { [key: string]: unknown }> extend
103121
super(options);
104122
}
105123

124+
@InitializedOnly
125+
get targetElement() {
126+
return window.frameElement!;
127+
}
128+
106129
@InitializedOnly
107130
changeLanguage(language: Language) {
108131
window.top!.document.dispatchEvent(new CustomEvent('changeLanguage', { detail: language }));
109132
}
110133

111-
init(options?: TranslatorInitOptions) {
134+
override init(options: TranslatorInitOptions = {}) {
112135
if (typeof window === 'undefined') {
113136
throw new Error('Cannot initialize SynchronizedTranslator outside of browser');
114137
} else if (!window.frameElement) {
115138
throw new Error('Cannot initialize SynchronizedTranslator in context where window.frameElement is null');
116139
} else if (window.frameElement.getAttribute('name') !== 'interactive-instrument') {
117140
throw new Error('SynchronizedTranslator must be initialized in InstrumentRenderer');
118141
}
119-
120-
this.isInitialized = true;
121-
this.currentDocumentLanguage = this.extractLanguageProperty(window.frameElement);
122-
123-
if (options?.onLanguageChange) {
124-
this.onLanguageChange = options.onLanguageChange;
125-
}
126-
127-
const languageAttributeObserver = new MutationObserver((mutations) => {
128-
mutations.forEach((mutation) => {
129-
if (mutation.attributeName === 'lang') {
130-
this.currentDocumentLanguage = this.extractLanguageProperty(mutation.target as Element);
131-
this.handleLanguageChange?.(this.resolvedLanguage);
132-
}
133-
});
134-
});
135-
136-
languageAttributeObserver.observe(window.frameElement, { attributes: true });
142+
return super.init(options, window.frameElement);
137143
}
138144
}
139145

0 commit comments

Comments
 (0)