6

In the UI5 Demo Kit, Walkthrough step 8 (still) mentions the following:

To be on the safe side, we would have to use a similar mechanism as in the controller to use a string from the resource bundle and replace parts of it. This can be done with the jQuery.sap.formatMessage formatter.

It can be used for translatable texts with placeholders in XML views e.g.:

<Title text="{
  parts: [
    'i18n>overflowToolbarTitle',
    'appView>/listItemCount'
  ],
  formatter: 'jQuery.sap.formatMessage'
}" />

However, jQuery.sap.formatMessage is depreciated since 1.58, and if I use instead the suggested alternative sap.base.strings.formatMessage, the following error is thrown:

formatter function sap.base.strings.formatMessage not found!

How can the new formatMessage module be used in XML?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Epicurist
  • 903
  • 11
  • 17

1 Answers1

11

UI5 1.69+

With commit:2dab48e, it is now possible to require modules declaratively in XML views:

<Title xmlns="sap.m" xmlns:core="sap.ui.core"
  core:require="{ formatMessage: 'sap/base/strings/formatMessage' }"
  text="{
    parts: [
      'i18n>overflowToolbarTitle',
      'appView>/listItemCount'
    ],
    formatter: 'formatMessage'
  }"
/>

With this, we can avoid having dependency to an intermediate Controller and make use of the new module independently.


UI5 1.68 and below (previous answer)

UI5 currently doesn't allow fetching and assigning a module to the formatter.1 Furthermore, the new function module is never exported with the name sap.base.strings.formatMessage.src

One of the alternative approaches would be to point to the method assigned in the controller whereas that method points to the required formatMessage module.

<Title text="{
  parts: [
    'i18n>overflowToolbarTitle',
    'appView>/listItemCount'
  ],
  formatter: '.formatMessage'
}"/>
sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "sap/base/strings/formatMessage",
  // ...
], function(Controller, formatMessage/*, ...*/) {
  "use strict";

  return Controller.extend("...", {
    formatMessage: formatMessage,
    // ...
  });
})

I could imagine that UI5 supports fetching and assigning modules within binding definition in later versions, but at the time of writing it's not possible.



1 Related enhancement request: https://github.com/SAP/openui5/issues/2475

Community
  • 1
  • 1
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
  • One can enhance this a bit further, by using `parts: [{path: '...'}, {value: 'someStaticValue'}]` in case a static parameter has to be passed to the i18n model. – fmi21 Jul 30 '22 at 11:56
  • @fmi21 Yes, static binding is possible since UI5 1.61: https://stackoverflow.com/a/53609552/5846045 – Boghyon Hoffmann Jul 30 '22 at 13:02
  • I've also noticed you're using some form of 'shorthand' syntax in the 'parts' part, where you can omit the token 'path' and directly specify it as a string and not an object array instead. Do you have any references for this as well? – fmi21 Jul 30 '22 at 18:06
  • 1
    @fmi21 Using strings only in `parts` is allowed according to https://sdk.openui5.org/topic/a2fe8e763014477e87990ff50657a0d0 – Boghyon Hoffmann Jul 31 '22 at 22:27
  • 1
    @fmi21 The type information of `parts` is now fixed in the API reference accroding to the commit from today: https://github.com/SAP/openui5/commit/2eeaf175f4f4bc94b0dc743dc525e7d840194b08 – Boghyon Hoffmann Aug 04 '22 at 09:35
  • Hi! Am I missing some important part here? I have defined `core:require` in the `View` definition, and then in Text control I have the same - one part with i18n text with placeholder and second part with that value for placeholder. Yet when I run the app - it is just empty, also no errors. I'm on 1.99. The expected outcome is "User: {0}" – Shanir Jan 04 '23 at 08:46
  • Hi @Shanir , could you share an [MCVE](https://en.wikipedia.org/wiki/Minimal_reproducible_example)? Preferably via Plunker or in a separate question.. – Boghyon Hoffmann Jan 04 '23 at 08:59
  • Why can we use `formatter: 'sap.ui.require.toUrl'`, but not `formatter: 'sap.base.strings.formatMessage'`? :-) – Pieter Jun 21 '23 at 12:21
  • 1
    @Pieter The "Previous Answer" explains the reason "[...] the new function module is never exported with the name `sap.base.strings.formatMessage`". See also https://github.com/SAP/openui5/issues/2475 – Boghyon Hoffmann Jun 21 '23 at 13:10