feat: translators

This commit is contained in:
tresabhi committed 2025-01-03 12:08:03 -06:00
1 parent d6e7f35214
commit 309fda1ed1
1 file changed
+26 -33
+26 -33
View File
@@ -1,37 +1,34 @@
import en from '@blitzkit/core/lang/en.json'; import en from '@blitzkit/core/lang/en.json';
import ru from '@blitzkit/core/lang/ru.json'; import ru from '@blitzkit/core/lang/ru.json';
type Translation = string | TranslationTree;
type TranslationTree = { [key: string]: Translation }; type TranslationTree = { [key: string]: Translation };
// type Options<DefaultLocale extends string> = { type Translation = string | TranslationTree;
// defaultLocale: DefaultLocale;
// };
// export function prate< type NestedKeys<Type> = Type extends object
// Locale extends string, ? {
// DefaultLocale extends Locale, [Key in keyof Type]: Key extends string
// >( ? Type[Key] extends object
// strings: Record<Locale, Translation>, ? '$' extends keyof Type[Key]
// options: Partial<Options<DefaultLocale>>, ? `${Key}` | `${Key}.${NestedKeys<Type[Key]>}`
// ) { : `${Key}.${NestedKeys<Type[Key]>}`
// function : Key
// } : never;
}[keyof Type]
: never;
// const {prate }= prate({ en, ru }, { defaultLocale: 'en' }); class Prate<
Locale extends string,
class Prate<Locale extends string, DefaultLocale extends Locale> { DefaultLocale extends Locale,
private readonly strings = new Map<Locale, TranslationTree>(); Strings extends TranslationTree,
Path extends NestedKeys<Strings>,
> {
private readonly defaultStrings: Map<string, string>; private readonly defaultStrings: Map<string, string>;
constructor( constructor(
strings: Record<Locale, TranslationTree>, public strings: Record<Locale, Strings>,
public defaultLocale: DefaultLocale, public defaultLocale: DefaultLocale,
) { ) {
for (const locale in strings) {
this.strings.set(locale, strings[locale]);
}
this.defaultStrings = this.flatten(strings[defaultLocale]); this.defaultStrings = this.flatten(strings[defaultLocale]);
} }
@@ -56,17 +53,17 @@ class Prate<Locale extends string, DefaultLocale extends Locale> {
} }
locale(locale: Locale) { locale(locale: Locale) {
const strings = this.strings.get(locale); if (this.strings[locale] === undefined) {
throw new TypeError(`Locale "${locale}" is not defined`);
}
const strings = this.strings[locale];
const defaultStrings = this.defaultStrings; const defaultStrings = this.defaultStrings;
const defaultLocale = this.defaultLocale; const defaultLocale = this.defaultLocale;
if (strings === undefined) {
throw new TypeError(`Locale "${locale}" is not undefined`);
}
const translations = this.flatten(strings); const translations = this.flatten(strings);
function translate(path: string, literals?: string[]) { function translate(path: Path, literals?: string[]) {
let translation = translations.get(path) ?? translations.get(`${path}.$`); let translation = translations.get(path) ?? translations.get(`${path}.$`);
if (translation === undefined) { if (translation === undefined) {
@@ -91,12 +88,8 @@ class Prate<Locale extends string, DefaultLocale extends Locale> {
return translation; return translation;
} }
return { translate }; return { translate, t: translate };
} }
} }
const prate = new Prate({ en, ru }, 'en'); const prate = new Prate({ en, ru }, 'en');
const { translate } = prate.locale('ru');
console.log(translate('bot.common.wn8'));