在Struts2中使用I18n Interceptor [英] i18n Interceptor usage in Struts2

查看:97
本文介绍了在Struts2中使用I18n Interceptor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让网站多语言。



我有一个普通的拦截器堆栈,其中包含

 < interceptor-ref name =i18n/> 

用于更改区域设置的常见jsp:

 < s:a action =locale-managernamespace =/ common> 
< s:param name =request_locale> fa_IR< / s:param>
فارسی
< / s:a>

< s:a action =locale-managernamespace =/ common>
< s:param name =request_locale> en_US< / s:param>
英语
< / s:a>

一个简单的LocaleManager操作

 公共类LocaleManager扩展了ActionSupport {
private static final Logger log = LoggerFactory.getLogger(LocaleManager.class);

public String execute(){
log.debug(将local更改为{},getLocale());
返回主页;
}

}

在上面的场景中,i18n拦截器总是运行所有操作,这是没有解决方案。因为区域设置仅在用户单击locale-manager操作时更改。



我尝试删除拦截器堆栈并仅将i18n拦截器添加到LocaleManager,如下所示

  @InterceptorRefs({@InterceptorRef(i18n)})
公共类LocaleManager扩展了ActionSupport {
.... .....

但它没有用?我错过了什么,或者我应该编写自己的拦截器吗?

解决方案

始终运行 I18nInterceptor 每个Action的拦截器......它是 defaultStack 的一部分(在 struts-default.xml ),这是有原因的。



<它以一种简单的方式工作:如果有一个名为 request_locale 的请求参数,它会将新的语言环境设置为session。当调用 getText()或类似函数从本地化软件包中获取正确的消息时,将读取该值。



来自官方文档


一个拦截器,它处理将会话
中指定的语言环境设置为当前操作请求的语言环境。此外,这个
拦截器将查找特定的HTTP请求参数,并将区域设置
设置为提供的任何值。这意味着这个
拦截器可以用来允许你的应用程序动态
改变用户会话的语言环境,或者只用
当前请求(自XWork 2.1.3起) 。这对于需要多语言支持的
应用程序非常有用,并且希望用户能够在任何时候设置他或她的语言偏好。在执行此拦截器期间删除了区域设置
参数,
确保未在$ b $中没有典型相应setter的操作(例如
request_locale)上设置属性b动作。



例如,使用默认参数名称,请求
foo.action?request_locale = en_US ,然后美国英语的区域设置是
保存在用户的会话中,并将用于所有将来的请求。



如果没有设置区域设置(例如,第一次访问),
拦截器使用浏览器区域设置。




一些用法示例:




I want to make a site multilingual.

I have an normal interceptor stack which contains

<interceptor-ref name="i18n" /> 

A common jsp for changing locale:

<s:a action="locale-manager" namespace="/common">
    <s:param name="request_locale">fa_IR</s:param>
    فارسی  
</s:a>

<s:a action="locale-manager" namespace="/common">
    <s:param name="request_locale">en_US</s:param>
    English
</s:a>

And a simple LocaleManager action

public class LocaleManager extends ActionSupport{
    private static final Logger log = LoggerFactory.getLogger(LocaleManager.class);

    public String execute() {
        log.debug("Change the local to {}", getLocale() );
        return "homepage";
    }

}

In the above scenario the i18n interceptor always run for all actions, which is not got solution. Because the locale only change when user clicks on locale-manager action.

I tried to remove the interceptor stack and add i18n interceptor only to LocaleManager as below

@InterceptorRefs({ @InterceptorRef("i18n") })
public class LocaleManager extends ActionSupport{
  .........

But it did not worked ?! Am I missing something, or should I write my own interceptor ?

解决方案

Always run I18nInterceptor interceptor for every Action... It is part of the defaultStack (configured in the struts-default.xml), it is there for a reason.

It works in a simple way: if there is a request parameter named request_locale, it sets the new locale into session. That value will be then read when calling getText() or similar functions to get the proper message from the localized bundle.

From the official documentation:

An interceptor that handles setting the locale specified in a session as the locale for the current action request. In addition, this interceptor will look for a specific HTTP request parameter and set the locale to whatever value is provided. This means that this interceptor can be used to allow for your application to dynamically change the locale for the user's session or, alternatively, only for the current request (since XWork 2.1.3). This is very useful for applications that require multi-lingual support and want the user to be able to set his or her language preference at any point. The locale parameter is removed during the execution of this interceptor, ensuring that properties aren't set on an action (such as request_locale) that have no typical corresponding setter in your action.

For example, using the default parameter name, a request to foo.action?request_locale=en_US, then the locale for US English is saved in the user's session and will be used for all future requests.

If there is no locale set (for example with the first visit), the interceptor uses the browser locale.


Some usage examples:

这篇关于在Struts2中使用I18n Interceptor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆