i18n Interceptor usage in Struts2

老子叫甜甜 提交于 2019-11-26 21:41:38

问题


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 ?


回答1:


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:

  • Struts 2 – i18n or localization example
  • Localization (i18n) in struts2
  • Struts 2 I18N (Struts 2 Interationalization)


来源:https://stackoverflow.com/questions/19546363/i18n-interceptor-usage-in-struts2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!