function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
charbel ALALAMcharbel ALALAM 

how to get a reference to custom label in lwc dynamically

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:
 getLabel({
                    LabelName:labelReference, //labelreference is the name of custom label
                }).then(result=>{
                    
                    console.log('myJSON:'+result);
 
                }).catch(error=>{
                    console.log('error');
                })
//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
SUCHARITA MONDALSUCHARITA MONDAL

Hi Charbel,

Please check with below link for accessing label in LWC

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/create_labels

Once you get the label, you can rendered it based on the requirement.

 

Hope this helps,
Sucharita

Rajendra Singh NagarRajendra Singh Nagar
I also faced similar restrictions and has worked on a service utility. Refer below:
https://rajendrasnagar.wordpress.com/2020/11/20/use-custom-labels-dynamically-in-lwc/
Neel Kamal 6Neel Kamal 6

Please upvote on idea exchang
https://ideas.salesforce.com/s/idea/a0B8W00000NUh9gUAD/get-custom-label-dynamically-in-lightning-web-component