获取操作类中i18n拦截器检测到的用户区域设置 [英] Get user locale detected by i18n interceptor in action class

查看:233
本文介绍了获取操作类中i18n拦截器检测到的用户区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Struts 2的Web应用程序.我将i18n拦截器与所有默认设置一起使用.我认为该拦截器的工作原理如下:

I have a webapp that uses Struts 2. I use the i18n interceptor with all the default settings. I thought this interceptor worked as follow:

  1. 如果有一个参数request_locale,请删除该参数,然后 将此语言环境放在会话中以备将来使用.
  2. 如果没有参数,请在httpheader中搜索语言环境并将其放置 会话中的语言环境以供将来使用.
  1. If there is a parameter request_locale, remove this parameter and place this locale in the session for future use.
  2. If there is NO parameter, search the httpheader for the locale and place this locale in the session for future use.

因此,在我的操作类(实现sessionAware)中,我具有以下方法:

So in my action class (which implements sessionAware) I have the following method:

public String getUserLocale()
{
    return (String) session.get("WW_TRANS_I18N_LOCALE");
}

但是,此方法在情况#2中不起作用,它仅返回null.所以我的问题是:如果没有显式的请求参数,我如何让我的操作知道i18n拦截器检测到的用户区域设置?因为它没有存储在会话中?

However, this method does not work in situation #2, it just returns null. So my question is: how can I let my action know the user locale that the i18n interceptor detected if there is no explicit request parameter? Because it is not stored in the session?

推荐答案

您不应将拦截器定义的会话密钥用于内部使用,以获取操作区域设置.

You shouldn't use a session key defined by the interceptor for internal usage to get an action locale.

如果i18n拦截器检测到request_locale参数,则创建请求的语言环境,并将其置于操作上下文以及拦截器指定的键下的会话中.

If a request_locale parameter is detected by the i18n interceptor the requested locale is created and put to the action context and also to the session under the key specified by the interceptor.

如果对拦截器的下一个请求不包含request_locale参数,则它将获取保存在会话中的语言环境,如果未找到,则使用请求语言环境.

If the next request to the interceptor doesn't include request_locale parameter then it gets a locale saved in the session, if not found the request locale is used.

再次,拦截器将返回的语言环境放置到操作上下文中.因此,要获得struts标记使用的语言环境,您应该使用

Again the interceptor put returned locale to the action context. So, to get a locale used by the struts tags you should use

 Locale locale = ActionContext.getContext().getLocale(); 

这篇关于获取操作类中i18n拦截器检测到的用户区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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