[SalesForce] LWC: how to dynamically get translated value from Label

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?

Best Answer

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.