5

I've got a language problem with my SAPUI5 controls.

If I execute e.g.:

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

My i18n files are loaded correctly and all labels are translated to German. But the controls are still in English.

The only way to get German controls is with the URL parameter:

sap-ui-language=DE

But I can't use a parameter in my case. Any idea?

Mike
  • 14,010
  • 29
  • 101
  • 161
alexP
  • 3,672
  • 7
  • 27
  • 36
  • What does the controls are still in English mean? – matbtt Dec 09 '16 at 07:55
  • In my case a FacetFilter. The placeholder is in English instead of German. But all labels are in German. All elements translated in the i18n are in German. – alexP Dec 09 '16 at 08:05
  • What backend are you using? Are all texts/labels coming from i18n files? What i18n language files do you have in your app? Are you using OData in your app and probably texts from $metadata? Please give us more information. I deleted my answer because I made too many assumptions... – Nabi Dec 11 '16 at 09:16
  • The data comes from a odata service. The labels from the i18n file. I think the "No filter selected" in the facetfilter comes from sapui5 control. – alexP Dec 11 '16 at 09:22
  • @alexP: Where in your code are you setting the language with `sap.ui.getCore().getConfiguration().setLanguage( "de" );` ? – Cassio Dec 13 '16 at 10:00
  • @alexP can you try doing that in Component.js? – Cassio Dec 13 '16 at 10:15
  • I've already tried that. Nothing happens. if i run `sap.ui.getCore().getConfiguration().setLanguage( "de" );` in Component.js all labels are German. The FacetFilter is still in English. It almost seems so that the only way to switch the facetfilter language is the URL parameter... – alexP Dec 13 '16 at 11:19
  • It's a bug in the control implementation! I will open an issue on github. For details see my updated answer below. – Nabi Dec 14 '16 at 08:14

2 Answers2

5

Please note that sap.ui.getCore().setLanguage() explicitly states

The framework does not guarantee that already created, language dependent objects will be updated by this call. It therefore remains best practice for applications to switch the language early, e.g. before any language dependent objects are created. Applications that need to support more dynamic changes of the language should listen to the localizationChanged event and adapt all language dependent objects that they use (e.g. by rebuilding their UI).

Besides that, I fully support Nabi's answer (but I'm not allowed to vote it up).

I just would like to add that controls (like FilterBar) better should use the hook approach:

FilterBar.prototype.onlocalizationChanged = function(oEvent) {
    // .. same bundle update code as in Nabi's proposal
}

Using the hook in controls avoids the need for adding attach + detach calls in init / exit and keeps the event registry small.

Mike
  • 14,010
  • 29
  • 101
  • 161
codeworrior
  • 311
  • 1
  • 4
3

I can easily confirm the behavior you described by testing the Explored App Example. There, just open the console and hit sap.ui.getCore().getConfiguration().setLanguage("de");

I also checked the implementation of the FacetFilter and I would call this a bug in the Control implementation. It comes from how the texts are loaded inside the control. Just in case you are interested:

  1. The message bundles all contain the correct translations for FACETFILTER_INFOBAR_NO_FILTERS (for en the translation comes from the "default" bundle):

  2. The FacetFilter has a hidden aggregation called SummaryBar. The SummaryBar contains the text you see. Of course, this text comes from a bundle.

  3. However, the bundle is initialized exactly once in init() by calling sap.ui.getCore().getLibraryResourceBundle("sap.m");. Here the API docs say:

    If only one argument is given, it is assumed to be the libraryName. The locale then falls back to the current session locale.

  4. This means the bundle is cached and therefor changes to the localization (e.g. language) do not trigger the bundle to load a new translation file. Thus, we will always see the initial language no matter what we try (even rerendering() does not help).

A solution would be to fix the control by adding the following code right after the the bundle gets loaded inside the init:

sap.ui.getCore().attachLocalizationChanged(function(oEvent){
    var oChanges = oEvent.getParameter("changes");    
    if (oChanges && oChanges.language){
        this._bundle = sap.ui.getCore().getLibraryResourceBundle("sap.m", oChanges.language);
        this.rerender();
    }
}.bind(this));

You can try this out in the explored app linked above, it worked for me just fine...

I just opened an issue on github.

Nabi
  • 2,536
  • 1
  • 16
  • 18
  • Hi, can you share more details about "this.rerender();" what this function will have in it, and where did you wrote this attachLocalizationChanged function. Like in Component or Controller – Kunal Panchal Jun 11 '18 at 06:09