cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to handle multiple language in SAP UI5 application..?

former_member626660
Participant
3,524

Hi SAP Community,

I’m facing issues with handling multiple languages inside my application. My requirement is to set the particular selected language mentioned in the selection field to the application. The custom texts are set correctly from the corresponding i18n bundles which are changed using the localizationChanged Event.

sap.ui.getCore().attachLocalizationChanged(function (oEvt) {
   var oChanges = oEvt.getParameter("changes");
   if (oChanges && oChanges.language) {
     this._bundle = sap.ui.getCore().getLibraryResourceBundle("sap.m", oChanges.language);
     sap.ushell.Container.getService("UserInfo").getUser().setLanguage(oChanges.language.toLocaleUpperCase()); 
     this._setDefaultModel(oChanges.language); // this is the function where I set the language parameters		       }
}.bind(this));

However, the application’s certain standard texts and backend driven texts ( which are highlighted in the below picture) remains unchanged. I would greatly appreciate your efforts and ideas that could help me solve this issue.

Below are some of my tried and tested methods with little to no luck

Method 1: I tried to directly access the private members of oDataModel and tried to set url parameters as below

var oModel = this.getOwnerComponent().getModel();
oModel.aUrlParams[0] = "sap-language=" + language.toLocaleUpperCase();
oModel.mMetadataUrlParams = {
	"sap-language": language.toLocaleUpperCase(),
	"sap-value-list": "none"
			};
this.getOwnerComponent().setModel(oModel);

This produced a 50-50 result-like, like in the request URL the specified language was appended but in the request payload it still remained as Accept-language:"en" and it did not change the metadata url parameters, so no effect was seen altogether


Method 2: As akshaya1591 suggested in her blog https://blogs.sap.com/2019/09/20/smart-templates-tips-and-tricks/?update=updated, I created a new model with my parameters and tried to load this new model as my default model like below,

var oDefaultModel = this.getView().getModel();
var custSrvUrl = oDefaultModel.sServiceUrl;
var oModel = new sap.ui.model.odata.v2.ODataModel({
	serviceUrl: custSrvUrl,
	annotationURI: [
"/sap/opu/odata/IWFND/CATALOGSERVICE;v=2/Annotations(TechnicalName='ZService_ANNO_MDL',Version='0001')/$value/",
"../annotations/annotations.xml"
			],
	serviceUrlParams: {
			"sap-language": language.toLocaleUpperCase()
			},
	metadataUrlParams: {
			"sap-language": language.toLocaleUpperCase()
			}
		});

oModel.fireAnnotationsLoaded(oModel.getServiceAnnotations());
var oMetaModel = oModel.getMetaModel();
oMetaModel.fireRequestCompleted();
this.getView().setModel(oModel);

The url params to my batch call and the metadata call were set successfully using this method and in the "Networks" tab of the debugger I could see the corresponding language files loaded, however, it had two drawbacks

  1. Even though the metadata and the service are appended with “sap-language=<my_desired_languageCode>” it did not change the standard SAP texts in the particular language and it also results in an Assertion error at times
  2. When another language is tried for the second time it throws an error saying “Assertion failed : Source must contain an annotation object to be merged” and “Cannot read property ‘getObject’ of null
  3. I also tried destroying or refreshing the existing model before this new model is set to the view but all resulted in the same error

Method 3: I tried to manipulate the manifest file and add runtime parameters from the controller, but it threw an error saying “Cannot read property 'settings' of undefined" as there were only i18n model inside oManifestUi5 object and the default oDatamodel wasn’t there.

var oManifestUi5 = this.getOwnerComponent().getMetadata().getManifestEntry("sap.ui5");
oManifestUi5.models[""].settings.serviceUrlParams["sap-language"] = language.toLocaleUpperCase();
// oManifestUi5.models[].settings.serviceUrlParams["sap-language"] = language.toLocaleUpperCase();
oManifestUi5.models[""].settings.metadataUrlParams["sap-language"] = language.toLocaleUpperCase();

Please correct me if I'm doing it the wrong way or suggest any new ways that could possibly help me in achieving my desired result.

Thanks in advance!!

Regards

Imtiaz N

View Entire Topic
saurabh_v
Active Participant
0 Kudos

Can you check if the language EN is set in the ABAP RFC connection (used by the system alias assigned to your OData service) which connects the front-end system to your back-end system?

former_member626660
Participant
0 Kudos

Hi Saurabh,

It seems like no language has been set in the RFC Connection,

Regards

Imtiaz N