How can I use the custom labels and the “query” keyword within the salesforce?

247    Asked by CrownyHasegawa in Salesforce , Asked on Apr 23, 2024

 I am a Salesforce administrator and I am responsible for maintaining q multinational organization Salesforce Instance. How can I use the custom label and the “query” keyword within the salesforce to streamline the process of translating user interface elements and custom messages into multiple languages? 

Answered by debbie Jha

 In the context of Salesforce, here are the steps given below:-

Create custom labels

You can start by creating the custom labels for each user interface element or the custom message which would need to be translated. The custom labels are the key-value pairs where the key represents the label’s unique identifier.

    Label Key: Welcome_Message

Label Value (English): Welcome to our platform!

    Label Value (French): Bienvenue sur notre plateforme!

Using the custom labels In apex code and visual force

Visualforce pages, and the lightning Component, replace hard–cider text with the reference to the custom labels. Thus, this would ensure that the appropriate language-specific text should be displayed based on the user's language settings.

    String welcomeMessage = System.Label.Welcome_Message;

Query custom label dynamically

For the purpose of dynamically retrieving the custom label values based on the users language preference or the language of a record by using the “query” keyword in SOQL queries.

Public class LocalizationController {
    Public String getWelcomeMessage() {
        String labelKey = ‘Welcome_Message’;
        String languageCode = UserInfo.getLanguage(); // Retrieve the user’s language code
        // Query the custom label using the “query” keyword
        List customLabels = [SELECT Label FROM CustomLabel WHERE Name = :labelKey AND Language = :languageCode LIMIT 1];
        If (!customLabels.isEmpty()) {
            Return customLabels[0].Label;
        } else {
            // Fallback to default label if translation not available
            Return System.Label.Welcome_Message;
        }
    }
}

Maintaining and updating custom label

You should regularly review and also update the custom labels as needed when the new translation we available to the users interface element. It would provide a user friendly interface for the purpose of managing custom labels, making it easy so that you can maintain and update transaction.

Here is the example given in java programming language:-

Import java.io.IOException;
Import java.io.InputStream;
Import java.util.Locale;
Import java.util.Properties;
Public class LocalizationService {
    Private static final String DEFAULT_LANGUAGE = “en”; // Default language

    Private Properties properties;

    Public LocalizationService(String language) {
        Properties = new Properties();
        String fileName = “messages_” + language + “.properties”;
        Try (InputStream input = getClass().getClassLoader().getResourceAsStream(fileName)) {
            If (input == null) {
                fileName = “messages_” + DEFAULT_LANGUAGE + “.properties”; // Fallback to default language
                input = getClass().getClassLoader().getResourceAsStream(fileName);
            }
            Properties.load(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Public String getMessage(String key) {
        Return properties.getProperty(key, “Message not found”);
    }
    Public static void main(String[] args) {
        // Simulate user’s language preference (e.g., “en” for English, “fr” for French)
        String languagePreference = “fr”;
        LocalizationService localizationService = new LocalizationService(languagePreference);
        String welcomeMessage = localizationService.getMessage(“welcome_message”);
        System.out.println(welcomeMessage);
    }
}


Your Answer

Interviews

Parent Categories