Node.js: is there no way to put HTML in i18n-node JSON translation files?

This question says it all. If I put the HTML directly in the translation file (JSON formatted), for example:

"test_html" : "click <a href='http://stackoverflow.com/'>here</a>", 

I get this in my HTML:

 click &lt;a href=&#39;http://stackoverflow.com/&#39;&gt;here&lt;/a&gt; 

I also tried combining this in my translation file:

 "test_html_placeholder" : "click %shere%s", 

With this in my HTML:

 <%= __('test_html_placeholder', '<a href="http://stackoverflow.com">', '</a>') %> 

But similar results were obtained.

The only thing I can find is clumsiness:

 "test_html_pre" : "click ", "test_html_link" : "here", "test_html_post" : ".", 

with this:

 <%= __('test_html_pre') %><a href="http://stackoverflow.com"><%= __('test_html_link') %></a><%= __('test_html_post') %> 

But this is so cumbersome that it is almost not worth doing, and, in addition, the word order in some languages ​​forced me to put some empty lines in my translation files, which i18n-node apparently do not like, gives the name of the key (attribute), when it encounters an empty string.

I also tried using the "\" character as an escape character in front of the characters, but I received an invalid JSON error when I raised the sails (restarting the server).

Any ideas, workarounds? I use sails.js, this is not my solution, but I am stuck in it and it comes with i18n-node. At the end of this day in this project I would like to consider the possibility of using another library, but I do not completely exclude it.

+7
html anchor
source share
2 answers

next to any upcoming discussion on whether to include (html-) code in language files or not:

try using

 <%- __('<a href="#">click</a>') %> 

instead

 <%= __('<a href="#">click</a>') %> 

in ejs (the default template engine for sails) a '<% =' will avoid any html tags, while '<% -' puts the output as if not touching it. I am sure you will find unescaped html in your .json files. i18n does not perform any conversion except JSON.stringify() , but almost all template engines exclude strings by default to prevent xssi.

+9
source share

For those using pug / jade, you can use

 !{ __('key_for_your_text') } 
0
source share

All Articles