'Pass dynamic value into react-i18next useTranslate

So I have a value I am receiving from an endpoint and I'd like to pass it into my translate command.

So this is what I have currently: ${t('translation:user.form.placeholder')}

And I'd like to be able to do something like this: ${t('translation:user.form.${placeholder}')}

Is there a way to do this? I'm happy to provide more clarity to the question if needed. Any help would be appreciated.



Solution 1:[1]

Looking at the question I am assuming you want to interpolate a string with a dynamic value. For instance

{
    "translation:user.form": "Some text with {{dynamicValue}}"
}

This dynamicValue can be replaced with second param options which a string or TOptions<StringMap>.

    const { t } = useTranslation();
    ...
    t('translation:user.form', {dynamicValue: 'Some value or variable'})

Here is the doc for interpolation https://www.i18next.com/translation-function/interpolation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pramod Mali