Saturday, February 16, 2019

How to get User Locale information in Lightning Components

Lightning framework provide $Locale which can be used to get current user preferred locale. For example you can get user preferred timezone, language, currency format etc.

In Spring'19, Salesforce introduces new date-time $Locale attributes for date- time formattingas mentioned below:
  • longDateFormat ("MMMM d, yyyy")                              
  • shortDateFormat ("M/d/yyyy")
  • shortDatetimeFormat ("M/d/yyyy h:mm a")
  • shortTimeFormat ("h:mm a")
You can refer this global variable either directly in lightning component markup or in controller.js  file.

Below is sample code:
<aura:application extends="force:slds">
<c:SK_UserLocaleInfoCmp/>
</aura:application>
<aura:component>
<div style="background-color:#E6E6FA;border-style: solid;margin:5%;padding:2%;">
<b>Getting User Locale Information in Lightning Component Markup</b>
<table>
<tr>
<td>Country</td><td>{!$Locale.country}</td>
</tr>
<tr>
<td>Timezone</td><td>{!$Locale.timezone}</td>
</tr>
<tr>
<td>Currency</td><td>{!$Locale.currency}</td>
</tr>
<tr>
<td>Long Date Format</td><td>{!$Locale.longDateFormat}</td>
</tr>
</table>
</div>
<aura:attribute type="String" name="ltngCurrencyCode"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<div style="background-color:#E6E6FA;border-style: solid;margin:5%;padding:2%;">
<b>Getting User Locale Information from Controller.js</b>
<table>
<tr>
<td>Currency Code</td><td>{!v.ltngCurrencyCode}</td>
</tr>
</table>
</div>
</aura:component>
({
doInit : function(component, event, helper) {
var currCode = $A.get("$Locale.currencyCode");
component.set("v.ltngCurrencyCode",currCode);
}
})

Snapshot of lightning component

For detailed information on Locale global variable, please refer below URL:
$Locale in Lightning Component


Hope this will help!!!

1 comment:

  1. very interesting article. Thank you so much. I read a similar article from https://www.privatewriting.com/research-papers-for-sale about frameworks and I want to say that the material is useful in both places. just guys are professionally engaged in writing articles and essays

    ReplyDelete