Dynamic message keys #1607
-
Hi, I have am trying to get the mesages dynamically like this: // defined in messages.ts
export default {
PageTitle: {
new: {...},
reports: {...},
...
}
}
const titleTranslations = useTranslations('PageTitles')
const title =
props.title ?? titleTranslations(`${pathname.split('/').at(-1) as typeof keys}.title`) But am having a hard time doing it because TypeScript can't stop complaining about it. It works if I explicitly define the variable to be a certain key. Is there some way I can type or cast this to work with dynamic keys? Any help is highly appreciated! 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I actually got the same problem and i dont know how to fix that ... |
Beta Was this translation helpful? Give feedback.
-
Same problem here |
Beta Was this translation helpful? Give feedback.
-
Try this
en.json = |
Beta Was this translation helpful? Give feedback.
Try this
type Theme = "dark" | "light" | "system";
{t('general.theme.${theme as Theme}')}
en.json =
{ "theme": { "title": "Theme", "dark": "Dark", "light": "Light", "system": "System" }, }
it worked with me