9

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

1 Answer 1

17

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.

2
  • Not sure should I create a new question or not but if you change Language to Japan (日本語) and when I run the above code I'm getting 日本語 instead of Japanese or Japan.
    – Nick
    Commented Apr 19, 2017 at 0:07
  • 2
    @NickKahn That's what I tried to explain in the beginning. Display values are translated to the running user's language.
    – sfdcfox
    Commented Apr 19, 2017 at 4:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .