Java Struts 1.3:如何在 Web 应用程序中设置默认语言环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/343609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:40:22  来源:igfitidea点击:

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

javalocalestruts-1

提问by Leonel

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

我的 struts 应用程序中有两个或三个 i18n 文件。我可以通过Global.LOCALE_KEY在会话中设置变量来在这些之间切换。

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 ?

有没有办法为应用程序设置默认语言环境(我猜可能在 struts-config.xml 文件中)?会话是设置语言环境的唯一地方吗?

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

当然,我可以拦截对第一页的调用并在会话中设置变量,但这更麻烦。

回答by JeeBee

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

在您的 web.xml 中,您可以定义一个上下文参数:

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

Then up front in your webapp:

然后在你的 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.

尽管您必须更改该属性行以将其粘贴到会话中。您可能需要修改一个 ActionComponentServlet 的版本,它可以像这样进行预初始化。

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

可能有更好的方法来做到这一点,这只是我继承的代码。

回答by Leonel

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

嗯,我终于通过编写Java代码而不是使用struts-config.xml解决了这个问题。

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

我创建了一个上下文侦听器来设置 Struts 类中静态字段的值。

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

看到这个问题: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");
    }
} 

回答by Eduard Wirch

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 modeproperty):

编辑:Struts 1.x 中存在一个关于默认消息处理的错误,如果您在默认模式下定义消息(如果省略该mode属性,则会选择该模式):

<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.

并且默认语言环境与没有后缀的属性中的语言不同:Texts.properties

Let's say our Texts.propertiesfile 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).

假设我们的Texts.properties文件将包含英文文本。此外还有一个德语翻译:Texts_de.properties. 我们的默认系统区域设置是法语,因为我们在法语服务器上运行(并且我们没有明确设置它)。

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:

此问题的解决方案是为每个消息资源声明设置 mode 属性:

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

回答by Unknown

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:

如果要在创建会话时设置会话,可以使用 HttpSessionListener。为每个新会话设置默认语言环境如下所示:

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:

然后将此元素添加到 web.xml 文件中:

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

Or you could add it "programatically" by the method ServletContext.addListener

或者您可以通过 ServletContext.addListener 方法“以编程方式”添加它