Dojo本地化 - 当本地化字符串包含单引号时,data-dojo-props失败

时间:2015-10-30 11:16:01

标签: javascript localization dojo

我试图本地化一个dojo模板,当本地化字符串包含单引号时,我遇到了问题。

我的模板看起来像这样:

var server = require('karma').server;

//... more code
server.start( { .... } , function(exitCode){
  // ...
});

并且法语语言资源包中的关联行看起来像这样:

<div>
    <select data-dojo-props="label:'${i18n.mySelectorLabel}'"
            >
    </select>
</div>

我希望dojo模板系统可以处理单引号,在法语单词“element&#39;”中出现,而不会在资源包中进行额外的转义。

执行我的代码会导致以下错误:

"mySelectorLabel" : "d'élément :",

显然,单引号没有被适当地转义,但我不确定我的错误在哪里。如果必须在资源包中的字符串中转义单引号,这似乎很奇怪。

2 个答案:

答案 0 :(得分:0)

我只是猜测地把它扔出去。尝试在模板中反转使用单引号和双引号:

<div class="content">
  <h2>This is a long heading</h2>

  <p>This is a long text but it should be the same width as the h2, not wider nor narrower Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur temporibus commodi expedita debitis nisi accusantium, saepe dolorem sapiente eum suscipit quod
    vitae!</p>

  <p>This is a long text but it should be the same width as the h2, not wider nor narrower Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur temporibus commodi expedita debitis nisi accusantium, saepe dolorem sapiente eum suscipit quod
    vitae!</p>
</div>

更新:不幸的是,这一直无法正常运行 - 请参阅OP的评论以获得进一步说明。

答案 1 :(得分:0)

看看dojo解析器是如何工作的,它似乎将data-dojo-props属性的值传递给eval。你最终得到这样的东西:

eval("{" + "label:'${i18n.mySelectorLabel}'" + "}");

这似乎意味着问题中概述的问题是道场的基本限制。

vogomatix提出的解决方法适用于&#39;具体而言,但受使用的缩放器影响。 Uglify很好,但Dojo ShrinkSafe不是。

考虑到这些要点,我们提出的解决方案如下:

  1. 转义资源包中的字符。
  2. 明确设置标签
    <select label="${i18n.mySelectorLabel}">
  3. 使用附加点以编程方式设置标签。
  4. 我们选择了2.不理想但是在Dojo ShrinkSafe和Dojo 1.10与Uglify一起工作时都表现出来。