Date and Binding Variables

Hi,
We created an Event Content Type and I have to manipulate two date fields in the binding variables. I’m able to change the format of the date as I want but not change the “Locale” of the date.

$tools.date.format("EEEE d MMMM yyyy",$sys.item.getProperty("event_start").Date)

So I always have the date in English and can not have it in French or Spanish.
Here is the method I’m trying to use : “public java.lang.String format(java.lang.String format, java.lang.Object obj,java.util.Locale locale)”

Is someone can help me on this one ?

Thanks,

Guillaume

Guillaume,

You may need to upgrade your Velocity Tools. As has been discussed here before, we currently ship Velocity Tool 1.2, and there have been numerous improvements in the later versions.

See http://forum.percussion.com/showthread.php?t=123 for a description of how to update your tools.

We will be adding the latest released version in the next Rhythmyx release.

Dave

Hi Dave,
We upgraded our Velocity Tools to 1.4 and I have the same problem. I’m still unable to change the locale of the “format” method. My problem is to instanciate a “Locale” Class and pass it in the parameters. When I use the methos “$tools.date.getLocale()” it work but it is always “en_US” and I want to change it for the same locale that my content item.

Regards,

Guillaume

Guillaume,

I see the problem: we only return the Locale of an item as a String, and you need it as a java.util.Locale object.

Let me talk to people here and see where is the best place to put this.

Dave

Guillaume,

It took me a while, but I found a way to do what you want.

In the system there’s an “old-style” UDF (you remember those, right?) that has the function you are looking for. It’s called “sys_FormatDateToLocale”, and it takes 3 parameters: the date, the format and the locale, all as Strings.

You can call old style UDFs with the $rx.ext.call JEXL function.

I created a test template…

$modifiedDate = $rx.ext.call("i18n/","sys_FormatDateToLocale", $sys.item.getProperty("rx:sys_contentlastmodifieddate").String, "EEEE d
MMMM yyyy", "fr-ca")

The result is: mardi 1 avril 2008

Is this what you were looking for?

Dave

Note: Since this was written, I’ve added $user.psoStringTools.getLocale() to the PSO Toolkit. The new and improved way to do this is:


Demain:  $tools.date.format("EEEE MMM dd, yyyy", $tomorrow, $user.psoStringTools.getLocale("fr-ca"))

Hi Dave,

Wow :smiley: This is exactly what I was trying to do.
Thanks a lot, I would never find that my self.
I’m really happy to find a good solution for that, I was starting to make a big “$rx.cond.choose” to translate every day of the week and month :confused:
This is a lot better.

Thanks again,

Guillaume

Guillaume,

Before you go off the deep end with $rx.cond.choose, there’s another thing you should consider for the next time you have a localization problem.

You can also use $rx.i18n.getString() to translate words, as long as you add keys into the /Rhythmyx/rxconfig/I18n/ResourceBundle.tmx file.

Define your own “namespace” (e.g. “hec.templatekeys”), and add keys to the TMX file such as “hec.templatekeys.dayofweek@1”. The English value of this key will be “sunday” and the french value will be “dimanche”, etc.

$locale = $sys.item.getProperty("rx:sys_lang")
$nmspc = "hec.templatekeys.dayofweek@"
$mydate = $sys.item.getProperty("rx:sys_contentlastmodifieddate").Date
$day = $mydate.get(7) 
$dayname = $rx.I18n.getString($nmspc + $day, $locale)

This is obviously more complex than the UDF solution, but when you have another translation problem this may be easier to use than if-then-else logic.

BTW, I’ve filed a bug for your original problem. The locale of the date really should be set in the getProperty() method, and you shouldn’t have to deal with this. If you’ve said that the item is a French item, all dates in that item should come out in French. No promises on when this will actually make it into the product.

Dave

PS: those of you wondering where the “7” came from, check out the javadoc for java.util.GregorianCalendar