javascriptsapui5

I18n: getResourceBundle returns promise


Following walkthrough step 22 - Custom Formatters, the call to this.getOwnerComponent().getModel("i18n").getResourceBundle() returns a fulfilled promise instead of the resource bundle object.

Since what version or via what setting is this behaviour changed?

To cope with this, I'm using await in my formatter function:

sap.ui.define([], function () {
  'use strict'

  return {
    formatText: async function () {
      var oRB = await this.getOwnerComponent()
          .getModel('i18n')
          .getResourceBundle();
      return oRB.getText('hi');
    }
  }
});

Solution

  • Whether ResourceModel#getResourceBundle returns a Promise or the actual resource bundle depends on whether the respective ResourceModel is supposed to load the *.properties files asynchronously or not.

    For example, if the ResourceModel setting has async: true, the API getResourceBundle returns a Promise. Take a look at your ResourceModel settings in the i18n related manifest sections. There, you must have "async": true somewhere.

    👉 Simply define i18n property binding with parts.

    <ObjectStatus text="{
      parts: [
        'invoice>Status',
        'i18n>invoiceStatusA',
        'i18n>invoiceStatusB',
        'i18n>invoiceStatusC'
      ], formatter: '.formatter.statusText'
    }"/>
    
    statusText: function (sStatus, sAText, sBText, sCText) {
      // No need to call ResourceBundle#getText
      switch (sStatus) { 
        case "A": return sAText;
        case "B": return sBText;
        case "C": return sCText;
        default: return sStatus;
      }
    },
    

    With the i18n properties directly bound, ResourceBundle#getText is no longer required as the framework takes care of passing the text values from the resolved getResourceBundle promise to the formatter internally.

    See also How to use "formatMessage" module with i18n model and parameters in XML views