Struts 1.3: How to set a default Locale in a web app?
Asked Answered
J

4

0

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.

Julee answered 5/12, 2008 at 11:54 Comment(0)
T
3

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.

Truckle answered 5/12, 2008 at 12:13 Comment(0)
A
1

If you want to setup a session as it is created, you could use a HttpSessionListener. Setting up a 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 could add it "programatically" by the method ServletContext.addListener

Amputee answered 27/4, 2016 at 1:34 Comment(1)
I know your question was long time ago but just in case someone else got the same issue :)Amputee
J
0

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");
    }
} 
Julee answered 8/12, 2008 at 12:27 Comment(0)
C
0

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>
Cruzcruzado answered 14/12, 2008 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.