3

Dojo 自定义小部件可以按照此处此处_templated列出的步骤通过 mixin进行国际化。然后像这样的小部件模板中的占位符: 自动替换为适当的语言翻译。${i18n.username}

在小部件之外进行类似 nls 语言替换的最简单方法是什么?理想情况下,我想向标签添加一个属性,以替换其中的所有占位符,包括嵌套标签。是否有某种类型的容器小部件已经这样做了?

还是 Dojo 开发假设一切都在(自定义)小部件中?我需要本地化已经存在的不使用小部件的代码。

到目前为止,我发现的最佳解决方案是:

4

1 回答 1

2

我假设外部 html 中的符号是${i18n.username}. 这将找到任何具有class="i18nReplace"并替换节点的 innerHTML 的节点。我没有测试任何这些,但我希望你可以以此为起点。

dojo.require("dojo.i18n");
dojo.require("dojo.query");
dojo.requireLocalization("myI18n", "myI18N"); // This will need to be modified to get your i18n files

dojo.addOnLoad(function() {
  var i18n = dojo.i18n.getLocalization("myI18n", "myI18N");
  dojo.query(".i18nReplace").forEach(function(node, index, arr){

      node.innerHTML = dojo.replace(node.innerHTML, { i18n: i18n } );

      // blindly doing this, does not support nested tags.
      // you could add conditional logic to check for children 
      // and if they exist separately process them, otherwise 
      // replace the html.
  });
});
于 2012-04-10T03:14:53.897 回答