4

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?

2
  • 1
    Why do you need dynamic labels? What's the purpose of this?
    – sfdcfox
    Commented Mar 21, 2019 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
    Commented Mar 25, 2019 at 8:56

3 Answers 3

1

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.

3
  • This doesn't work in an LWC, please see my comment below!
    – Tim
    Commented Jun 25, 2020 at 10:57
  • Try making your class an actual controller, ie global ApexDatabase(Object controller) { } global ApexDatabase(ApexPages.StandardController controller) { } Commented Jun 25, 2020 at 15:40
  • Also, it used to work... so don't judge too harsly. I said it was a hack. Commented Jun 25, 2020 at 21:00
1

In an LWC you can wrap an Aura function with eval() and it will work still.

I have been trying to dynamically retrieve a Custom Label for an LWC as it contains a Decryption Key, but it doesn't seem possible.

Also the comments above using Component.Apex.OutputText won't work, I get back this error:

Visualforce Component types can only be created within a controller class for a Visualforce page.

Try wrapping your Aura code in eval like this:

eval('$A.getReference("$Label.c.' + labelName + '")');
1
  • This does seem like a good technique... as long as they continue to allow $A in lwc. Commented Jun 25, 2020 at 15:41
0

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

https://twitter.com/TheVishnuKumar/status/1107558419983032320

You must log in to answer this question.

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