public class I18nInterceptor extends AbstractInterceptor
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, it also looks for specific cookie to read locale from. 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.
| Modifier and Type | Class and Description |
|---|---|
protected class |
I18nInterceptor.CookieLocaleHandler |
protected static interface |
I18nInterceptor.LocaleHandler
Uses to handle reading/storing Locale from/in different locations
|
protected class |
I18nInterceptor.RequestOnlyLocaleHandler |
protected class |
I18nInterceptor.SessionLocaleHandler |
protected static class |
I18nInterceptor.Storage |
| Modifier and Type | Field and Description |
|---|---|
protected String |
attributeName |
static String |
DEFAULT_COOKIE_ATTRIBUTE |
static String |
DEFAULT_COOKIE_PARAMETER |
static String |
DEFAULT_PARAMETER |
static String |
DEFAULT_REQUEST_ONLY_PARAMETER |
static String |
DEFAULT_SESSION_ATTRIBUTE |
protected LocaleProvider |
localeProvider |
protected String |
parameterName |
protected String |
requestCookieParameterName |
protected String |
requestOnlyParameterName |
protected I18nInterceptor.Storage |
storage |
| Constructor and Description |
|---|
I18nInterceptor() |
| Modifier and Type | Method and Description |
|---|---|
protected Parameter |
findLocaleParameter(ActionInvocation invocation,
String parameterName) |
protected Locale |
getLocaleFromParam(Object requestedLocale)
Creates a Locale object from the request param, which might
be already a Local or a String
|
protected I18nInterceptor.LocaleHandler |
getLocaleHandler(ActionInvocation invocation)
Override this method to use your own implementation of
I18nInterceptor.LocaleHandler |
String |
intercept(ActionInvocation invocation)
Override to handle interception
|
void |
setAttributeName(String attributeName) |
void |
setLocaleProvider(LocaleProvider localeProvider) |
void |
setLocaleStorage(String storageName) |
void |
setParameterName(String parameterName) |
void |
setRequestCookieParameterName(String requestCookieParameterName) |
void |
setRequestOnlyParameterName(String requestOnlyParameterName) |
protected void |
useLocale(ActionInvocation invocation,
Locale locale)
Save the given locale to the ActionInvocation.
|
destroy, initpublic static final String DEFAULT_SESSION_ATTRIBUTE
public static final String DEFAULT_PARAMETER
public static final String DEFAULT_REQUEST_ONLY_PARAMETER
public static final String DEFAULT_COOKIE_ATTRIBUTE
public static final String DEFAULT_COOKIE_PARAMETER
protected String parameterName
protected String requestOnlyParameterName
protected String attributeName
protected String requestCookieParameterName
protected I18nInterceptor.Storage storage
protected LocaleProvider localeProvider
public void setParameterName(String parameterName)
public void setAttributeName(String attributeName)
public void setRequestOnlyParameterName(String requestOnlyParameterName)
public void setRequestCookieParameterName(String requestCookieParameterName)
public void setLocaleStorage(String storageName)
public void setLocaleProvider(LocaleProvider localeProvider)
public String intercept(ActionInvocation invocation) throws Exception
AbstractInterceptorintercept in interface Interceptorintercept in class AbstractInterceptorinvocation - the action invocationActionInvocation.invoke(), or from the interceptor itself.Exception - any system-level error, as defined in Action.execute().protected I18nInterceptor.LocaleHandler getLocaleHandler(ActionInvocation invocation)
I18nInterceptor.LocaleHandlerinvocation - current action invocation contextI18nInterceptor.LocaleHandlerprotected Locale getLocaleFromParam(Object requestedLocale)
requestedLocale - the parameter from the requestprotected Parameter findLocaleParameter(ActionInvocation invocation, String parameterName)
protected void useLocale(ActionInvocation invocation, Locale locale)
invocation - The ActionInvocation.locale - The locale to save.Copyright © 2000–2017 Apache Software Foundation. All rights reserved.