Dynamically Accessing Labels In Apex

The Summer ’23 update of Salesforce has brought forth a novel feature known as Dynamic Access Labels in Apex. This feature enables developers to utilize the System.Label.get(namespace, label, language) method to retrieve a custom label, with the option to specify a language. Consequently, developers can dynamically resolve label names during runtime, even overriding the user’s current language if a translation is available for the requested language.

For instance, consider a scenario where you have a custom label named “greeting” with translations in various languages. With this newly introduced feature, you can dynamically access the label in Apex code as follows:

String greetingLabel = System.Label.get(null, 'greeting', 'fr');

This code snippet will fetch the French translation of the “greeting” label. This can be beneficial in scenarios where you need to present information to users in various languages according to their preferences or location.

I trust this proves helpful!