Dojo internationalization through markup outside the widget

Dojo custom widgets can be internationalized with mixing _templated

by following the steps below here and here . Then the placeholders in the widget template: are ${i18n.username}

automatically replaced with the appropriate language translation.

What's the simplest way to do a similar nls language replacement outside of the widget? Ideally, I would like to add an attribute to the tag so that all the overriding elements, including nested tags, can be found. Is there some kind of container widget that already does this?

Or does Dojo development assume everything will be in a (custom) widget? I need to localize pre-existing code that doesn't use widgets.

The best solutions I've found so far:

+3


source to share


1 answer


I assumed the notation in the outer html is ${i18n.username}

. This will find any node that has class="i18nReplace"

and replaces the innerHTML node. I haven't tested this, but hopefully you can use it as a starting point.



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.
  });
});

      

+2


source







All Articles