* ����locale��
*/
@Override
public void prepare() {
// ���ȴ�session��ȡ��input charset�������õ�request�У��Ա��һ������request parameters��
LocaleInfo locale = getLocaleFromSession();
try {
// ��ͼ��queryString��ȡ��inputCharset
String queryString = getRequest().getQueryString();
String inputCharset = locale.getCharset().name();
if (queryString != null) {
Matcher matcher = inputCharsetPattern.matcher(queryString);
if (matcher.find()) {
String charset = null;
if (matcher.groupCount() >= 2) {
charset = matcher.group(2);
}
if (LocaleUtil.isCharsetSupported(charset)) {
inputCharset = charset;
} else {
log.warn("Specified input charset is not supported: " + charset);
}
}
}
getRequest().setCharacterEncoding(inputCharset);
log.debug("Set INPUT charset to " + inputCharset);
} catch (UnsupportedEncodingException e) {
try {
getRequest().setCharacterEncoding(CHARSET_DEFAULT);
log.warn("Unknown charset " + locale.getCharset() + ". Set INPUT charset to " + CHARSET_DEFAULT);
} catch (UnsupportedEncodingException ee) {
log.error("Failed to set INPUT charset to " + locale.getCharset());
}
}
// ��parameter��ȡlocale��Ϣ��������ڣ������õ�cookie�С�
if (PARAMETER_SET_TO_DEFAULT_VALUE.equalsIgnoreCase(getRequest().getParameter(paramKey))) {
HttpSession session = getRequest().getSession(false); // ���session�����ڣ�Ҳ���ô���
if (session != null) {
session.removeAttribute(sessionKey);
}
locale = new LocaleInfo(defaultLocale, defaultCharset);
log.debug("Reset OUTPUT locale:charset to " + locale);
} else {
// ��ͼ��queryString��ȡ��outputCharset
String queryString = getRequest().getQueryString();
String outputCharset = null;
if (queryString != null) {
Matcher matcher = outputCharsetPattern.matcher(queryString);
if (matcher.find()) {
String charset = null;
if (matcher.groupCount() >= 2) {
charset = matcher.group(2);
}
if (LocaleUtil.isCharsetSupported(charset)) {
outputCharset = charset;
} else {
log.warn("Specified output charset is not supported: " + charset);
}
}
}
// ���parameter��ָ����locale����ȡ�ò�����֮
LocaleInfo paramLocale = getLocaleFromParameter();
if (paramLocale != null) {
getRequest().getSession().setAttribute(sessionKey, paramLocale.toString());
// ��parameter�е�locale��Ϣ����cookie����Ϣ��
locale = paramLocale;
}
if (outputCharset != null) {
locale = new LocaleInfo(locale.getLocale(), outputCharset);
}
}
// �������������locale��Ϣ��
getResponse().setLocale(locale.getLocale());