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.CookieLocaleFinder |
protected class |
I18nInterceptor.RequestOnlyLocaleFinder |
protected class |
I18nInterceptor.SessionLocaleFinder |
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(HttpParameters params,
String parameterName) |
protected I18nInterceptor.RequestOnlyLocaleFinder |
getLocaleFinder(ActionInvocation invocation) |
protected Locale |
getLocaleFromParam(Object requestedLocale)
Creates a Locale object from the request param, which might
be already a Local or a String
|
String |
intercept(ActionInvocation invocation)
Override to handle interception
|
protected Locale |
readStoredLocale(ActionInvocation invocation)
Reads the locale from the session, and if not found from the
current invocation (=browser)
|
protected Locale |
readStoredLocaleFromCookie(ActionInvocation ignore) |
protected Locale |
readStoredLocalFromCurrentInvocation(ActionInvocation invocation) |
protected Locale |
readStoredLocalFromSession(ActionInvocation invocation) |
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 Locale |
storeLocale(ActionInvocation invocation,
Locale locale)
Store the locale to the chosen storage, like f.
|
protected void |
storeLocaleInCookie(ActionInvocation ignore,
Locale locale) |
protected void |
storeLocaleInSession(ActionInvocation invocation,
Locale locale) |
protected void |
useLocale(ActionInvocation invocation,
Locale locale)
Save the given locale to the ActionInvocation.
|
destroy, init
public 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
AbstractInterceptor
intercept
in interface Interceptor
intercept
in class AbstractInterceptor
invocation
- the action invocationActionInvocation.invoke()
, or from the interceptor itself.Exception
- any system-level error, as defined in Action.execute()
.protected I18nInterceptor.RequestOnlyLocaleFinder getLocaleFinder(ActionInvocation invocation)
protected Locale storeLocale(ActionInvocation invocation, Locale locale)
invocation
- the action invocationlocale
- the locale to storeprotected void storeLocaleInCookie(ActionInvocation ignore, Locale locale)
protected void storeLocaleInSession(ActionInvocation invocation, Locale locale)
protected Locale readStoredLocale(ActionInvocation invocation)
invocation
- the current invocationprotected Locale getLocaleFromParam(Object requestedLocale)
requestedLocale
- the parameter from the requestprotected Locale readStoredLocalFromSession(ActionInvocation invocation)
protected Locale readStoredLocaleFromCookie(ActionInvocation ignore)
protected Locale readStoredLocalFromCurrentInvocation(ActionInvocation invocation)
protected Parameter findLocaleParameter(HttpParameters params, String parameterName)
protected void useLocale(ActionInvocation invocation, Locale locale)
invocation
- The ActionInvocation.locale
- The locale to save.Copyright © 2000–2016 Apache Software Foundation. All rights reserved.