You need to sign in to do that
Don't have an account?
charbel 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:
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
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
Have you imported the controller class in LWC?
here is sample code example to call your apex class with dynamic custom label name.
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
Thanks
Surya G
yes i imported the controller, the problem is how to get the custom label dynamically in apex.
this is the function in apex:
@AuraEnabled
public static String getLabel(String LabelName){
//Labelname is the name of the custom labem
system.debug('LabelName'+LabelName);
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
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: main component: 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.
Thanks
Surya G