2

I am using a custom label in lightning controller/helper as:

$A.get("$Label.c.MaxCountProducts");

And the label has value as:

Maximum Product(s) count {0} is exceeded.

I want to dynamically populate the value of {0} here. How can it be achieved to display, say if max product count is 5,

"Maximum Product(s) count 5 is exceeded."

1
  • once you get the label value into a var, you can use a simple string replace function to put the actual number instead of the placeholder or you can use regex to replace multiple occurrences dynamically Mar 31, 2017 at 5:59

2 Answers 2

5

The framework provides a format function for this purpose e.g. with the label:

Hello {0} and {1}

the code could be:

{!format($Label.mySection.myLabel, v.attribute1, v.attribute2)}
3
  • How would one address this in Javascript? With $A.util.format being pulled I'm not sure how to do the same function without displaying the label in the component (think using custom labels for error messaging, that starts in the helper not in the component). Thanks! Sep 7, 2017 at 22:43
  • NM, you answered this question in another thread: salesforce.stackexchange.com/a/170634/8359 Sep 7, 2017 at 22:46
  • @techbusinessman I've linked the other answer to this... thanks. Don't really follow your first comment - perhaps ask as a question with a bit more detail?
    – Keith C
    Sep 7, 2017 at 22:54
0

You can bind this one attribute inside your controller init method or any other controller method as per your usecase and then show that variable.

controller.js

component.set("v.productCount",$A.get("$Label.c.MaxCountProducts"));

component

Maximum Product(s) count {!v.productCount} is exceeded.

You must log in to answer this question.

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