SAP UI5: Internationalization for each view – Addendum for Nested Views

After writing my previous post on SAP UI5: i18n for each view, I found out that the solution does not work completely as intended when using nested views.

If you’re using nested views, each view would still have its own set of unique texts that are entirely specific to just that view, and for those cases, the solution as described still works. But there might also be a number of texts that are shared by both the outer and one or more of the inner views. It would make sense to be able to define those texts in the i18n model at the level of the outer view, and have the i18n models of the nested view pick up and enhance the i18n model of the outer view.

Problem: onInit() is not the right place to initialize the i18n model

The problem with the original solution is that the onInit() method of the nested views gets called before that of the outer view. It makes sense – the whole can be initialized only after its parts have been initialized. But this does mean that the onInit() method is not the right place to initialize the i18n model.

Please consider these lines from the _initI18n() method that I proposed to initialize the i18n model:

  1.       //Use the bundledata to create or enhance the i18n model</span>
  2.       var i18nModel = this.getModel(i18n);
  3.       if (i18nModel) {
  4.         i18nModel.enhance(bundleData);
  5.       }
  6.       else {
  7.         i18nModel = new ResourceModel(bundleData);
  8.       }
  9.       //set this i18n model.
  10.       this.setModel(i18nModel, i18n);

Suppose this code runs as part of a nested view’s onInit(). The call to getModel() will try to acquire the i18n model that is already set, or else the i18n model of the owner component. That’s how the getModel() method in the base controller works (please see my previous blog post to review that code).

Now, at this point, no i18n model has been set for the view, and so the owner component’s i18n model will be picked up. The i18n model of the outer view will however never be found, since the onInit() of the controller of the outer view has not been called yet (and therefore, its _initI18n() has not been called either).

Solution: Use onBeforeRendering() rather than onInit()

It turns out that this can be solved by calling the _initI18N() method in the onBeforeRendering() method rather than in the onInit() method. While nested views are initialized before initializaing the outer view, it’s the other way around for the rendering process. This makes sense: as the outer view is being rendered, this requires rendering of its containing views. So the onBeforeRendering() method of the outer view will be called before theonBeforeRendering() method of its nested views. (It’s the other way around for onAfterRendering(): outer views will be done rendering after its containing views are rendered).

Ensure i18n initialization occurs only once

There is one extra consideration in moving the i18n initialization from the onInit() to onBeforeRendering(). The reason is that views may go trough multiple rendering cycles, whereas the onInit() will only run once. If there are repeated rendering cycles, we do not want to reinitialize the i18n model, so we add a lock that ensures the i18n model is initialized only once:

  1.     …
  2.     onInit: function(){
  3.       this._initI18n();
  4.     },
  5.   onBeforeRendering: function(){
  6.       this._initI18n();
  7.   },
  8.   _i18nInitialized: false,
  9.     _initI18n: function(){
  10.       if (this._i18nInitialized === true) {
  11.         return;
  12.     }
  13.       var i18n = “i18n”;
  14.       //create bundle descriptor for this controllers i18n resource data
  15.       var metadata = this.getMetadata();
  16.       var nameParts = metadata.getName().split(“.”);
  17.       nameParts.pop();
  18.       nameParts.push(i18n);
  19.       nameParts.push(i18n);
  20.       var bundleData = {bundleName: nameParts.join(“.”)};
  21.       //Use the bundledata to create or enhance the i18n model
  22.       var i18nModel = this.getModel(i18n);
  23.       if (i18nModel) {
  24.         i18nModel.enhance(bundleData);
  25.       }
  26.       else {
  27.         i18nModel = new ResourceModel(bundleData);
  28.       }
  29.       //set this i18n model.
  30.       this.setModel(i18nModel, i18n);
  31.       this._i18nInitialized = true;
  32.     },
  33.     …

Overriding onBeforeRendering() in extensions of the base controller

And of course, when extending the base controller, you’ll need to remember to call the onBeforeRendering() method of the ascendant when overriding the onBeforeRendering() method:

  1. sap.ui.define([
  2.   “just/bi/apps/components/basecontroller/BaseController”], function(Controller){
  3.   “use strict”;
  4.   var controller = Controller.extend(“”, {
  5.     onBeforeRendering: function(){
  6.       Controller.prototype.onBeforeRenderingcall(this);      …
  7.     }
  8.   });
  9.   return controller;
  10. });


I hope you enjoyed this addendum. Feel free to share your insights if you think there is a better way to do handle i18n.

This article belongs to
  • SAP UI5: Internationalization for each view
  • Roland Bouman