2

I have a SAPUI5 app that tries to load some translation files based on user language but those languages are missing in the original library of SAP.

For example it tries to load a translation with fa locale as following:

https://webidetestingXXXXXXX.dispatcher.hana.ondemand.com/webapp/resources/sap/m/messagebundle_fa.properties

enter image description here

As it can be seen it tries to read the translation from sap.m namespace!

Now the question is, as I have access to English translation file of this recourse, how can I activate a call back mechanism that when a translation file is missing, it takes a look on my i18n folder and then if it couldn't find the file there, then load the default translation!?

For example I can download the English file and provide a translation for Persian language under webapp\i18n\sap\m\messagebundle_fa.properties and when it's failed to find the file in original place then read it from my local folder!

Please note the actual address of my webapp\i18n folder inside the WebIDE is something similar to https://webidetestingXXXX.dispatcher.hana.ondemand.com/~1595255696000~/webapp/i18n/. That ~1595255696000~ refers to the current instance of run app. And as you see it is missing for the files that failed to load!

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
MJBZA
  • 4,796
  • 8
  • 48
  • 97
  • Could be helpful [Ref1](https://github.com/SAP/openui5/issues/733), [Ref2](https://github.com/SAP/openui5/issues/2911) – MJBZA Jul 21 '20 at 09:29
  • Interesting to see how UI5 control libs completely left out Persian language but fully supports Persian calendar together with Persian CLDR file. If there is enough demand, I can imagine control devs can include `messagebundle_fa.properties` to their libs (unless there are legal restrictions). Would be nice if you could create an enhancement request on GitHub so that people can give their to that. – Boghyon Hoffmann May 09 '21 at 09:28
  • As of 1.78 ([commit `7a231af`](https://github.com/SAP/openui5/commit/7a231aff034fd1904481aec6cd01b00de82ec69c)) and as mentioned by codeworrior below, control libs now use fallback locales internally to avoid 404 requests. In the case of requesting `messagebundle_fa.properties`, the `messagebundle_en.properties` is used instead. – Boghyon Hoffmann May 09 '21 at 09:31
  • Yes, the language is overlooked due to the sanctions against Iran. But as I know, Persian calendar use sometimes even in some iternal DB algorithms due to it's accurecy. Therefore, maybe they need it for some internal use cases. I don't need the translations to be honest. The whole project was for learning. – MJBZA May 09 '21 at 11:17

1 Answers1

3

There's no specific redirect or callback mechanism for resource bundles in UI5.

The only workarounds that I can imagine, are either to use the paths mapping feature of the UI5 module loader to redirect requests for all language files of a library to your own, enriched copy (A) or to preload them and register them under the expected names in the loader (B).

Both variants have to be applied early, before UI5 tries to access any text from the libraries that you want to enrich.

Note: I did not fully test these workarounds (they might contain typos or the mappings / resources might have to be adapted), and, and that's the bad part, they only work for UI5 versions < 1.78. Starting with 1.78, UI5 knows what *.properties files exist per library and does not request any other file.

Variant A (mapping)

sap.ui.loader.config({
  paths: {
    "sap/m/messagebundle.properties": "my/enriched/copy/messagebundle.properties"
  }
});
  • Pro: uses a public API.
  • Cons: cumbersome as all languages have to be copied (and updated) and only manageable when not too many libraries are required.

Variant B (preloading)

// somehow load the 'fa' texts for sap.m (ideally async)
var sapmtext = ...;

// then register it in the preload cache of the loader before 
sap.ui.require.preload({
  "sap/m/messagebundle_fa.properties": sapmtext
});
  • Cons: sap.ui.require.preload is not a public API.
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
codeworrior
  • 311
  • 1
  • 4
  • It seems it is the answer, let me test it. Just an small question, can I put the map in manifest.json? – MJBZA Jul 21 '20 at 11:34
  • For the second approach: Where do I have to put it? I left it in the `onInit` function of component and it failed! While it loads the file after ui5 core tries to load the files! – MJBZA Jul 21 '20 at 12:09
  • Sorry non of them work! I even tested the first approach based on [this](https://sapui5.hana.ondemand.com/#/topic/1dfab2e19fc0479d9dfcefc28d3642f1.html), but it does not redirect the requests. – MJBZA Jul 21 '20 at 12:21
  • Seems that I remembered the workaround wrong. ResourceBundle always loads all languages from the same location, mapping individual lunges to a different location does not work. So you would have to redirect all requests to your location and copy all resource bundles there. That was the cumbersome part. For the second variant, you have to do it early enough, before the library is loaded. Otherwise, the framework might already have tried to load the resource bundle. – codeworrior Jul 21 '20 at 12:45
  • I found the problem. It seems it is a bug in UI5! `_fa` must be removed then it will redirect the requests! – MJBZA Jul 21 '20 at 12:51
  • I can check if the language is set to fa then set the map! – MJBZA Jul 21 '20 at 12:52
  • That's not a bug, see my previous comment. ResourceBundle determines the base URL only once and derives all other language file locations for that URL. Therefore redirecting a single language does not work. That was the cumbersome part that I did not fully remember. I've adapted my reply accordingly. Anyhow, all the limitations still apply (must be done early enough, not working with UI5 >= 1.78) – codeworrior Jul 21 '20 at 12:55
  • Conditionally registering the paths is fine, but then you have to take special care to support dynamic language switching (sap.ui.getCore().getConfiguration().setLanguage()) – codeworrior Jul 21 '20 at 12:56
  • Regarding the sentence `Starting with 1.78, UI5 knows what *.properties files exist per library and does not request any other file.` it means UI5 will be limited to its own 38 translated languages in the future? – MJBZA Jul 21 '20 at 13:00
  • The first solution works with sapui5 1.80 even. Just we have to redirect conditionally to different folders based on user language. And we must not use any `_fa` suffix. – MJBZA Jul 22 '20 at 07:28