SOLVED

Localize Date Format in Token (Velocity)

Go to solution
Highlighted
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
Highlighted
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

4 REPLIES 4
Highlighted
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

Highlighted
Level 10 - Community Moderator

Re: Localize Date Format in Token (Velocity)

L. Perez please check the above answer.

Highlighted
Anonymous
Not applicable

Re: Localize Date Format in Token (Velocity)

Thank you Sanford. This was just what I needed.

Highlighted
Level 10 - Community Moderator

Re: Localize Date Format in Token (Velocity)

Cool, gotta do a blog post on this.