Change the user's @link(java.util.Locale) based on @link(ActionForm)
properties.
This Action
looks for language
and
country
and variant
properties on the given
form, constructs an appropriate Locale object, and sets it as the Struts
Locale for this user's session as well as the JSTL locale.
Any ActionForm, including a @link(DynaActionForm), may be used.
If a page
property is also provided, then after
setting the Locale, control is forwarded to that URI path.
Otherwise, control is forwarded to "success".
// Extract attributes we will need
HttpSession session = request.getSession();
Locale locale = getLocale(request);
String language = null;
String country = null;
String variant = null;
String page = null;
try {
language = (String) PropertyUtils.getSimpleProperty(form, "language");
country = (String) PropertyUtils.getSimpleProperty(form, "country");
variant = (String) PropertyUtils.getSimpleProperty(form, "variant");
page = (String) PropertyUtils.getSimpleProperty(form, "page");
} catch (Exception e) {
log.error(e.getMessage(), e);
}
boolean isLanguage = language != null && language.length() > 0;
boolean isCountry = country != null && country.length() > 0;
boolean isVariant = variant != null && variant.length() > 0;
if ( isLanguage && isCountry && isVariant ) {
locale = new java.util.Locale(language, country, variant);
} else if ( isLanguage && isCountry ) {
locale = new java.util.Locale(language, country);
} else if ( isLanguage ) {
locale = new java.util.Locale(language, "");
}
// reset the Struts locale
session.setAttribute(Globals.LOCALE_KEY, locale);
// reset the JSTL locale
Config.set(session, Config.FMT_LOCALE, locale);
if (null==page || "".equals(page))
return mapping.findForward(SUCCESS);
else
return new ActionForward(page);