-
In React, is there a way I can pull a translation using a key? For example assuming I have the translations mapped correctly in the
As you describe in the example https://github.com/ivanhofer/typesafe-i18n/tree/main/packages/runtime#arrays The reason is in my forms I'm using the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Sure, you can do that. But keep in mind, that you should type the import type { Locales, Translation } from './i18n/i18n-types'
import { useI18nContext } from './i18n/i18n-react'
const { LL } = useI18nContext()
// basic
let name: keyof Translation = '...'
return <>{LL[name]()}</>
// or nested
let name: keyof Translation['formValidation'] = '...'
return <>{LL.formValidation[name]()}</> Please also keep in mind that all the translations you are trying to access should have the same "shape". If you are passing arguments, they should have the same name and all translations should take the same amount of arguments, or else you will get TypeScript errors that you have to handle with |
Beta Was this translation helpful? Give feedback.
Sure, you can do that. But keep in mind, that you should type the
name
property accordingly to make sure you don't try to access an unknown translation, which result in an empty string beeing rendered.Please also keep in mind that all the translations you are trying to access should have the same "shape". If you are passing arguments, they should have the same name …