Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Getting Labels in Apex
Custom Labels
Custom labels have a limit of 1,000 characters and can be accessed from an Apex class. To define custom labels, from Setup, in the Quick Find box, enter Custom Labels, and then select Custom Labels.
1public with sharing class LabelController {
2 @AuraEnabled
3 public static String getLabel() {
4 String s1 = 'Hello from Apex Controller, ' ;
5 String s2 = System.Label.MyLabelName;
6 return s1 + s2;
7 }
8}1({
2 doInit : function(component, event, helper) {
3 var action = component.get("c.getLabel");
4 action.setCallback(this, function(response) {
5 var state = response.getState();
6 if (state === "SUCCESS") {
7 component.set("v.mylabel", response.getReturnValue());
8 }
9 // error handling when state is "INCOMPLETE" or "ERROR"
10 });
11 $A.enqueueAction(action);
12 }
13})Finally, make sure you wire up the Apex class to your component. The label is set on the component during initialization.
1<aura:component controller="LabelController">
2 <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
3 <aura:attribute name="mylabel" type="String"/>
4 {!v.mylabel}
5</aura:component>Passing in Label Values
1<aura:component controller="LabelController">
2 <aura:attribute name="mylabel" type="String" default=" "/>
3 <lightning:input name="mytext" label="{!v.mylabel}"/>
4</aura:component>You can also retrieve labels in JavaScript code, including dynamically creating labels that are generated during runtime. For more information, see Getting Labels in JavaScript.
Retrieving Custom Labels Using System.Label Methods
You can use methods in the System.Label class to check for and retrieve translated labels.
- To check if translation exists for a label and language in a namespace, use translationExists(namespace, label, language).
- To retrieve the custom label for a specified namespace and default language setting, use get(namespace, label).
- To retrieve the custom label for a specified namespace and language, use get(namespace, label, language).