Dynamic Custom Labels

I know what you’re thinking, ‘this is cRAzY talk’. But do not doubt my friend, Salesforce has a very powerful translation framework.

Besides being able to change the language used by the Salesforce standard areas with the flick of a picklist, you can include translatable Custom Labels in most of your custom application. To do this you will need to request that the ‘Translation Workbench’ feature be enable for you Salesforce development environment, after which you will find new areas in your ‘Setup’ menu.

Post enablement you can then create a Custom Labels and include them where necessary within your code. This can be done within a VisualForce page like so

<apex:outputText value="{!$Label.customlabel}"/>

or within your Apex code thus

String err_message = System.Label.customlabel;

This in itself is awesomesauce, but you can further leverage this power by capturing what I like to call tokens within the Custom Label text e.g.

You need a translatable Custom Label that outputs some dynamically created value so you capture the text as:

There are currently {0} accounts outstanding.

You can now dynamically populate the token with a value retrieved by some method by doing the following within your page:

<apex:outputText value="{!$Label.customlabel}">
    <apex:param value="{!value}"/>

When the page is displayed it will then display the appropriate value within the text field. Should you need more tokens within your label, simply include more curly braced tokens with the values being consecutively increasing integers. Simple yet powerful. Brings a tear to the eye.

3 thoughts on “Dynamic Custom Labels”

Leave a Comment