Mike
Mike

Reputation: 14616

Can't change an app localization during the lifetime

As far as I know, UI5 determines the wanted localization based on browser's HTTP header accept-language with respect of supportedLocales and fallbackLocale values of the manifest.json app descriptor.

In my case, however, initially user sees an authorization dialog in English and after authorization I would like set an app localization based on a user config, loaded upon successful authorization.

Based on Language switch in SAPUI5, I've tried to apply:

sap.ui.getCore().getConfiguration().setLanguage("fr");

in the view onInit() but instead I get a blinked view in the desired localization and then an empty window.

Since sap.ui.getCore().setLanguage() does not guarantee that already created, language dependent objects will be updated by this call, I've tried location.reload(); to trigger a complete page reloading and rebuilding all UI5 elements but it doesn't help.

Upvotes: 1

Views: 789

Answers (2)

fmi21
fmi21

Reputation: 545

My go-to choice is

sap.ui.getCore().getConfiguration().applySettings({
    language: 'de',
});

This switches the used i18n bundle.

However, there are still some occurrences that don't get translated e.g. the work-day labels in a sap.m.PlanningCalendar, which remain in the browser's language.

Upvotes: 1

Benedikt Kromer
Benedikt Kromer

Reputation: 731

I'm not sure if this can be done without a reload.

For modern browsers, you can use this code to change the URL:

const oURL = new URL(window.location.href);
window.location.href = oURL.searchParams.set("sap-ui-language", "en").toString();

If, for some reasons, you have the language already in the URL, you need to add a cachebreaker.

Upvotes: 2

Related Questions