aspose file tools*
The moose likes Struts and the fly likes Getting ApplicationResources from a Custome Request Processor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Getting ApplicationResources from a Custome Request Processor" Watch "Getting ApplicationResources from a Custome Request Processor" New topic
Author

Getting ApplicationResources from a Custome Request Processor

Angelo Bonaparte
Greenhorn

Joined: Aug 02, 2006
Posts: 19
Hi I have an application that needs to access the ApplicationResources_en or ApplicationResources_fr depending of the locale of the user.

We have a Customer request processor that extends the struts request processor. In the processPreprocess we try to access the properties file which should be either fr or en depending on the locale but the en, default properties files is the only one we can get too. We are trying to find a way to load it but seem to fail. Any help appreciated. Thanks. See code below.

Therefore the following code:

// Return the requested message resources instance
MessageResources resources = (MessageResources) request.getAttribute(
"org.apache.struts.action.MESSAGE" );

Always returns the English file....
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Hi I have an application that needs to access the ApplicationResources_en or ApplicationResources_fr depending of the locale of the user.

We have a Customer request processor that extends the struts request processor. In the processPreprocess we try to access the properties file which should be either fr or en depending on the locale but the en, default properties files is the only one we can get too. We are trying to find a way to load it but seem to fail. Any help appreciated. Thanks. See code below.

Therefore the following code:

// Return the requested message resources instance
MessageResources resources = (MessageResources) request.getAttribute(
"org.apache.struts.action.MESSAGE" );

Always returns the English file....


hi,
gave a thought over your problem.
here is what ( i think ) needs to be done in your processPreprocess( ) method -

1. read the clients locale first -
Locale locale = request.getHeader("Accept-Language");
2. now you need to retrieve property file corresponding to this locale
look out for chapter 10 of the Strus Complete Reference book.

to do this, the classes that you will use are org.apache.struts.util.MessageResources and / or
org.apache.struts.util.PropertyMessageResources

note that you locale specific files mut be like ApplicationResources_fr.properties ( file for french locale ).

i believe the Struts Complete Reference book has chapters in detail that deal with internationlization of you struts application.

Hope this help you get going. Please do share your code with me.
Addditionally you can also go here internationalization. But this example uses the ResourceBundle, and PropertyResourceBundel classes of java.util package , which is not serializable ( not desired if your application is distributed ). To overcome this struts provides classes ( give above ), which duplicate the functionality of the java classes, and are serializable.

Hope this helps you ( this has come out of m 8 months of experience at the job )

LEt me know the progrees.
Thanks


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It's important to understand that the Struts MessageResources object is not a file representing a single language, but an object that represents the entire Resource Bundle including all supported languages.

Once you have this object, you can get a message for any language by passing a Locale object to its getMessage method. For example:

getMessage(new Locale("en"), "some.key")

retrieves the message in English, while

getMessage(new Locale("fr"), "some.key")

retrieves the message in French.

Calling the method that only accepts a key:

getMessage("some.key")

retrieves the message from the default locale, which in your case appears to be English.
[ October 06, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
hey merril, i think angelo is talking to load a complete .prpoperties file depending on the client's locale. i think he isnt talking to load some specific entry from the file.

how do we do it ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting ApplicationResources from a Custome Request Processor