SAP UI5: i18n for each view

Quite recently, I dove into SAP UI5 development. To educate myself, I followed a workshop and I used the Walkthrough. During my explorations, I ran into a particular issue which I didn’t see very readily addressed. I also found a solution for this particular issue, and even though I still have a ton to learn, I think it is worth sharing this with you.

So, here goes:

The Feature: Translatable texts and the i18n model

One of the SAP UI5 features highlighted in the Walkthrough is treatment of translatable texts. In the walkthrough this is realized by setting up a resource model, the i18n model.

The i18n model is sourced form i18n .properties files, which are essentially lists of key/value pairs, one per line, and separated by an equals sign:

  1. greetingAction=Say Hello
  2. greeting=Hello {0}!

To actually setup a i18n model using these texts, you can explicitly instantiate a
sap.ui.model.resource.ResourceModel: i18n for each view

  1. sap.ui.define([
  2.     “sap/ui/core/mvc/Controller”,
  3.     “sap/ui/model/resource/ResourceModel”
  4. ], function(Controller, ResourceModel){
  5.     “use strict”;
  6.     return Controller.extend(“”, {
  7.         onInit: function(){
  8.             var i18nModel = new ResourceModel({
  9.                 bundleName: “”
  10.             });
  11.             this.getView().setModel(i18nModel, “i18n”)
  12.         }
  13.     });
  14. });

Or, you can have your application instantiate the model by listing it in the models property of the sap.ui5 entry in the  manifest.json application descriptor file:

  1. “models”: {
  2.   “i18n”: {
  3.     “type”“sap.ui.model.resource.ResourceModel”,
  4.     “settings”: {
  5.     “bundleName”“”
  6.     }
  7.   }
  8. }

In many cases, the text is required for the static labels of ui elements like input fields, menus and so on. Inside a view, static texts may be retrieved from the i18n model through special data binding syntax, like so:

  1. <Button text=“{i18n>greetingAction”/>

Texts may also be retrieved programmatically inside controller code by calling the .getText() getter on the resource bundle object. The resource bundle which may be obtained from the ResourceModel object using the.getResourceBundle() getter:

  1. var bundle = this.getModel(“i18n”).getResourceBundle();
  2. var text = bundle.getText(“greeting”, [“World”]);

Now, the cool thing is that you can write a separate i18n .properties file for each locale that you want to support. The framework discovers which locale is required by the client and use that to find the best matching i18n .propertiesfiles appropriate for the client’s locale.

The file name is used to identify to which language and/or locale the texts inside the file apply. For example, you’d put the German texts in a file, and the English texts in a file, and if you want to distinguish between British and American English, you’d create both a file.

(I haven’t found out to exactly which standard the sap ui i18n .properties files apply, but from what I’ve seen so far I think it’s safe to assume that you can use the two-letter lowercase ISO 639-1 code for the language and the two-letter uppercase ISO 3166-1 code for the country)

The Problem: One i18n Model for entire application

Now, the walkthrough demonstrates the feature by adding one i18n model for the entire application so that it becomes available in any of the views that make up the application. I appreciate that the walkthrough is not the right place to cover all kinds of more advanced scenarios, so I can understand why it settles for just one application-wide i18n model.

However, I can’t help but feeling this is not an ideal approach. Main reason is that it seems at odds with the fact that many texts are specific to just one particular view. This challenges both the development workflow as well as the reusability of our application components:

  • Each time you create or modify a particular view, you also have to edit the global i18n files. To keep things manageable, you will probably invent some kind of view-specific prefix to prefix the keys pertaining to that view, and you’ll probably end up creating a view-specific block in that i18n file. At some point, you’ll end up with a lot of lines per i18n file, which is not so maintainable
  • Suppose you want to reuse a particular view in another application. Contrary to the practice used in the Walthrough, I like to keep view and associated Controller together, and in a folder separate from any other view and controller. This way I can easily copy, move or remove the things that belong together. Except that the texts, which also belong to that view/controller, are in the global i18n properties file, and need to be managed separately.

The Solution: Keep per-view i18n files near view and controller code

The solution I found is to create a i18n subfolder beneath the folder that contains my Controller and View. Since I already keep each associated view and controller together, and separate from the other views and controllers, this approach makes sense: It’s just one step further in keeping code and resources that depend directly on each other physically together.

So, this is what my file and folder structure looks like:


So, the webapp folder is the root of the sap ui5 project. The components folder is where I keep subfolders for each functional unit (i.e. View+Controller+resources) of the application. In the picture, you see two such subfolders, basecontroller (more about that below) and mainpanel.

The mainpanel folder is the one that contains an actual component of my application – a MainPanel View, and its MainPanel controller. Here we also find the i18n folder specific to this view, and inside are the i18n .properties files (one for each locale).

In order to load and apply the view-specific i18n files, I’m using generic extension of sap.ui.core.mvc.Controller which loads the “local”, view-specific i18n resource bundle. This is what’s in the basecontroller folder, and it looks like this:

  1. sap.ui.define([
  2.   “sap/ui/core/mvc/Controller”,
  3.   “sap/ui/model/resource/ResourceModel”
  4. ], function(Controller, ResourceModel){
  5.   “use strict”;
  6.   var controller = Controller.extend(“”, {
  7.     onInit: function(){
  8.       this._initI18n();
  9.     },
  10.     _initI18n: function(){
  11.       var i18n = “i18n”;
  12.       //create bundle descriptor for this controllers i18n resource data
  13.       var metadata = this.getMetadata(this);
  14.       var nameParts = metadata.getName().split(“.”);
  15.       nameParts.pop();
  16.       nameParts.push(i18n);
  17.       nameParts.push(i18n);
  18.       var bundleData = {bundleName: nameParts.join(“.”)};
  19.       //Use the bundledata to create or enhance the i18n model
  20.       var i18nModel = this.getModel(i18n);
  21.       if (i18nModel) {
  22.         i18nModel.enhance(bundleData);
  23.       }
  24.       else {
  25.         i18nModel = new ResourceModel(bundleData);
  26.       }
  27.       //set this i18n model.
  28.       this.setModel(i18nModel, i18n);
  29.     },
  30.     getModel: function(modelname){
  31.       var view = this.getView();
  32.       var model = view.getModel.apply(view, arguments);
  33.       if (!model) {
  34.         var ownerComponent = this.getOwnerComponent();
  35.         if (ownerComponent) {
  36.           model = ownerComponent.getModel(modelname);
  37.         }
  38.       }
  39.       return model;
  40.     },
  41.     setModel: function(model, modelName){
  42.       var view = this.getView();
  43.       view.setModel.apply(view, arguments);
  44.     }
  45.   });
  46.   return controller;
  47. });

Note how the BaseController initializes the i18 model by calling the _init118n() method. In this method we extract the className of the this object from its metadata (using the .getName() getter on the metadata obtained using the.getMetadata() getter), and we pop off the unqualified classname to obtain its namespace. We then add the string"i18n" twice – once for the folder, and once for the files inside it. We use this to create the bundlename which we use to instantiate the actual ResourceModel.

Before setting that model to the controller’s view, we check if there is already an i18n model set using getModel(). This is a utility method that gets the model from this controller’s associated view, or of the component that “owns” the view and this controller.

If a i18n model is already available, we enhance that by calling the .enhance() method on it, rather than replacing it. This way, any texts defined at a higher level are still available in this controller and view. This gives us a functional i18n model, which we then set using setModel(), which simply calls setModel() on the view associated with this controller.

To actually use the BaseController, we extend it when creating a “real” controller:

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

Note that if that real controller has its own onInit() method, we need to first call the onInit() method of the base controller, or rather, of whatever class we’re extending. Fortunately, since we are extending it we already have a reference to it (in the snippet above, it’s the Controller parameter injected into our definition), so we call its onInit()method via the prototype while using the controller that is currently being defined (this) as scope.

Addendum for Nested Views

It turns out that this solution does not work completely for nested views. Fortunately, only a small improvement is required to make it work under these circumstances. The details of that particular issue are described in a separate post: SAP UI5: Internationalization for each view – Addendum for Nested Views


I hope you enjoyed this article! I hope it will be of use to you. If you have an alternative solution – quite possiby, a better one – then please feel free to leave a comment and point out your solution. I’m eager to hear and learn so don’t hesitate to share your opinion and point of view.

This article belongs to
  • i18n
  • SAP UI5
  • SAP UI5: i18n internationalization
  • Translatable texts and the i18n model
  • Roland Bouman