SOLVED

Re: Localize Date Format in Token (Velocity)

Go to solution
Anonymous
Not applicable

Localize Date Format in Token (Velocity)

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.

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Localize Date Format in Token (Velocity)

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).

View solution in original post

6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

Re: Localize Date Format in Token (Velocity)

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
Level 10 - Community Moderator

Re: Localize Date Format in Token (Velocity)

L. Perez please check the above answer.

Vivian_Corwin
Level 1

Re: Localize Date Format in Token (Velocity)

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}

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Localize Date Format in Token (Velocity)

Remember to use the Syntax Highlighter when posting code so it’s readable (I edited your post this time). Also, it’s far better to open a new thread rather than using one from 5 years ago!

 

I assume you don’t have access to your consultant so we’re not duplicating work.

 

1) Yes, the long month is “MMMM”.

 

2/3) You don’t need to change any code to change the name of a {{my.token}}. But what you do need to when copying a new token is make sure all the necessary fields are checked off in the tree on the right-hand-side of Script Editor. In this case, it looks like you’re only using one field: Expire_Date_mkto__c.

 

 

 

Anonymous
Not applicable

Re: Localize Date Format in Token (Velocity)

Thank you Sanford. This was just what I needed.

SanfordWhiteman
Level 10 - Community Moderator

Re: Localize Date Format in Token (Velocity)

Cool, gotta do a blog post on this.