Files
keycloak_theme/src/lib/i18n/useKeycloakTranslation.tsx

36 lines
921 B
TypeScript
Raw Normal View History

import { useKeycloakLanguage } from "./useKeycloakLanguage";
import { messages } from "./generated_messages/login";
import { useConstCallback } from "powerhooks";
2021-03-01 20:45:37 +01:00
import type { ReactNode } from "react";
export type MessageKey = keyof typeof messages["en"]
export function useKeycloakThemeTranslation() {
const { keycloakLanguage } = useKeycloakLanguage();
const t = useConstCallback(
2021-03-01 20:45:37 +01:00
(key: MessageKey, ...args: (string | undefined)[]): ReactNode => {
let out: string = messages[keycloakLanguage as any as "en"][key] ?? messages["en"][key];
2021-03-01 20:45:37 +01:00
args.forEach((arg, i) => {
if (arg === undefined) {
return;
}
out = out.replace(new RegExp(`\\{${i}\\}`, "g"), arg);
});
return <span className={key} dangerouslySetInnerHTML={{ "__html": out }} />;
}
);
return { t };
}