I am working with 2 objects and I need to dynamically generate a string based on the selected key. The second part of the generated string should be a key within the object associated with the selected key. In this scenario, only "test" or "test2" should be allowed as options after the "." in the code snippet t("common.")
.
const common = {
"test": "Test",
"test2": "Test2"
}
const greetings = {
"hello": "Hello"
}
export type I18nMap = Record<typeof locales[number], I18nNamespace>;
export interface I18nNamespace {
common: typeof common;
greetings: typeof greetings;
}
export const locales = (["en", "nl"] as const);
type Interpolation = Record<string, string | number>
export function useTranslation<
T extends keyof I18nNamespace,
U extends T extends T ? keyof I18nNamespace[T] : never,
V extends `${T}.${U}`
>(namespace: T | T[]): {t: (key: V, interpolation?: Interpolation) => void} {
// ...
}
const { t } = useTranslation(["common", "greetings"])
// Only allow common.test | common.test2
t("common.")