Search code examples
reactjsi18nextreact-i18next

React-i18next how to put dynamic html content?


The result I want to see is as follows

Modules will be public on app.example.com

only the URL part should be dynamic and clickable with a html tag

my language file:

{
  "moduleWillBePublicOn" : "Modules will be public on <1>Portal URL</1>" 
}

my code:

const ShowModuleUrl = ({ moduleUrl }) => {
   return (
      <Trans i18nKey="moduleWillBePublicOn">
         Modules will be public on 
         <a href={moduleUrl}>{moduleUrl}</a>
      </Trans>
   );
};

The output I got is as follows

enter image description here

the link it goes to is correct. but I want to print moduleUrl variable instead of "Portal URL" like this

enter image description here

How can I get output like in the 2nd picture


Solution

  •  <Trans
         i18nKey="moduleWillBePublicOn"
         values={{ moduleUrl: moduleUrl}}
         components={[
            <a href={moduleUrl} target="_blank" rel="noreferrer">
               {moduleUrl}
            </a>,
         ]}>
      </Trans>