Locale for DateFormat
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
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
1 Posted by Ryan Griffith on Oct 08, 2014 @ 02:03 PM
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:
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:
Now that you can obtain a specific Locale, I think you should be able to pass that into the Date Tool's
format()
method: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:
Please let me know if you have any questions.
Thanks!
2 Posted by Jonathan Franks on Oct 08, 2014 @ 03:12 PM
Hey thanks a lot Ryan, this did the trick.
3 Posted by Jonathan Franks on Oct 08, 2014 @ 03:14 PM
For any others with this issue, here is what I did in the end:
4 Posted by Ryan Griffith on Oct 08, 2014 @ 03:28 PM
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 Posted by Jonathan Franks on Oct 08, 2014 @ 05:14 PM
Of course. Go ahead and do with it as you will !
Thanks
6 Posted by Ryan Griffith on Oct 08, 2014 @ 05:30 PM
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!
Ryan Griffith closed this discussion on Oct 08, 2014 @ 05:30 PM.