23

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!

  • 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 Callaway 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 Callaway Feb 19 '13 at 15:12
30

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>
  • 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 Callaway Feb 18 '13 at 23:47
  • This is super helpful !! – CodeNinja Mar 18 at 19:14
19

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...

  • 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
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/

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.