[SalesForce] get current user Language

I'm aware of that if you have this UserInfo.getLanguage() you get the language code but I'm looking to find a way to get the name of the Language say instead of ja I want Japan

I tried looking at the User object but could not find Language instead I found
LanguageLocaleKey,LocaleSidKey

Best Answer

The values are translated into the user's language, so you can't reliably get "Japanese" (for example) because it'll only be that value in some languages. You can find the translated value using the following code:

String language, userLanguage = UserInfo.getLanguage();
for(PicklistEntry value: User.LanguageLocalekey.getDescribe().getPicklistValues()) {
    if(value.getValue() == userLanguage) {
        language = value.getLabel();
        break;
    }
}

At the end of this loop, language will contain the user's selected language translated into their language.

If you don't mind using a query, you can also query for it using toLabel:

User u = [SELECT toLabel(LanguageLocaleKey) FROM User WHERE Id = :UserInfo.getUserId()];
System.debug(u.LanguageLocaleKey);

If you query a bunch of users, all of the LanguageLocaleKey values will be the queried user's language in the user's current language.

Related Topic