Label Class

Provides methods to retrieve a custom label or to check if translation exists for a label in a specific language and namespace. Label names are dynamically resolved at run time, overriding the user’s current language if a translation exists for the requested language. You can’t access labels that are protected in a different namespace.

System

Custom labels enable developers to create multilingual applications by automatically presenting information (for example, help text or error messages) in a user’s native language. Custom labels have a limit of 1000 characters and can be accessed from Apex classes, Visualforce pages, Lightning pages, or Lightning components. For more information on custom labels, see Custom Labels in Salesforce Help.The label values can be translated into any language Salesforce supports. For supported languages, see Supported Languages in Salesforce Help.

  • To define custom labels, from Setup, in the Quick Find box, enter Custom Labels, and then select Custom Labels.
  • To assign translated values, turn on Translation Workbench and add translation mappings.
  • To retrieve the label for a default language setting or for a language and namespace, use System.Label.get(namespace, label, language).
  • To check if translation exists for a label and language in a namespace, use Label.translationExists(namespace, label, language).

In Apex code, you can refer to or instantiate a Label like this, where myLabelName is a placeholder value:

For information on passing in labels into Aura components, see Getting Labels in Apex in the Lightning Aura Components Developer Guide.

This example returns True if an English label called MyLabel exists in the MyNamespace namespace.

This example retrieves the custom label translation text for MyLabel in French.

The following are methods for Label.

Retrieve a custom label for the specified namespace and a default language setting.

public static String get(String namespace, String label)

  • namespace: Type: String If the namespace name is null, it defaults to the package namespace. If the namespace name is an empty string, it implies the org namespace.
  • label: Type: String The label name cannot be null or an empty string.

Type: String

Retrieve a custom label for the specified namespace and language.

public static String get(String namespace, String label, String language)

  • namespace: Type: String If the namespace name is null, it defaults to the package namespace. If the namespace name is an empty string, it implies the org namespace.
  • label: Type: String The label name cannot be null or an empty string.
  • language: Type: String This parameter must be a valid language ISO code. See the Platform-Only Languages section in Supported Languages in Salesforce Help.

Type: String

Check if translation exists for a label and language in a namespace.

public static Boolean translationExists(string namespace, string label, string language)

  • namespace: Type: String If the namespace name is null, it defaults to the package namespace. If the namespace name is an empty string, it implies the org namespace.
  • label: Type: String The label name cannot be null or an empty string.
  • language: Type: String This parameter must be a valid language ISO code. See the Platform-Only Languages section in Supported Languages in Salesforce Help.

Type: Boolean