Better i18n API

This commit is contained in:
garronej
2023-03-19 14:03:06 +01:00
parent 0608adde89
commit 819f297de8
2 changed files with 100 additions and 97 deletions

View File

@ -69,11 +69,11 @@ export type GenericI18n<MessageKey extends string> = {
export type I18n = GenericI18n<MessageKeyBase>;
export function useGenericI18n<ExtraMessageKey extends string = never>(params: {
kcContext: KcContextLike;
extraMessages: { [languageTag: string]: { [key in ExtraMessageKey]: string } };
}): GenericI18n<MessageKeyBase | ExtraMessageKey> | null {
const { kcContext, extraMessages } = params;
export function createUseI18n<ExtraMessageKey extends string = never>(extraMessages: {
[languageTag: string]: { [key in ExtraMessageKey]: string };
}) {
function useI18n(params: { kcContext: KcContextLike }): GenericI18n<MessageKeyBase | ExtraMessageKey> | null {
const { kcContext } = params;
const [i18n, setI18n] = useState<GenericI18n<ExtraMessageKey | MessageKeyBase> | undefined>(undefined);
@ -178,6 +178,9 @@ export function useGenericI18n<ExtraMessageKey extends string = never>(params: {
}, []);
return i18n ?? null;
}
return { useI18n };
}
function createI18nTranslationFunctions<MessageKey extends string>(params: {

View File

@ -5,6 +5,6 @@ export default Fallback;
export { createKeycloakAdapter } from "keycloakify/lib/keycloakJsAdapter";
export { useDownloadTerms } from "keycloakify/lib/useDownloadTerms";
export { getKcContext } from "keycloakify/kcContext/getKcContext";
export { useGenericI18n } from "keycloakify/i18n";
export { createUseI18n } from "keycloakify/i18n";
export type { PageProps } from "keycloakify/pages/PageProps";