+ Start a Discussion
charbel ALALAMcharbel ALALAM 

What would you like to know?

hi guys, 
i have a question plz:
How can i get a reference to a custom label dynamically using lwc:
i have this code and i want to convert it to lwc:
var currentCat = categoryWrapper.categories[i];  var labelReference = $A.getReference("$Label.c."+currentCat.name);   component.set("v.tempLabel", labelReference);
//we can't use $A.getreference in lwc,
 and using apex i didn't found a solution 
this is the function in apex:
 @AuraEnabled     public static String getLabel(String LabelName){                  system.debug('LabelName'+LabelName);         String s2 = system.Label.LabelName ;  return s2;     }
this is the function in lwc:
                    LabelName:labelReference, //labelreference is the name of custom label
//i want to return the value of a custom label when i send the name of custom label in parameter. Where Is my error, and is there a solution ?
Best Regards
Surya GSurya G
Hi Charbel,
Have you imported the controller class in LWC?
here is sample code example to call your apex class with dynamic custom label name.
import { LightningElement, wire } from 'lwc';
import getLabel from '@salesforce/apex/controllerName.getLabel';

export default class customLabelLWC extends LightningElement {
    get labelReference() {
        // write  your logic to get custom label name dynamically

    @wire(getLabel, { LabelName: '$labelReference' })
    wiredLabel({ error, data }) {
        if (data) {
            this.labelValue = data;
        } else if (error) {

whenever your custom label name changes, lwc will automatically call apex method 'getLabel' and return the respective custom label value.
let me know if it helps

Surya G

charbel ALALAMcharbel ALALAM
hi suya, 
yes i imported the controller, the problem is how to get the custom label dynamically in apex.
this is the function in apex:
    public static String getLabel(String LabelName){
        //Labelname is the name of the custom labem
        String s2 = system.Label.LabelName ;// In s2 i want to return the value of the custom label
        return s2;
the problem in apex is (External string does not exist: LabelName).
If i use the name of the LabelName directly like this:
String s2 = system.Label.B2B_CUL_Textile;//It return the value of custom label which is Textile
//the get function receive the name of custom label and i want it to return the value of this custom label
Surya GSurya G
Hi charbel,

Yes. you are correct. we cannot use apex to dynamically call custom label unless it is called as visualforce controller
And unfortunately we cannot import custom label dynamically in lwc.

But, as a workaround, we can have utility service component in LWC that will import all the custom label in your org, and we can extend that class in your main component that will get the custom label value dynamically.

service component:
import { LightningElement } from 'lwc';
import Test_1 from '@salesforce/label/c.Test_1';
import Test_2 from '@salesforce/label/c.Test_2';
// import all label in this component

export default class CustomLabelService extends LightningElement {

   // add all the custom label name in this object
   customLabels = {
        getTest_1 : Test_1,
        getTest_2 : Test_2

    // method called from your main class(lwc)
    getCustomLabelValue(labelName) {
        return this.customLabels['get'+labelName];
main component:
import { LightningElement, wire } from 'lwc';
import CustomLabelService from 'c/customLabelService';

export default class customLabelLWC extends CustomLabelService {
    labelName = 'Test_2'; // change the custom label name as required

        this.labelValue = this.getCustomLabelValue(this.labelName);
This become more complex if your org has many custom labels a you have to import everything in service component, also we have to add label to service component if any new custom label is created.

Surya G