Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL fmt:message and resource bundle

Tags:

java

jstl

struts2

I want to set the "dir" property of my table from resource bundle based on the locale.

Here is snippet:

        <fmt:setBundle basename="class.path.to.resource.bundle"/>
        <table align=center class="" dir=<fmt:message key="registration.direction"/>>

When the page renders I get this:

   <table align=center dir=???registration.direction???>

I have two resource bundles for english and arabic.

registration.direction = ltr -> English

registration.direction = rtl -> Arabic

Please tell what I am doing wrong? The dir should have "ltr" or "rtl" depending on the locale.

Thanks

BR SC

like image 807
SmoothCriminel Avatar asked Jan 12 '11 10:01

SmoothCriminel


People also ask

What is fmt bundle?

The <fmt:bundle> tag loads the resource bundle which is used by its tag body. This tag will make the specified bundle available for all <fmt:message> tags that occurs between the boundary of <fmt:bundle> and </fmt:bundle> tags. It is used to create the ResourceBundle objects which will be used by their tag body.

What is FMT message?

The <fmt:message> tag is used for displaying an internationalized message. It maps the key of localized message to return the value using a resource bundle specified in the bundle attribute.

What is the optional attribute in FMT bundle?

prefix : This is an optional attribute that may be used for adding to the beginning to the value of the key of <fmt:message> action.


1 Answers

two things

1) I would add a variable to store the message result in

<fmt:message key="registration.direction" var="direction" />

then

2) I would do the following with your code

  <fmt:setBundle basename="class.path.to.resource.bundle"/>
  <fmt:message key="registration.direction" var="direction" />
  <table align=center class="" dir="${direction}">

Now as far as your resource bundles, typically You should have the following structure for your resource bundles

/foo/bar/MyResourceBundle.properties
/foo/bar/MyResourceBundle_en.properties
/foo/bar/MyResourceBundle_en_US.properties
/foo/bar/MyResourceBundle_<lang>[_COUNTRY[_VAR]].properties

If your bundle is not structured in this fashion that might be some of your problem.

Make sure that all keys that are expected to be available are defined in MyResourceBundle with reasonable defaults.

I'm amending this answer as I'm not sure if my comment got lost in a hide function.

With the fact that you are using Struts 2, I'm under the impression that you're using the i18n interceptor. The interceptor will store the current locale in the sesion variable named WW_TRANS_I18N_LOCALE. As such you should be able to get to it and set the locale for the JSTL tags by using the following:

<fmt:setLocale scope="session" value="${sessionScope.WW_TRANS_I18N_LOCALE}" />

Hope that works for you.

like image 187
Dave G Avatar answered Sep 19 '22 10:09

Dave G