Is there a format function available in a Lightning Component controller or helper?

The Dynamically Populating Label Parameters documentation describes how you can:

Output and update labels using the format() expression function


{!format($Label.mySection.myLabel, v.attribute1, v.attribute2)}

But I need to build some error strings in a controller and in a helper not directly in the component markup.

I see that in the Using Custom Labels documentation a label value can be obtained in JavaScript via:


But is there a way to get hold of the format function? (Simply calling it gives me “format is not defined” in both controller and helper.)


Would prefer a platform function or a way to get to the platform function so please answer if you have a way to do that.

But for now I have added this to my helper:

format: function(string) {
    var outerArguments = arguments;
    return string.replace(/\{(\d+)\}/g, function() {
        return outerArguments[parseInt(arguments[1]) + 1];

which is a slight tweak of the accepted answer to MessageFormat in javascript (parameters in localized UI strings) so that the substitution values don’t have to be wrapped in an array.


Better to use the build-in format function – see How to add dynamic variable to custom label in lightning javascript helper class.

Source : Link , Question Author : Keith C , Answer Author : Keith C

Leave a Comment