Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #24552 > unrolled thread
| Started by | Tim Slattery <Slattery_T@bls.gov> |
|---|---|
| First post | 2013-07-01 12:59 -0400 |
| Last post | 2013-07-01 13:37 -0700 |
| Articles | 5 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
Where did my language setting go? Tim Slattery <Slattery_T@bls.gov> - 2013-07-01 12:59 -0400
Re: Where did my language setting go? Tim Slattery <Slattery_T@bls.gov> - 2013-07-01 14:30 -0400
Re: Where did my language setting go? markspace <markspace@nospam.nospam> - 2013-07-01 12:09 -0700
Re: Where did my language setting go? Tim Slattery <Slattery_T@bls.gov> - 2013-07-01 16:13 -0400
Re: Where did my language setting go? markspace <markspace@nospam.nospam> - 2013-07-01 13:37 -0700
| From | Tim Slattery <Slattery_T@bls.gov> |
|---|---|
| Date | 2013-07-01 12:59 -0400 |
| Subject | Where did my language setting go? |
| Message-ID | <hbd3t8prm69278clpjolii23cuortojbn7@4ax.com> |
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 Slattery_T@bls.gov
[toc] | [next] | [standalone]
| From | Tim Slattery <Slattery_T@bls.gov> |
|---|---|
| Date | 2013-07-01 14:30 -0400 |
| Message-ID | <p7i3t89561o32oj1kaejfdq7ib78e8oolt@4ax.com> |
| In reply to | #24552 |
Tim Slattery <Slattery_T@bls.gov> 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 Slattery_T@bls.gov
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2013-07-01 12:09 -0700 |
| Message-ID | <kqsjra$odd$1@dont-email.me> |
| In reply to | #24553 |
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/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.
[toc] | [prev] | [next] | [standalone]
| From | Tim Slattery <Slattery_T@bls.gov> |
|---|---|
| Date | 2013-07-01 16:13 -0400 |
| Message-ID | <tho3t8houvs8urtv080j4g9dv7cci4ln34@4ax.com> |
| In reply to | #24554 |
markspace <markspace@nospam.nospam> 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
Slattery_T@bls.gov
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2013-07-01 13:37 -0700 |
| Message-ID | <kqsovn$1t1$1@dont-email.me> |
| In reply to | #24555 |
On 7/1/2013 1:13 PM, Tim Slattery wrote:
> markspace <markspace@nospam.nospam> 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web