Tomas Bozzo Tomas Bozzo - 1 year ago 93
Java Question

Struts 1 custom localization

I need some help with the following problem. We have an existing web application that uses no framework at all (just servlets) and we need to add new functionalities but instead of adding a new servlet to the

, we are going to add Struts 1 along with Spring.

Since the project already exists, we have to configure Struts to be able to get along with the conventions already in place. One of those is localization.

Instead of using .property files, all localizable texts come from our database so I implemented my own MessageResourcesFactory and MessageResources classes. The problem is that in our application we get the user language from an Integer session attribute called "LANGUAGE" and not from the Locale attribute "org.apache.struts.action.LOCALE" that Struts uses by default.

The question is this. How do I tell Struts to fetch the user language from my custom Integer attribute instead of the stantard Locale attribute?


Answer Source

As far as I know there is no direct way to tell Struts to use another session attribute for using the locale. And even if Struts could do this, you'd need to implement a mapping between your custom integer and a "real" Locale object.

My suggestions:

  • Implement your own javax.servlet.http.HttpSessionAttributeListener which syncs the LANGUAGE attribute to the corresponding Struts locale attribute
  • Or change the code where you set your LANGUAGE attribute to also set the Struts session attribute
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download