Struts 1.3: How do I set the default locale in a web application?

I have two or three i18n files in struts application. I can switch between them by setting a variable Global.LOCALE_KEY

in the session.

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

Of course, I could intercept the call to the first page and set the variable in the session, but this is more cumbersome.

0


source to share


4 answers


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

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

      

Then forward 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 will have to modify this property string to insert it into the session. You might have to hack a version of ActionComponentServlet that does pre-initialization like this.

Probably the best way to do this is just the code that I inherited.

+3


source


If you want to customize the session as it is created, you can use the HttpSessionListener. Setting the default locale for each new session would look something like this:

package com.mycompany.web.session;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.struts.Globals;

public class LocaleController implements HttpSessionListener {

    private static Locale defaultLocale = locale.ENGLISH;

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        event.getSession().setAttribute(Globals.LOCALE_KEY, defaultLocale);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
    }
}

      

Then you add this element to your web.xml file:



<listener>
    <listener-class>com.mycompany.web.session.LocaleController</listener-class>
</listener>

      

Or you can add it "programmatically" using the ServletContext.addListener method

+1


source


Hm, I finally solved this 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 when tomcat starts up?

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");
    }
} 

      

0


source


If you only want the default resource file, just omit the language code in the filename:

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 message handling if you define your messages in default mode (which will be selected if you omit the property mode

):

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

      

and the default language standard - not the same language as in the properties without postfix: Texts.properties

.

Let's say our Texts.properties

file will contain English text. In addition, there is a German translation: Texts_de.properties

. Our default locale is French because we are running on a French server (and we did not specify it explicitly).

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

If the first request requests an English page, each subsequent request for the same page will be in English, unless there is an explicit properties file for the requested language code (which is what we want).

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

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

      

0


source







All Articles