Chris Cooper
Chris Cooper

Reputation: 899

Internationalising day-of-week output

I need to output the days of the week in a JSP. Clearly I could just do:

<fmt:message key="day.monday" />
<fmt:message key="day.tuesday" />
...

and have the days of week in locale-specific message resource files. But I notice that there is a class java.text.DateSymbols which contains utility methods for getting the day of week strings out as an array, which has a constructor which takes a locale string. I've tried to use this using <jsp:useBean> but I can't see any easy way to set the locale - using <fmt:setLocale> has no effect.

Am I going about this the wrong way? It seems like a fairly common thing to need to do and I can't see a built-in way round this.

Upvotes: 1

Views: 211

Answers (1)

Paweł Dyda
Paweł Dyda

Reputation: 18662

First of all, please keep in mind that depending on the context, day of the week could be written as nominative, dative, genitive, ... (write in other case). Therefore, using DateFormatSymbols directly is not necessary the best idea, as it may lead to Internationalization defects.
Exporting these names to resources and letting translators decide what will be best for their language is probably good idea.

Note: Do not recycle these translations, as because of grammar they might feet in one place and be totally incorrect in other, again this depends on the context.

If you want to use some Locale-dependent classes anyway, you need to create HttpSession and store Locale object to it. Then you can retrieve it in the back-end and return valid Locale-dependent information.
This is not easy though. Well, there are reasons why people created things like Spring Framework, Struts, JSF, etc. JSP on its own is really painful to use.

Upvotes: 2

Related Questions