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."

  • 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 – Vamsi Krishna Mar 31 '17 at 5:59
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)}
  • 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! – techbusinessman Sep 7 '17 at 22:43
  • NM, you answered this question in another thread: salesforce.stackexchange.com/a/170634/8359 – techbusinessman Sep 7 '17 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 '17 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.