Localized Date Formatting

Description

The $date Viewtool provided by dotCMS allows locale-specific date formatting, but a valid Locale instance needs to be passed to its format method.

In this example below a Locale instance is initially retrieved from the current Session. This is possible because dotCMS sets a default Locale, which may be configured globally with the following steps:

1) Add the required language (say, Dutch, or Italian) through the administration interface (CMS Admin -> Language variables).

2) Override a few properties in dotmarketing-config.properties. You can do it in your own plugin, or through the dotCMS configuration plugin (in ):

DEFAULT_LANGUAGE_COUNTRY = Italy
DEFAULT_LANGUAGE = it_IT
DEFAULT_LANGUAGE_CODE = it
DEFAULT_LANGUAGE_STR = Italian
DEFAULT_LANGUAGE_COUNTRY_CODE = IT

Note that changing these variables changes also the default language for your CMS contents, so be careful if you have different requirements.

3) After setting the Locale globally, you can retrieve it from the Session as shown in the example.

Code

## Retrieve the default Locale from the current Session
#set ( $currentLocale = $session.getAttribute("com.dotmarketing.LOCALE") )

## Use the Locale just retrieved to format the date: in this example, the month will be shown with its name in the correct language, i.e. "March" for English, "Maart" for Dutch, "Marzo" for Italian, and so on.
#set ( $formattedDate = $date.format("dd MMMM yyyy", $yourDateVariable, $currentLocale) )