Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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!

share|improve this question
    
If you're just looking for translating the field labels (as opposed to any other text on your visualforce pages), you'll want to use the translation workbench as fields are by nature multi-lingual already. –  Ralph Feb 18 '13 at 23:48
    
Standard fields are but what about custom fields? –  Phil B Feb 19 '13 at 3:14
    
Yep, all fields, including custom ones, can be translated using the translation workbench. –  Ralph Feb 19 '13 at 15:12
add comment

1 Answer

up vote 8 down vote accepted

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>
share|improve this answer
    
Wow I didn't know about the dynamic access in VF. Very cool. Must try soon! Thanks :) –  techtrekker Feb 18 '13 at 19:12
    
Thanks, it sounds like this is the best option currently available. I did find a more popular version of the idea that is asking a little bit more as well (retrieving translations): success.salesforce.com/ideaView?id=08730000000714W –  Phil B Feb 18 '13 at 19:31
    
Very cool! Did not know that was possible. –  Ralph Feb 18 '13 at 23:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.