[SalesForce] possible to access custom labels dynamically

In apex you can reference a custom label with system.Label.labelName, but is there a more dynamic way to get a custom label for example system.Label.get(labelName)? I can't find anything like this in the documentation but was hoping it was possible.

I'm trying to have custom labels for fields to support multiple languages. The list of fields I'm showing is built in the controller and displayed inside an apex:repeat tag so I can't hardcode the labels. Is there a way to accomplish this?

Thanks!

Best Answer

EDIT This is now possible! You can instantiate a dynamic visualforce component in Apex to get this.

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

ORIGINAL ANSWER

No, you can't reference labels dynamically in apex. You can vote for this idea here. However, you can reference them dynamically from visualforce so you can put them on the page like so:

public class MyController
{
    public String mylabel{get; set;}

    public MyController()
    {
         mylabel='labelname';   
    }
}

<apex:page Controller="MyController">
    <apex:outputText> {!$Label[mylabel]} </apex:outputText>
</apex:page>