views:

4744

answers:

3

I have two or three i18n files in my struts application. I am able to switch between these by setting the Global.LOCALE_KEY variable in the session.

Is there a way to set a default locale for the application (probably in the struts-config.xml file, I guess) ? Is the session the only place to set the locale ?

Sure, I could intercept the call to the first page and set the variable in the session, but that's more cumbersome.

+1  A: 

In your web.xml you can define a context-param:

<context-param>
 <param-name>LOCALE</param-name>
 <param-value>en-GB</param-value>
</context-param>

Then up front in your webapp:

java.util.Enumeration<String> setout = servletContext.getInitParameterNames();
while (setout.hasMoreElements()) {
    String paramName = setout.nextElement();
    configProperties.put(paramName, servletContext.getInitParameter(paramName));
}

although you'll have to change that properties line to stick it on the session instead. You may need to hack up a version of ActionComponentServlet that does pre-initialisation like this.

There's probably a better way to do this, this is just code that I inherited.

JeeBee
A: 

Hm, I finally solved this one by writing Java code instead of using struts-config.xml.

I created a context listener to set the value of a static field in the Struts class.

See this question: Is there a way to run a method/class only on tomcat startup?

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class AppContextListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent event) { /* empty. */ }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        /*
         * Default locale
         */
        ServletContext sc = event.getServletContext();
        sc.setAttribute(org.apache.struts.Globals.LOCALE_KEY, "pt_BR");
    }
}
Leonel
A: 

If you just need a resource file to be selected as default simply omit the language code in the file name:

Texts_en_GB.properties
Texts_pt_BR.properties
Texts.propertiers ( <-- this one will be selected when no resources for requested language could be found)

EDIT: There is a bug in Struts 1.x regarding default messages handling if you define your messages in default mode (which will be choosed if you omit the mode property):

<message-resources key="Texts" parameter="com.mycompany.Texts" null="false"/>

and the default locale is not the same language as in the properties without postfix: Texts.properties.

Let's say our Texts.properties file will contain English text. Additionally there is a German translation: Texts_de.properties. Our default system locale is French because we're running on a French server (and we didn't set it explicitly).

If your first request after server startup requests the German translation of a page all subsequent requests of the same page will be served in German if there is no explicit properties file for the requested language code.

If the first request asks for a English page every subsequent requests of the same page will be served in English if there is no explicit properties file for the requested language code (which is what we want).

The solution for this problem is to set the mode property for every message resource declaration:

<message-resources key="Texts" parameter="com.mycompany.Texts" null="false">
    <set-property key="mode" value="JSTL" />
</message-resources>
Eduard Wirch