001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet;
018
019import java.util.Locale;
020
021import javax.servlet.http.HttpServletRequest;
022import javax.servlet.http.HttpServletResponse;
023
024import org.springframework.lang.Nullable;
025
026/**
027 * Interface for web-based locale resolution strategies that allows for
028 * both locale resolution via the request and locale modification via
029 * request and response.
030 *
031 * <p>This interface allows for implementations based on request, session,
032 * cookies, etc. The default implementation is
033 * {@link org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver},
034 * simply using the request's locale provided by the respective HTTP header.
035 *
036 * <p>Use {@link org.springframework.web.servlet.support.RequestContext#getLocale()}
037 * to retrieve the current locale in controllers or views, independent
038 * of the actual resolution strategy.
039 *
040 * <p>Note: As of Spring 4.0, there is an extended strategy interface
041 * called {@link LocaleContextResolver}, allowing for resolution of
042 * a {@link org.springframework.context.i18n.LocaleContext} object,
043 * potentially including associated time zone information. Spring's
044 * provided resolver implementations implement the extended
045 * {@link LocaleContextResolver} interface wherever appropriate.
046 *
047 * @author Juergen Hoeller
048 * @since 27.02.2003
049 * @see LocaleContextResolver
050 * @see org.springframework.context.i18n.LocaleContextHolder
051 * @see org.springframework.web.servlet.support.RequestContext#getLocale
052 * @see org.springframework.web.servlet.support.RequestContextUtils#getLocale
053 */
054public interface LocaleResolver {
055
056        /**
057         * Resolve the current locale via the given request.
058         * Can return a default locale as fallback in any case.
059         * @param request the request to resolve the locale for
060         * @return the current locale (never {@code null})
061         */
062        Locale resolveLocale(HttpServletRequest request);
063
064        /**
065         * Set the current locale to the given one.
066         * @param request the request to be used for locale modification
067         * @param response the response to be used for locale modification
068         * @param locale the new locale, or {@code null} to clear the locale
069         * @throws UnsupportedOperationException if the LocaleResolver
070         * implementation does not support dynamic changing of the locale
071         */
072        void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale);
073
074}