In Aura we used to do this:

let labelName = 'mylabel';
$A.getReference("$Label.c."+ labelName);

In LWC $A is not accessible anymore.

Following the documentation I can only see a way to get access to Label's value through import. As far as I know import don't work dynamically. By this I mean that you need to know which label you want the value of in advance. In our case the label's name would be stored in a Custom Metadata

I was hoping for a solution involving Apex and/or SOQL but could not find anything.

Any idea?

  • 1
    Why do you need dynamic labels? What's the purpose of this? – sfdcfox Mar 21 '19 at 19:09
  • I am developing a dynamic form engine. It reads configuration from metadata and renders a multistep wizard. The point is avoid code deployment when the business wants only to change a few labels or fields. That's the reason why all config is read from metadata. Everything needs to be translatable. – LaurentD Mar 25 '19 at 8:56

You can dynamically get a label via Apex using this slightly hackish technique:

Component.Apex.OutputText output = new Component.Apex.OutputText();
output.expressions.value = '{!$Label.' + labelStringPassedIn + '}';
String labelValue = String.valueOf(output.value);

Unfortunately you'll need to call this for each label you want to retrieve.

This works in a lightning context.

|improve this answer|||||

You can not import label dynamically. Here are the conversions from twitter where we have talked about the same thing.

It does include a message from Principal Architect at @Salesforce. This is not possible now but it will be there in the future.

And Dynamic import hasn’t made it to a JavaScript standard yet. It is a level 4 proposal https://github.com/tc39/proposal-dynamic-import/#import


|improve this answer|||||

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.