26

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!

3
  • 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. 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. Feb 19 '13 at 15:12
34

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>
5
  • Wow I didn't know about the dynamic access in VF. Very cool. Must try soon! Thanks :) 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. Feb 18 '13 at 23:47
  • This is super helpful !!
    – HSG
    Mar 18 '19 at 19:14
  • 2
    It doesn't work anymore, System throws System.VisualforceException: Visualforce Component types can only be created within a controller class for a Visualforce page.
    – RKM
    Sep 15 '20 at 5:03
20

Following is a code snippet of a work-around. But it has a limitation of working Only in a constructor of a VF page controller class.

string labelName = 'DemoLabel';
Component.Apex.OutputText output;
output = new Component.Apex.OutputText();
output.expressions.value = '{!$Label.' + labelName + '}';
string labelValue = string.valueOf(output.value);

Hope this helps...

3
  • got it working outside of a constructor kevansfdc.blogspot.com/2017/02/…
    – Kevan
    Feb 18 '17 at 20:07
  • 1
    N.B. Referencing a labelName that does not exist results in an uncatchable Internal Server Error.
    – sfdcfox
    Jul 16 '17 at 13:17
  • 1
    It doesn't work anymore, System throws VisualforceException: System.VisualforceException: Visualforce Component types can only be created within a controller class for a Visualforce page.
    – RKM
    Sep 15 '20 at 5:00
0

There's another workaround by KK Ramamoorthy which will also work outside of constructors. Is has it's own limitation, though: it doesn't work in Trigger context because the getContent() is seen as a callout.

What is does is it renders an in-memory visualforce page that takes the label name as a url parameter (and optionally also a language parameter). Then it fetches the label text from the generated page.

https://kksfblog.wordpress.com/2015/03/27/lost-in-translation-an-api-for-dynamically-translating-custom-labels-in-apex/

0

You can use metadata API to get the labels and also translations as well. Hope it will help out someone.

For labels : 
List<MetadataService.CustomLabel> csList = (List<MetadataService.CustomLabel>) service.readMetadata('CustomLabel', new String[] { 'a','b' }).getRecords();

For translations :
MetadataService.Translations translations = (MetadataService.Translations) service.readMetadata('Translations', new String[] { 'es' }).getRecords()[0];

Add this apex-mdapi wrapper class to your org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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