2

Is it possible to display the date field based on the user locale. Example: if the user locale is France and date is(4-July-2016), i want to display date in Visual force email template as 4-juillet-2016 in French format and if locale is Netherlands then display date as 4-juli-2016 in template. Thanks in advance.

2 Answers 2

2

The Date and time zone is a visualforce email template is always GMT. According to How Do You Control The Timezone Of A DateTime Field In A Visualforce Email Template, you would need to do something similar to the following:

In order to format the date in various TimeZone in Email Template, follow the steps:

  1. First create a Controller class for the Component

Formatted_DateTime_Controller Class Code

public class controller_formatted_datetime
{
public DateTime date_time { get; set; } //property that reads the datetime value from component attribute tag
public String defined_format { get; set;} //property that reads the string value from component attribute tag
public String getFormattedDatetime()
{
if (date_time == null) {return ''; }
else { if (defined_format == null) {
return date_time.format(); //return the full date/time in user's locale and time zone
}
else { return date_time.format(defined_format,'PST');  //Specify Time zone like IST,CST
}}}}

If you want to support more than one time zone, get either the $User.LocaleSidKey or $User.TimeZoneSidKey from your page and add that to your logic of what to return.

  1. Create a Component

VF Component Code (Name: VFEmailTempComp)

<apex:component access="global" controller="controller_formatted_datetime">{!FormattedDatetime}
<apex:attribute assignTo="{!date_time}" description="The DateTime value to be rendered" name="date_time_value" type="DateTime"></apex:attribute>
<apex:attribute assignTo="{!defined_format}" description="The optional format to use for the DateTime value to be rendered" name="date_time_format" type="String"></apex:attribute>
</apex:component>

You'll want to make the above formatting conditional on the $User values passed to your controller.

  1. Create an Email Template and embed the component. Setup|Administration Setup|Communication Templates|Email Templates

Email Template Code

<messaging:emailTemplate subject="Testing DateTime Format" recipientType="Contact" >
<messaging:plainTextEmailBody >
Formatted: <c:VFEmailTempComp date_time_value="{!NOW()}" date_time_format="EEE MMM d kk:mm:ss z yyyy" />
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

It will display current time in PST timezone.

1

Following example of visualforce email template changes numeric representation of month to correspondent month name for user locale.

<messaging:emailTemplate recipientType="User"
  relatedToType="Opportunity"
  subject="for Opportunity: {!relatedTo.name}">
  <messaging:htmlEmailBody >
    <html>
      <body>
        <font face="arial" size="2">
          <p>Dear {!recipient.name},</p>
            <p>
            <apex:outputtext value="{!day(DATEVALUE(relatedTo.LastModifiedDate))}/"/>
            <apex:outputtext value="{!case(month(DATEVALUE(relatedTo.LastModifiedDate)),5, "May", 6, "June",7, "Juillet","None")}" rendered="{!recipient.LanguageLocaleKey=='nl_NL'}"/>

            <apex:outputtext value="{!case(month(DATEVALUE(relatedTo.LastModifiedDate)),1,"January",2,"February",3,"March",4,"April",5, "May",6,"June",7,"Juli",8,"August",9,"September",10,"October",11,"November",12,"December","None")}" rendered="{!recipient.LanguageLocaleKey=='fr'}"/>
            <apex:outputtext value="/{!year(DATEVALUE(relatedTo.LastModifiedDate))}"/>
          </p>
        </font>
      </body>
    </html>
  </messaging:htmlEmailBody> 

</messaging:emailTemplate>

If you send test email to user in locale 'fr', you'll get:

Dear Alice,

4/Juli/2016

This solution requires changes only in template itself. And it should work fine if you have two languages and need one field. If you want to translate more fields and have more languages, additional changes could be required.

You must log in to answer this question.

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