Skip to content

Use Trans-component on array #626

Answered by aralroca
lkgoerges asked this question in Q&A
Discussion options

You must be logged in to vote

@lkgoerges I suppose something like that would work, although it's not very elegant....

import useTranslation from 'next-translate/useTranslation'
import DynamicNamespaces from 'next-translate/DynamicNamespaces'
import Trans from 'next-translate/Trans'

function Example() {
  const { t } = useTranslation('ns')
  // array to object ['a', 'b'] to { 0: 'a', 1: 'b' }
  const longTextNs = { ...t('long-text', {}, { returnObjects: true }) }

  return (
    <I18nProvider namespaces={{ longTextNs }}>
      <Trans
        i18nKey="longTextNs:1" // by array index
        components={[<b />]}
      />
    </I18nProvider>
  )
}

Basically, it creates a new provider with a new namespace with the content…

Replies: 1 comment 1 reply

Comment options

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

Answer selected by lkgoerges
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