Struts2 中 i18n 拦截器的使用 [英] i18n Interceptor usage in Struts2

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

问题描述

我想让网站多语言.

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

I have an normal interceptor stack which contains

<interceptor-ref name="i18n" /> 

用于更改语言环境的常用jsp:

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>

还有一个简单的 LocaleManager 操作

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

}

在上面的场景中,i18n 拦截器总是针对所有操作运行,这没有得到解决.因为区域设置只会在用户点击区域设置管理器操作时发生变化.

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.

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

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 ?

推荐答案

始终运行 I18nInterceptor 每个动作的拦截器...它是 defaultStack 的一部分(配置在struts-default.xml),就在那里出于某种原因.

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.

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

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.

来自官方文档:

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

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.

例如,使用默认参数名称,请求foo.action?request_locale=en_US,那么美国英语的语言环境是保存在用户的会话中,并将用于所有未来的请求.

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 拦截器的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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