HTML tags in i18next translation

JavascriptInternationalizationI18next

Javascript Problem Overview


I'm using i18next to power i18n for my weblog. It works great on text-only content, but when I try to translate content that includes HTML markup, it is displaying the raw markup when I translate the text.

As an example, here is a snippet of the markup from a post that is not working as expected:

<div class="i18n" data-i18n="content.body">
  In Medellín they have many different types of <i>jugos naturales</i>&nbsp;(fruit juice) ... <br />
  <br />
  ...
</div>

The translation code looks like this:

var resources = {
  "en": ...,
  "es": {
    "translation": {
      "content": {
        "body": "En Medellín hay varios tipos diferentes de <i>jugos naturales</i> ... <br /><br /> ... "
      }
    }
  }
}

i18n.init({"resStore": resources}, function( t ) {
  $('.i18n').i18n();
});

When the translation is rendered, HTML tags are escaped and output as text:

En Medellín hay varios tipos diferentes de &lt;i&gt;jugos naturales&lt;/i&gt;...&lt;br /&gt;&lt;br /&gt;

How do I get i18next to change the HTML of translated elements?

Javascript Solutions


Solution 1 - Javascript

In order to make this work, you have to prefix the data-i18n attribute of the elements you want to translate with [html]:

<div class="i18n" data-i18n="[html]content.body">

Source: i18next.jquery.js

Solution 2 - Javascript

For anyone trying to do this in React (for example with react-i18-next), be aware that React also escapes the string! So we have to tell both i18n and React not to escape the string.

  • To tell i18n to skip escaping, we use {escapeValue: false} as others have shown.

  • To tell React to skip escaping, we use React's dangerouslySetInnerHTML attribute.

<div dangerouslySetInnerHTML={
    {__html: t('foo', {interpolation: {escapeValue: false}})}
} />

That attribute accepts an object with one property __html. React intentionally made it ugly, to discourage its use, because not escaping can be dangerous.

For security, raw user input should not be used inside this element. If you do need to use an untrusted string here, be sure to sanitise or escape it, so the user cannot inject raw < or > into the page.

Solution 3 - Javascript

You need to turn off escaping:

i18n.t("key", { 'interpolation': {'escapeValue': false} })

Solution 4 - Javascript

Don't put the HTML tags in the translation. It's a bad idea anyway. Separation of concerns guys will be all whiny about it.

Use the <Trans> component if react-i18next [https://react.i18next.com/latest/trans-component][1]

Do like so:

// Component.js

<Trans>Welcome, <strong>User!</strong>, here's your <strong>broom</strong></Trans>

And the corresponding translation file:

// your locales/starwars_en.js file

translations: {
  "Welcome, <1>User!</1>, here's your <3>broom</3>": "Welcome, <1>young padawan!</1>, here's your <3>light saber</3>",
}

These numbers <1> and <3> will strike you as random but wait for it:

Trans.children = [
  'Welcome, ',                        // index 0
  '<strong>User!</strong>'            // index 1
  ', please don't be ',               // index 2
  '<strong>weak</strong>',            // index 3
  ' unread messages. ',               // index 4
]

More info here: https://stackoverflow.com/a/62563302/537648

Solution 5 - Javascript

i18n.t('key',{dateStr: date, interpolation: {escapeValue: false}})

worked for me if date='15/10/2020', slashes were kept as well

Solution 6 - Javascript

From the documentation:

> Hint 3: Escaping: > > // given resources > {
> 'en-US': { > translation: { > key1: Not escaped nameHTML, > key2: Escaped name > } > } > }; > > i18n.t("key2", { name: '', escapeInterpolation: true }); // -> Escaped <tag> > i18n.t("key1", { name: '', escapeInterpolation: false }); // -> Not escaped > > Adding suffix 'HTML__' to your value will prevent the escaping even if > option is set so. > > You could turn on escaping on init i18n.init({escapeInterpolation: > true}); or by passing in option to t function like in the sample.

Solution 7 - Javascript

You can turn off the escaping during initialization globally:

i18n.init({
  // ...
  interpolation: {
    escapeValue: false,
  }
});

Solution 8 - Javascript

I'm using both React and React Native. For the React app The solution works. Not for React Native.

React
i18n.js

interpolation: {
      escapeValue: false   
    }

somefile.jsx

<div dangerouslySetInnerHTML={{__html: t('dropZone.description')}} />

React Native
This solution does not work because < div> is not allowed within a Text tag. I tried to add the dangerouslySetInnerHTML to the Text tag, but then nothing is visible.

Does someone have a solution for React Native?

Solution 9 - Javascript

> This is an answer for React.

Eventually found a straight forward solution that can make code easier to maintain. Hope this helps!

{
  "stackoverflow": "<site_anchor>Welcome to stackoverflow!</site_anchor> You can also <profile_anchor>check my profile</profile_anchor>" 
}
import {Trans} from "react-i18next";
<Trans i18nKey="stackoverflow" components={{
  site_anchor: <Link href="https://stackoverflow.com" target="_blank"/>,
  profile_anchor: <Link href="https://stackoverflow.com/users/18131146/mondayrris" target="_blank"/>,
}} />

Solution 10 - Javascript

Solution in ReactJS:

Initial component:

...
<Typography>Hello <b>World</b><br />This is an <strong>example</strong></Typography>
...

Using Interweave with react-i18next

...
import { Markup } from "interweave"
...
<Typography><Markup content={t("paragraph")} /></Typography>
...

And your en.json file:

{
 "paragraph": "Hello <b>World</b><br />This is an <strong>example</strong>"
}

es.json:

{
 "paragraph": "Hola <b>Mundo</b><br />Esto es un <strong>ejemplo</strong>"
}

More information: Here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser212218View Question on Stackoverflow
Solution 1 - Javascriptuser212218View Answer on Stackoverflow
Solution 2 - JavascriptjoeytwiddleView Answer on Stackoverflow
Solution 3 - JavascriptDotevoView Answer on Stackoverflow
Solution 4 - Javascriptviktor_vangelView Answer on Stackoverflow
Solution 5 - JavascriptgarboView Answer on Stackoverflow
Solution 6 - JavascriptborisdiakurView Answer on Stackoverflow
Solution 7 - JavascriptFinesseView Answer on Stackoverflow
Solution 8 - JavascriptAnniek van DijkView Answer on Stackoverflow
Solution 9 - JavascriptmondayrrisView Answer on Stackoverflow
Solution 10 - JavascriptCamilo GomezView Answer on Stackoverflow