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:
JavaScript
x
6
1
<div class="i18n" data-i18n="content.body">
2
In Medellín they have many different types of <i>jugos naturales</i> (fruit juice) ... <br />
3
<br />
4
5
</div>
6
The translation code looks like this:
JavaScript
1
15
15
1
var resources = {
2
"en": ,
3
"es": {
4
"translation": {
5
"content": {
6
"body": "En Medellín hay varios tipos diferentes de <i>jugos naturales</i> ... <br /><br /> ... "
7
}
8
}
9
}
10
}
11
12
i18n.init({"resStore": resources}, function( t ) {
13
$('.i18n').i18n();
14
});
15
When the translation is rendered, HTML tags are escaped and output as text:
JavaScript
1
2
1
En Medellín hay varios tipos diferentes de <i>jugos naturales</i>...<br /><br />
2
How do I get i18next to change the HTML of translated elements?
Advertisement
Answer
In order to make this work, you have to prefix the data-i18n
attribute of the elements you want to translate with [html]
:
JavaScript
1
2
1
<div class="i18n" data-i18n="[html]content.body">
2
Source: i18next.jquery.js