node.js:有没有办法将HTML放入i18n-node JSON转换文件中? [英] node.js: is there no way to put HTML into i18n-node JSON translation files?

查看:155
本文介绍了node.js:有没有办法将HTML放入i18n-node JSON转换文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题说明了一切。如果我将HTML直接放入(JSON格式)的翻译文件中,如下所示:

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

我在HTML中得到这个:

  click& lt; a href =& lt; apache href =&#39; http:// stackoverflow的.com /&安培;#39;&安培; gt;此处&安培; LT; / A&安培; GT; 

我也尝试在我的翻译文件中合并:

 test_html_placeholder:点击%shere%s,

有了这个在我的HTML中:

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

但是得到了相似的结果。

只有我可以开始工作的是这种笨拙:

 test_html_pre:click,
test_html_link :here,
test_html_post:。,

用这个:

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

但是它太麻烦了,几乎不值得去做,而且某些语言的单词顺序会迫使我在我的翻译文件中放入一些空字符串,i18n-node似乎不喜欢它,因为它在
遇到空字符串时将键名(属性)名称吐出。



我也尝试在符号前使用\作为转义字符,但是当我解除sails(重新启动服务器)时,我收到了无效的JSON错误。



任何想法,解决方法?我使用sails.js,这不是我的决定,但我坚持它,它与i18n节点。在这个项目当天晚些时候有人会考虑使用另一个库,但并不是完全没有问题。

讨论是否在语言文件中包含(html-)代码:

尝试使用

 <% -  __('< a href =#>点击< / a>')%> 

而不是

 <%= __('< a href =#>点击< / a>')%> 

在ejs(sails默认模板引擎)中,'<%='会转义任何html标签而'<% - '按原样输出输出而不触及它。我很肯定你会在你的.json文件中找到非转义的html。除了 JSON.stringify()之外,i18n不会进行任何转换,但几乎所有模板引擎都会默认转义字符串以防止xssi。


The question says it all. If I put HTML directly into the (JSON-formatted) translation file, like this:

"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 got similar results.

The only thing I can get to work is this 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 it's so cumbersome as to be almost not worth doing, and moreover the word order in some languages would force me to put some empty strings in my translation files, which i18n-node doesn't seem to like as it spits out the key (attribute) name when it encounters an empty string.

I also tried using "\" as an escape character in front of the symbols, but I got an invalid JSON error when I lifted sails (restarted the server).

Any ideas, workarounds? I'm using sails.js, it wasn't my decision but I'm stuck with it and it comes with i18n-node. It's kind of late in the day on this project to consider using another library, but not completely out of the question.

解决方案

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

try to use

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

instead of

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

in ejs (the sails default template engine) a '<%=' will escape any html tags while '<%-' puts output as is without touching it. I am pretty sure you'll find unescaped html in your .json files. i18n doesn't do any transformation other than JSON.stringify() but almost all template engines do escape strings by default to prevent xssi.

这篇关于node.js:有没有办法将HTML放入i18n-node JSON转换文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆