Improve i18n api typing

This commit is contained in:
Joseph Garrone 2024-12-06 00:38:09 +01:00
parent c323b94a8c
commit 60a9b5a693
3 changed files with 66 additions and 6 deletions

View File

@ -1,11 +1,9 @@
import { i18nBuilder } from "keycloakify/account";
import type { ThemeName } from "../kc.gen";
const { useI18n, ofTypeI18n } = i18nBuilder
.withThemeName<ThemeName>()
.withExtraLanguages({})
.withCustomTranslations({})
.build();
/** @see: https://docs.keycloakify.dev/i18n */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { useI18n, ofTypeI18n } = i18nBuilder.withThemeName<ThemeName>().build();
type I18n = typeof ofTypeI18n;

View File

@ -46,7 +46,7 @@ export type I18nBuilder<
}>
) => I18nBuilder<
ThemeName,
MessageKey_themeDefined,
string extends MessageKey_themeDefined ? never : MessageKey_themeDefined,
LanguageTag_notInDefaultSet,
ExcludedMethod | "withCustomTranslations"
>;

View File

@ -62,3 +62,65 @@ type I18n = typeof ofTypeI18n;
assert<Equals<typeof node, JSX.Element>>;
}
{
const i18n = Reflect<I18n>();
i18n.msg("passwordConfirm");
}
{
const i18n = Reflect<I18n>();
// @ts-expect-error
i18n.msg("iDoNotExist");
}
{
const { ofTypeI18n } = i18nBuilder
.withThemeName<"keycloakify-starter">()
.withCustomTranslations({})
.build();
type I18n = typeof ofTypeI18n;
{
const i18n = Reflect<I18n>();
// @ts-expect-error
const node = i18n.msg("iDoNotExist");
assert<Equals<typeof node, JSX.Element>>;
}
}
i18nBuilder.withThemeName<"my-theme-1" | "my-theme-2">().withCustomTranslations({
en: {
myCustomKey1: "my-custom-key-1-en",
// @ts-expect-error
myCustomKey2: {
"my-theme-1": "my-theme-1-en"
//"my-theme-2": "my-theme-2-en"
}
}
});
i18nBuilder
.withThemeName<"my-theme-1" | "my-theme-2">()
.withExtraLanguages({
he: {
label: "עברית",
getMessages: () => import("./he")
}
})
.withCustomTranslations({
en: {
myCustomKey1: "my-custom-key-1-en",
myCustomKey2: "my-custom-key-2-en"
},
// @ts-expect-error
he: {
myCustomKey1: "my-custom-key-1-he"
//myCustomKey2: "my-custom-key-2-he"
}
});