29

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. Commented Feb 18, 2013 at 23:48
  • Standard fields are but what about custom fields?
    – Phil B
    Commented Feb 19, 2013 at 3:14
  • Yep, all fields, including custom ones, can be translated using the translation workbench. Commented Feb 19, 2013 at 15:12

6 Answers 6

35

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 :) Commented Feb 18, 2013 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
    Commented Feb 18, 2013 at 19:31
  • Very cool! Did not know that was possible. Commented Feb 18, 2013 at 23:47
  • This is super helpful !!
    – HSG
    Commented Mar 18, 2019 at 19:14
  • 5
    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
    Commented Sep 15, 2020 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...

4
  • got it working outside of a constructor kevansfdc.blogspot.com/2017/02/…
    – Kevan
    Commented Feb 18, 2017 at 20:07
  • 1
    N.B. Referencing a labelName that does not exist results in an uncatchable Internal Server Error.
    – sfdcfox
    Commented Jul 16, 2017 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
    Commented Sep 15, 2020 at 5:00
  • this is not working inside the class as I need to access the label value inside the same class dynamically. Commented Oct 25, 2021 at 11:26
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

0

In summer 23 release notes, they introduced an easier method to Access Labels in Apex Dynamically:

Use the System.Label.get(namespace, label, language) method to get a custom label, optionally specifying a language.

The feature now allows dynamic resolution of label names at run time, including overriding the user’s current language if a translation exists for the requested language. Previously, retrieving labels required compile-time knowledge of the label API name.

You can also check if translation exists for a label and language in a namespace by using Label.translationExists(namespace, label, language). You can’t access labels that are protected in a different namespace.

Need to wait until the release/feature is generally available.

0

In Summer ’23 Release.
Use the System.Label.get(namespace, label, language) method to get a custom label, optionally specifying a language. The feature now allows dynamic resolution of label names at run time, including overriding the user’s current language if a translation exists for the requested language. Previously, retrieving labels required compile-time knowledge of the label API name.

String strLabel = System.Label.get('namespace','LableName','hi')

You must log in to answer this question.

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