cancel
Showing results for 
Search instead for 
Did you mean: 
Subscribe
Hello everyone,
For specific architecture and UX requirements in a KPI application I am building, I need to handle certain text strings inside the Controller instead of binding them directly in the XML View.
However, I am facing a timing issue where the i18n model is not yet loaded or available during the early controller lifecycle hooks.
 
What I have tried so far:
1. Accessing it inside onInit
 
onInit: function () {
    // This fails because the model is not ready yet
    this._oBundle = this.getView().getModel("i18n").getResourceBundle();
}

Console Error:  Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getText')

2. Accessing it inside onBeforeRendering:

onBeforeRendering: function () {
    this._oBundle = this.getView().getModel("i18n").getResourceBundle();
},
Console Error: Dashboard.controller.js:20 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getResourceBundle')
 
How I intend to use it:
Once retrieved, I need to dynamically update UI controls with parameters like this:
var sTitle = this._oBundle.getText("shellBarTitle", [sTime, sDate]);
this.byId("_IDGenShellBar").setTitle(sTitle);

It did not work. The title is blank

I am sure this is a common scenario, but many older SAP Community solutions I found rely on deprecated synchronous methods

or require a lot of workaround logic.
What is the current best practice or modern SAPUI5 reusable approach to safely access the i18n resource bundle as soon as it becomes available in the lifecycle?
Thanks a million.
0 Likes
View Entire Topic
sravan_aleshwaram
Contributor
0 Likes

Hello @deiamolina 


The i18n model is likely not available when onInit() or onBeforeRendering() runs.

Use the model from the Component and wait for the ResourceBundle promise:

onInit: async function () {
const oBundle = await this.getOwnerComponent()
.getModel("i18n")
.getResourceBundle();

const sTitle = oBundle.getText("shellBarTitle", [sTime, sDate]);

this.byId("_IDGenShellBar").setTitle(sTitle);
}


Or:

this.getOwnerComponent()
.getModel("i18n")
.getResourceBundle()
.then((oBundle) => {
this.byId("_IDGenShellBar")
.setTitle(oBundle.getText("shellBarTitle", [sTime, sDate]));
});

 

Access the i18n model through getOwnerComponent().getModel("i18n") instead of this.getView().getModel("i18n"), and handle getResourceBundle() asynchronously in newer SAPUI5 versions.


Thanks,
Sravan