[SalesForce] Get User Language in Visualforce without controller

I have a Visualforce page without a controller and I would like to get the user LanguageLocaleKey value without having to add a controller (I know it can be done in Apex using UserInfo.getLanguage())

I tought it could be done using the $User global variable

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_variables_global_user.htm

but if I try to put {!$User.LanguageLocaleKey} in the Visualforce page I get an error:

Error: Field LanguageLocaleKey does not exist. Check spelling

Is it possible to do it without a controller ?

Is there a reference of the available fields for global variable $User ? The documentation link above is not really helpful

Best Answer

Looks like language field is not available via $User global variable. Here is a knowledge article about it

https://help.salesforce.com/apex/HTViewSolution?id=000205451&language=en_US

I checked for the available values for user and Language is not one of them. However there are other ways to implement this

  1. Use Remote object and query the languageISOCode from there

    < apex:page>

    <  apex:remoteObjects jsNamespace="RemoteObjectModel">
      <  apex:remoteObjectModel name="User" jsShorthand="userRec"
            fields="Id,LanguageLocaleKey">
        </apex:remoteObjectModel>
    </apex:remoteObjects>
    
    <input type="button" onclick="fetchUsers();" value="MyButton"/>
    
    <script>
        var fetchUsers = function(){
            // Create a new Remote Object
            var ur = new RemoteObjectModel.userRec();
            var userID = '{!$user.id}';
            // Use the Remote Object to query for 10 warehouse records
            ur.retrieve({where: {Id: {eq:userID}}, limit: 10 }, function(err, records, event){
                if(err) {
                    alert(err.message);
                }
                else {
                    console.log(records[0].get('LanguageLocaleKey'));
                }
            });
        };
    </script>
    

  2. Use SFDC JS Toolkit to query it (JS Wrapper for APIs). Connection.js

Related Topic