Where did my language setting go?

T

Tim Slattery

I've got a web system written with Struts 1.3.10 that has to display
in English or Spanish. The front page always should display in
English, then the user can click a button to switch the language. It's
supposed to work this way even if the browser's language preference is
Spanish. Seems a bit odd to me too, but that's how the powers-that-be
want it.

The way it works is that a language preference is passed into the
front page. The app used this to create a Locale object specifying the
appropriate language, then uses the Struts method setLocale to save it
in the session object. It then does whatever else it needs to do and
forwards to the JSP page. The page retrieves the Locale and uses it to
determine which language to show.

The problem is that I print the saved Locale object just before
forwarding to the page. It's right, it says English when the page is
first requested without a language preference set. But when the
browser's language preference is Spanish, the page shows in Spanish.
And I can't switch it to English. Clicking the "English" link results
in a Spanish page even though the app is setting up the Locale object
correctly. I can only figure that the Locale is getting reset between
the forward command and handing control to the page. Does that make
any sense?

When the browser's language preference is English, it works right. I
get English first, and I can switch the language to Spanish.
 
T

Tim Slattery

Tim Slattery said:
The way it works is that a language preference is passed into the
front page. The app used this to create a Locale object specifying the
appropriate language, then uses the Struts method setLocale to save it
in the session object. It then does whatever else it needs to do and
forwards to the JSP page. The page retrieves the Locale and uses it to
determine which language to show.

OK, got it figured - kind of.

There were two properties files: ces_es.properties with the Spanish
stuff, and ces.properties with the English language stuff. The page
examined the Locale, and if it specified Spanish, it did:

<fmt:setBundle basename="ces_es"/>

If the Locale did not specify Spanish it did:

<fmt:setBundle basename="ces"/>

Apparently, when the browser's preference was set to Spanish, it would
tack "_es" onto the basename it searched for in the second instance.
Changing the name of the English properties file to ces_en.properties,
and changing basename to "ces_en" in the second setBundle did the
trick.

I guess I don't understand all the rules about searching for a
properties bundle.
 
M

markspace

There were two properties files: ces_es.properties with the Spanish
stuff, and ces.properties with the English language stuff. The page
examined the Locale, and if it specified Spanish, it did:

<fmt:setBundle basename="ces_es"/>

If the Locale did not specify Spanish it did:

<fmt:setBundle basename="ces"/>

I'm kind of confused why it would do this. I don't really know about
Struts or even JEE every much, so I'm probably missing something, but
getLocale() on HttpServletRequest should return the locale requested by
the browser (client). Then you should just be able to do this:

ResourceBundle labels = ResourceBundle.getBundle("ces", locale);

without having to have a special check for any language. It sounds like
the way you have it you'll need to use some sort of if-else in a cascade
to fetch additional languages, and that's almost always bad.

More on resources and searching for locales:

http://docs.oracle.com/javase/tutorial/i18n/resbundle/concept.html

'course, I don't know about Struts so I have no idea if this is even
possible. However, the attribute for setBundle is "basename" which sure
sounds to me like it should be doing this search automatically.

If it's not, you might want to find out why.
 
T

Tim Slattery

I'm kind of confused why it would do this. I don't really know about
Struts or even JEE every much, so I'm probably missing something, but
getLocale() on HttpServletRequest should return the locale requested by
the browser (client). Then you should just be able to do this:

ResourceBundle labels = ResourceBundle.getBundle("ces", locale);

You're probably right. I should check out just doing <fmt:setBundle
basename="ces"/> and see if would get the language suffix from the
Locale that's been set. I don't think it's very likely that this app
will get any more languages, but even so it would clean up the code a
lot.
 
M

markspace

You're probably right. I should check out just doing <fmt:setBundle
basename="ces"/> and see if would get the language suffix from the
Locale that's been set. I don't think it's very likely that this app
will get any more languages, but even so it would clean up the code a
lot.


I did a quick web search for "struts fmt setbundle" and found this example.


<fmt:setLocale value="en"/>
<fmt:setBundle basename="com.tutorialspoint.Example" var="lang"/>

<fmt:message key="count.one" bundle="${lang}"/><br/>

So a "setLocale" might be required as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top