-
Hello, in one of my namespace json files I have long text split as an array so it's more readable. {
"long-text": "some real long text"
} => {
"long-text": [
"some",
"real",
"long",
"text"
]
} It also contains components that needs to be inserted. {
"long-text": [
"some",
"<0>real</0>",
"long",
"text"
]
} So I have to use the <Trans
i18nKey="ns:long-text"
components={[<br/>]}
values={{price: config.price}}/> Obviously it doesn't render. But as far as I know this is not possible with the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@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 contents of the array. Within the I18nProvider you can use all the namespaces you already had plus the new one, they all fit together. Another example with function AnotherExample() {
const { t } = useTranslation('ns')
const longText = t('long-text', {}, { returnObjects: true }).join(' ')
return (
<I18nProvider namespaces={{ newNs: { longText } }}>
<Trans i18nKey="newNs:longText" components={[<b />]} />
</I18nProvider>
)
} |
Beta Was this translation helpful? Give feedback.
@lkgoerges I suppose something like that would work, although it's not very elegant....
Basically, it creates a new provider with a new namespace with the content…