Locale for DateFormat

Jonathan Franks's Avatar

Jonathan Franks

Oct 07, 2014 @ 08:15 PM

Hey guys

I am using the DateTool to format dates in a Format except that I can't figure out how to specify which locale to use for the date format other than the Locale that is returned by $_DateTool.getLocale().

Here is some sample code of what I'm doing:

    #set ($defaultDateFormat = $_DateTool.getDateFormat("MM-dd-yyyy", $_DateTool.getLocale(), $_DateTool.getTimeZone()))

I'd like to be able to do something like this:

    #set ($defaultDateFormat = $_DateTool.getDateFormat("MM-dd-yyyy", new Locale("fr"), $_DateTool.getTimeZone()))

That way, we could display the dates formatted for a French locale, a Spanish locale, etc. Is there any way to do this?

Thanks

  1. 1 Posted by Ryan Griffith on Oct 08, 2014 @ 02:03 PM

    Ryan Griffith's Avatar

    Hi Jonathan,

    Looking over the Locale documentation, I noticed there are constants defined for each available Locale. I believe you should be able to use a combination of the Field Tool and Date Tool to obtain a specific Locale:

    $_FieldTool.in($_DateTool.getLocale()).FRENCH
    

    Here is a direct link to the Locale constants within the documentation for reference.

    Note: it looks like this list is missing some common languages. What you can also do is loop over the available Locales to obtain the one you are looking for:

    #set ( $availableLocales = $_DateTool.getLocale().getAvailableLocales() )
    #foreach($locale in $availableLocales)
      #if ($locale == "fr")
        ...
      #end
    #end
    

    Now that you can obtain a specific Locale, I think you should be able to pass that into the Date Tool's format() method:

    #set ($defaultDateFormat = $_DateTool.getDateFormat("MM-dd-yyyy", $locale, $_DateTool.getTimeZone()))
    

    Also, I believe you should be able to just to the following if you simply need to output a formatted date for a specific Locale:

    $_DateTool.format("MM-dd-yyyy", $dateObj, $locale)
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by Jonathan Franks on Oct 08, 2014 @ 03:12 PM

    Jonathan Franks's Avatar

    Hey thanks a lot Ryan, this did the trick.

  3. 3 Posted by Jonathan Franks on Oct 08, 2014 @ 03:14 PM

    Jonathan Franks's Avatar

    For any others with this issue, here is what I did in the end:

    #macro(setCurrentLocale $code)
    
    ## Loop on all java.util.Locale.availableLocales() and try to find a match with the supplied code.
    #foreach ($locale in $_DateTool.getLocale().getAvailableLocales())
    
        #if ($code == $locale)
            #set ($currentLocale = $locale)
            #break($foreach)
        #end
    
    #end
    
    ## If we couldn't find a matching Locale, use the default.
    #if (!$currentLocale)
        #set ($currentLocale = $_DateTool.getLocale())
    #end
    
    #end
    
  4. 4 Posted by Ryan Griffith on Oct 08, 2014 @ 03:28 PM

    Ryan Griffith's Avatar

    Thank you for following up, Jonathan, I am glad to hear the suggested code helped out.

    Also, thank you for the macro, I can see it coming in handy. Would you be open to the idea of adding it to our Velocity Cookbook project under the macros section?

    Thanks!

  5. 5 Posted by Jonathan Franks on Oct 08, 2014 @ 05:14 PM

    Jonathan Franks's Avatar

    Of course. Go ahead and do with it as you will !

    Thanks

  6. 6 Posted by Ryan Griffith on Oct 08, 2014 @ 05:30 PM

    Ryan Griffith's Avatar

    Thanks, Jonathan. I went ahead and [added it to the project]9https://github.com/hannonhill/Velocity-Cookbook/tree/master/Macros).

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  7. Ryan Griffith closed this discussion on Oct 08, 2014 @ 05:30 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac