Skip to content

Dynamically pull a translation by reference #407

Answered by ivanhofer
proddy asked this question in Q&A
Discussion options

You must be logged in to vote

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.

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 …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@proddy
Comment options

Answer selected by ivanhofer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants