更改struts2的默认语言环境

3

我该如何修改struts.properties文件中指定的默认区域设置为struts.locale的Struts2默认区域设置?

默认语言环境是“en”,我需要将其更改为“en_US”

我尝试了以下方法:

<constant name="struts.locale" value="en_US" />

在我的struts.xml文件中。

然后发生了什么? - undefined
我仍然只能获得"en"作为区域设置。但是我需要更改它,因为我有像message-resources_en_US.properties这样的文件。 - undefined
2个回答

2

我知道这个回答有点晚了,但总有一天会有人发现它正是他或她长期寻找的内容。

Struts2框架根据浏览器的语言偏好设置默认区域设置,即查看Accept-language请求头,如果没有找到,则转到struts属性。

因此,如果您想更改为区域设置en_US,则应在浏览器参数中设置为首选语言。

如果您想更改此行为,则可以编写拦截器,该拦截器将为ActionContext设置所需的区域设置。这里是API的引用:http://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html#setLocale(java.util.Locale)不要忘记将您的拦截器放在struts.xml文件中的拦截器堆栈中。

有关创建自己的拦截器的教程:http://www.tutorialspoint.com/struts_2/struts_interceptors.htm

希望能帮助到某些人。


1

在 Yan Pak 的回答基础上,使用以下自定义拦截器来硬编码区域设置,忽略 Struts 基于浏览器的 Accept-Language 头部信息所尝试的任何操作。

import java.util.Locale;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class USLocaleInterceptor implements Interceptor {
  private static final long serialVersionUID = 1L;

  @Override
  public void destroy() { }

  @Override
  public void init() { }

  @Override
  public String intercept(ActionInvocation invocation) throws Exception {
    invocation.getInvocationContext().setLocale(Locale.US);
    return invocation.invoke();
  }
}

谢谢,这很有帮助。我补充一下,这个新的拦截器应该在默认的param拦截器之前被调用,因为默认的拦截器会用它来进行数字格式化。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接