wood burning stoves 2.0*
The moose likes Struts and the fly likes locale attribute in html:errors is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "locale attribute in html:errors is not working" Watch "locale attribute in html:errors is not working" New topic
Author

locale attribute in html:errors is not working

Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Hi,

I am trying to implement internationalizing error messages concept in amy application. At first I am trying with Spanish language, so I have the property file ApplicationResources_es.properties

In struts-config.xml I made an entry:


Now I want to display error message in Spanish language. So I have this code in my jsp:

ist not working, its just displaying error message in English language.

But if I put
like this, its showing the error message in Spanish.

I dont want to specify the bundle key directly, just based on the locale I want to display error message.

Please tell me what is wrong I am doing? why locale is not working?


Thanks.


Javardhan:
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The entry in your struts-config.xml file is incorrect. You should not create separate entries for each language, but only one entry for the entire bundle. Struts will then select the correct properties file based on the user's preferred language set in the browser.

For example, suppose you want to support English, Italian, and Spanish. You would create the following files in WEB-INF/classes/example:

ApplicationResources.properties (the default in case the locale cannot be determined)
ApplicationResources_en.properties (english)
ApplicationResources_es.properties (spanish)
ApplicationResources_it.properties (italian)

The entry in your struts-config.xml file would simply be:

<message-resources parameter="example.ApplicationResources" />

The message-resources stanza in struts-config.xml identifies only the base name of the bundle. It not language specific.

Struts then picks the language based on the preferred language set in the user's browser. If you want the user to be able to select the language, then you can set the locale programatically by using the setLocale() method in the Action class.

This link explains struts messages in greater detail. I'd strongly recommend you read it.


Merrill
Consultant, Sima Solutions
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Merrill,

Thank You so much for your information, I had gone thru that link, its helpful one.

I have removed those entries in struts-config, and I have the entry in JSP like this:

But still its displaying english message.

Hope I am not using the locale attribute properly, pls tell me what actually I need to give to the locale attribute...how I need to use that?

Thanks.
[ November 21, 2006: Message edited by: Ja vardhan ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm not sure I understand why you want to specify the language in the JSP. The whole point of Struts internationalization is to present content to the user in the language of their choice (not the language of your choice).

If you want to see the error messages in Spanish, simply put:

<html:errors />

in your JSP. Then set the preferred language of your browser to Spanish. To do this in Internet Explorer, select tools->internet options. Then from the general tab, select the languages button. Add Spanish as a language and make sure it's at the top of the list.


If you insist on forcing the message to display in spanish regarless of the user's browser settings, the way to do it would be to put the following statement in your Action class prior to the dispaly of this JSP:

setLocale(request, new java.util.Locale("es"));
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Hi Merrill,

Here I will explain you my requirement clearly.

In JSP I have one text box to enter the name and one dorpdown select box to choose the language:


Now user has to enter the name in text box and has to choose the language.
Once after he clicks on Submit....As you said am setting the locale in Action class based on the user selected language and the Welcome message will be displayed in that particular language.

Here the Name is mandatory field since I am applying 'required' rule in validation.xml :


If the user didn't enter any name in that text box....the error message will be displayed in the choosen language (by default English).

To diaply the error message....if I simply put <html:errors/> in my JSP....its showing the English message only (even though I have selected other language and had set the locale in Action class).

From apache site link , this is the description regarding locale attribute:
The session attribute key for the Locale used to select messages to be displayed. If not specified, defaults to the Struts standard value.


I dint understand how to use 'locale' attribute under html:errors .

Hope you understood my problem clearly.

Pls reply me.

Thanks.
[ November 21, 2006: Message edited by: Ja vardhan ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Let me also clear something up. The locale attribute of the <html:errors> tag is not for specifying a specific locale such as "en" or "es". It is for specifying a key or attribute name of an attribute in session scope that holds the locale to be used. Here's what the documentation says:
locale - The session attribute key for the Locale used to select messages to be displayed. If not specified, defaults to the Struts standard value.

The problem here is that if the user's entry fails validation, the execute method of your action class is never called, and hence, the locale is not set. It therefore reverts to the default, which is English.

Here's a possible workaround: The reset() method of an ActionForm is called prior to the validation being done. You could override this method to set a locale based on the user input. Example:



Having done this, the <html:errors> tag should display the message in the newly set locale
[ November 22, 2006: Message edited by: Merrill Higginson ]
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Merrill,

Thank you so much....its working now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: locale attribute in html:errors is not working
 
Similar Threads
Bilingual JSP application
Setting setBundle in sesssion scope
Custom ResourceBundle class?...
Problem changing languages struts 1.2.9
Problem with html:errors and [CODE]i18n[/CODE]