3

When creating custom labels, you can do the following in a Lightning component (Assuming salutation = "Hello, {0}"):

{!format($Label.mySection.salutation, v.name)}

What I would like to know, is how to do this in javascript instead? I know the standard process to get the label is:

$A.get("$Label.mySection.salutation");

The part I am having trouble with is how to populate the parameter in the label?

Thanks!

2 Answers 2

6

UPDATE

I now use the following function (taken from my comment on this answer):

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

ORIGINAL ANSWER

The release notes provided for the format function are much better than the actual documentation at this point: https://releasenotes.docs.salesforce.com/en-us/spring16/release-notes/rn_lightning_labels_dynamic_params.htm

You can use $A.util.format in JavaScript to get the same effect as {!format()}.

1
  • Now I use: format: function(string) { var outerArguments = arguments; return string.replace(/\{(\d+)\}/g, function() { return outerArguments[parseInt(arguments[1]) + 1]; }); }, Commented Dec 18, 2017 at 22:05
0

We cannot use $A.util.format as it was never exposed. Apparently they made an mistake exposing the documentation. But it would be a really nice feature to have :( here is the link.

2
  • Currently only option is to use javascript replace() function
    – Ranga
    Commented Dec 14, 2017 at 14:04
  • Correct. I use this function now: format: function(string) { var outerArguments = arguments; return string.replace(/\{(\d+)\}/g, function() { return outerArguments[parseInt(arguments[1]) + 1]; }); }, Commented Dec 18, 2017 at 22:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .