4

I know that in vf i can dynamically obtain the value of a label like this :

<apex:outputText> {!$Label[mylabel]} </apex:outputText>

But this does not work in lightning :

    $A.get("$Label.c['customerInformation']")

nor this :

 $A.get("$Label.c[my_variable]"));

Has anyone been able to do it?

4 Answers 4

4

What i wanted to do, in fact exist on lightning, here is the code :

 var labelSubStr = "task_mode_today";
 var labelReference = $A.getReference("$Label.c." + labelSubStr);
 cmp.set("v.tempLabelAttr", labelReference);
 var dynamicLabel = cmp.get("v.tempLabelAttr");

Here is the link to the doc : https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/labels_dynamic.htm

1

As i'm using a visualforce page to contain my lightning component. I evaluated my labels dynamically in my vf controller using the following method like this :

 public string getLabel(object o){      
    Component.Apex.OutputText output = new Component.Apex.OutputText();
    output.expressions.value = '{!$Label.' + string.valueOf(o) + '}';
    return String.valueOf(output.value);
}

This method use the output.expressions.value variable which can only be used in a visualforce class controller.

I processed all my required labels via this method and return a map of those label as an object to my lightning component

1
  • For anyone looking at this in 2023 - this does not work any longer. Creating dynamic Apex components is restricted to actual Visualforce controllers now.
    – Jo_gurt
    Commented Mar 17, 2023 at 13:11
0

Doing this: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/labels_value_provider_platform.htm works for me.

Just try this: console.log($A.get("$Label.namespace.labelName"));

2
  • console.log($A.get("$Label.namespace.labelName")); doesn't work for me. Forget about the dynamic, the static one isn't working either :(
    – vanessen
    Commented Aug 2, 2016 at 14:51
  • Even if you call an apex controller method that returns that label content doesn´t work for you?
    – D. Cruz
    Commented Aug 2, 2016 at 14:54
-1

I was able to find this on the Developer Pages.

I think what you are looking for is the following:

Labels In Lightning

You must log in to answer this question.

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