[SalesForce] How to Dynamically Populating Label Parameters in LWC

There was a feature for an Aura framework which supported Dynamically Populating Label Parameters in Aura component syntax providing functionality to format static label so that it is possible to specify "hello {0}" in the label text and display in UI "hello username" where username is current user name or any other populated value.

It is also confirmed here and here that this was supported for the Aura framework. I expect this to be natively supported by LWC as well.

However, looks like there is no corresponding feature in Lightning Web Component syntax or I can't find any information about it.

Can anyone help?

Best Answer

I think I will use the following workaround.

I would create a service LWC called formatUtils.

const format = (format) => { 
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) { 
  return typeof args[number] != 'undefined'
    ? args[number] 
    : match
  ;
});
};

and then use it

format( label, ['param1', 'param2'])