Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Where did my language setting go?

Reply
Thread Tools

Where did my language setting go?

 
 
Tim Slattery
Guest
Posts: n/a
 
      07-01-2013
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.

--
Tim Slattery
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Tim Slattery
Guest
Posts: n/a
 
      07-01-2013
Tim Slattery <(E-Mail Removed)> wrote:

>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.

--
Tim Slattery
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      07-01-2013
On 7/1/2013 11:30 AM, Tim Slattery wrote:

> 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/tutori...e/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.






 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      07-01-2013
markspace <(E-Mail Removed)> wrote:


>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.

--
Tim Slattery
(E-Mail Removed)
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      07-01-2013
On 7/1/2013 1:13 PM, Tim Slattery wrote:
> markspace <(E-Mail Removed)> wrote:
>
>
>> 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.
>



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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where can I get a "fastdebug" build of Java 7 for windows? Richard Maher Java 6 05-16-2013 12:21 AM
Re: [Chicago] Getting ASCII encoding where unicode wanted under Py3k Carl Karsten Python 0 05-13-2013 05:01 PM
Re: Getting ASCII encoding where unicode wanted under Py3k MRAB Python 0 05-13-2013 04:34 PM
Re: Getting ASCII encoding where unicode wanted under Py3k Peter Pearson Python 0 05-13-2013 04:32 PM
cisco t1 isdn call to DID or DID no ring tone optimus Cisco 1 12-31-2003 04:35 PM



Advertisments