Skip to content

Commit 3afae89

Browse files
committed
refactor: implement BaseTranslator
1 parent c42461e commit 3afae89

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

packages/runtime-core/src/i18n.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { get } from 'lodash-es';
22

33
import type { Language } from './types/core.js';
44

5-
function InitializedOnly<T extends Translator, TArgs extends any[], TReturn>(
5+
function InitializedOnly<T extends BaseTranslator, TArgs extends any[], TReturn>(
66
target: (this: T, ...args: TArgs) => TReturn,
77
context: ClassGetterDecoratorContext<T> | ClassMethodDecoratorContext<T> | ClassSetterDecoratorContext<T>
88
) {
@@ -28,16 +28,27 @@ export type TranslationKey<T extends { [key: string]: unknown }, Key = keyof T>
2828
/** @public */
2929
export type LanguageChangeHandler = (this: void, language: Language) => void;
3030

31-
/** @public */
32-
export class Translator<T extends { [key: string]: unknown } = { [key: string]: unknown }> {
31+
export abstract class BaseTranslator<T extends { [key: string]: unknown } = { [key: string]: unknown }> {
32+
protected abstract currentDocumentLanguage: Language | null;
33+
protected abstract fallbackLanguage: Language;
34+
protected abstract handleLanguageChange: LanguageChangeHandler | null;
3335
isInitialized: boolean;
34-
private currentDocumentLanguage: Language | null;
35-
private fallbackLanguage: Language;
36-
private handleLanguageChange: LanguageChangeHandler | null;
37-
private translations: T;
36+
protected abstract translations: T;
3837

39-
constructor(options: { fallbackLanguage?: Language; translations: T }) {
38+
constructor() {
4039
this.isInitialized = false;
40+
}
41+
}
42+
43+
/** @public */
44+
export class Translator<T extends { [key: string]: unknown }> extends BaseTranslator<T> {
45+
protected currentDocumentLanguage: Language | null;
46+
protected fallbackLanguage: Language;
47+
protected handleLanguageChange: LanguageChangeHandler | null;
48+
protected translations: T;
49+
50+
constructor(options: { fallbackLanguage?: Language; translations: T }) {
51+
super();
4152
this.currentDocumentLanguage = null;
4253
this.fallbackLanguage = options.fallbackLanguage ?? 'en';
4354
this.handleLanguageChange = null;

0 commit comments

Comments
 (0)