Localize Date Format in Token (Velocity) | Community
Skip to main content
June 22, 2018
Solved

Localize Date Format in Token (Velocity)

  • June 22, 2018
  • 1 reply
  • 5604 views

I am trying to create different tokens (each language a separate token), that will display the date format in the specified language.

Examples:

English Token: March 31, 2018

French Token: 31 mars 2018

Spanish Token: 31 de marzo de 2018

Portuguese Token: 31 de março de 2018

So far, I have the English token formatted correctly but specifying a language for the other tokens is not working.

English Token:

#set( $inTimeZone = $date.getTimeZone().getTimeZone('America/New_York') )

#set( $outTimeZone = $date.getTimeZone().getTimeZone('America/New_York') )

#set( $locale = $date.getLocale() )

#set( $myDate = $convert.parseDate($lead.Member_Expire_Date,'yyyy-MM-dd',$locale,$inTimeZone) )

${date.format('d MMMM, yyyy',$myDate,$locale,$outTimeZone)}

Do we have to load a library of locales prior to defining it? Any other suggestions?

I would like to do this without web hooks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

Locales are already there. You need to instantiate Locale objects (specifically for ES and PT because there are no built-in constants for those in JRE6).

#set( $inTimeZone = $date.getTimeZone().getTimeZone('America/New_York') )

#set( $outTimeZone = $date.getTimeZone().getTimeZone('America/New_York') )

#set( $locale = $date.getLocale() )

#set( $String = $context.getClass().forName("java.lang.String") )

#set( $LocaleFrom2Part = $locale.getClass().getConstructor($String,$String))

#set( $locales = {

  "en_US" : $LocaleFrom2Part.newInstance("en","US"),

  "fr_FR" : $LocaleFrom2Part.newInstance("fr","FR"),

  "es_ES" : $LocaleFrom2Part.newInstance("es","ES"),

  "pt_PT" : $LocaleFrom2Part.newInstance("pt","PT")

})

#set( $myDate = $convert.parseDate($lead.Member_Expire_Date,'yyyy-MM-dd',$locale,$inTimeZone) )

${date.format("d MMMM, yyyy",$myDate,$locales.en_US,$outTimeZone)}

${date.format("d MMMM yyyy",$myDate,$locales.fr_FR,$outTimeZone)}

${date.format("d 'de' MMMM 'de' yyyy",$myDate,$locales.es_ES,$outTimeZone)}

${date.format("d 'de' MMMM 'de' yyyy",$myDate,$locales.pt_PT,$outTimeZone)}

I don't know why you'd use separate tokens, though. That's just way more to maintain. There should be one token, and output should be governed by some other field on the lead (i.e. their preferred locale).

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
June 22, 2018

Locales are already there. You need to instantiate Locale objects (specifically for ES and PT because there are no built-in constants for those in JRE6).

#set( $inTimeZone = $date.getTimeZone().getTimeZone('America/New_York') )

#set( $outTimeZone = $date.getTimeZone().getTimeZone('America/New_York') )

#set( $locale = $date.getLocale() )

#set( $String = $context.getClass().forName("java.lang.String") )

#set( $LocaleFrom2Part = $locale.getClass().getConstructor($String,$String))

#set( $locales = {

  "en_US" : $LocaleFrom2Part.newInstance("en","US"),

  "fr_FR" : $LocaleFrom2Part.newInstance("fr","FR"),

  "es_ES" : $LocaleFrom2Part.newInstance("es","ES"),

  "pt_PT" : $LocaleFrom2Part.newInstance("pt","PT")

})

#set( $myDate = $convert.parseDate($lead.Member_Expire_Date,'yyyy-MM-dd',$locale,$inTimeZone) )

${date.format("d MMMM, yyyy",$myDate,$locales.en_US,$outTimeZone)}

${date.format("d MMMM yyyy",$myDate,$locales.fr_FR,$outTimeZone)}

${date.format("d 'de' MMMM 'de' yyyy",$myDate,$locales.es_ES,$outTimeZone)}

${date.format("d 'de' MMMM 'de' yyyy",$myDate,$locales.pt_PT,$outTimeZone)}

I don't know why you'd use separate tokens, though. That's just way more to maintain. There should be one token, and output should be governed by some other field on the lead (i.e. their preferred locale).

SanfordWhiteman
New Participant
June 25, 2018

L. Perez please check the above answer.

Vivian_Corwin
New Participant
February 6, 2024

Hello, 

 

Apologies, as I meant to reply at the end of the conversation and cannot delete my reply. 

 

A consultant we employed created a token called {{my.Date_Formatted}} with this code (pasted below). Please note we don't have experience with script tokes or velocity script. 

 

There are a few issues:

 1) Only the first three letters of the month display in the email, and we need the entire month spelled out. I think that the solution is to add an "M" to the code so that it's "userout" : "MMMM d, yyyy" (not "userout" : "MMM d, yyyy"). Will someone please verify that this is correct?

 

2) The token was created in a local program and we need it at the top level so other email programs can use the token. Marketo will not allow me to edit  script and save the edited token. 

 

3) Per the below velocity script, does the token need to be named {{my.Date_Formatted}}? Or can I create a new token and name it something else and will the script still work? If I need to edit the script in order to change the token name, where do I need to do so? I created a new token, made some educated guesses and edited in the places I bolded below, but the test did not work.

 
Any guidance here would be much appreciated. Thanks!

 

#set( $dateOptions = { "formats" : { "userin" : "yyyy-MM-dd", "userout" : "MMM d, yyyy" }, "timezones" : { "userin" : "America/Los_Angeles", "userout" : "America/Los_Angeles" }, "locale" : $date.getLocale() } ) #set( $myDatelike = ${lead.Expire_Date_mkto__c} ) #set( $myDate = $convert.parseDate( $myDatelike, $dateOptions.formats.userin, $dateOptions.locale, $date.getTimeZone().getTimeZone($dateOptions.timezones.userin) ) ) #set( $myDate_formatted = $date.format( $dateOptions.formats.userout, $myDate, $dateOptions.locale, $date.getTimeZone().getTimeZone($dateOptions.timezones.userout) ) ) ${myDate_formatted}