Example usage for org.springframework.web.servlet.i18n CookieLocaleResolver LOCALE_REQUEST_ATTRIBUTE_NAME

List of usage examples for org.springframework.web.servlet.i18n CookieLocaleResolver LOCALE_REQUEST_ATTRIBUTE_NAME

Introduction

In this page you can find the example usage for org.springframework.web.servlet.i18n CookieLocaleResolver LOCALE_REQUEST_ATTRIBUTE_NAME.

Prototype

String LOCALE_REQUEST_ATTRIBUTE_NAME

To view the source code for org.springframework.web.servlet.i18n CookieLocaleResolver LOCALE_REQUEST_ATTRIBUTE_NAME.

Click Source Link

Document

The name of the request attribute that holds the Locale .

Usage

From source file:org.broadleafcommerce.common.web.BroadleafLocaleResolverImpl.java

@Override
public Locale resolveLocale(WebRequest request) {
    Locale locale = null;//w w  w . j a  va 2s.c  om

    // First check for request attribute
    locale = (Locale) request.getAttribute(LOCALE_VAR, WebRequest.SCOPE_REQUEST);

    // Second, check for a request parameter
    if (locale == null && BLCRequestUtils.getURLorHeaderParameter(request, LOCALE_CODE_PARAM) != null) {
        String localeCode = BLCRequestUtils.getURLorHeaderParameter(request, LOCALE_CODE_PARAM);
        locale = localeService.findLocaleByCode(localeCode);
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            request.removeAttribute(BroadleafCurrencyResolverImpl.CURRENCY_VAR,
                    WebRequest.SCOPE_GLOBAL_SESSION);
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("Attempt to find locale by param " + localeCode + " resulted in " + locale);
        }
    }

    // Third, check the session
    if (locale == null && BLCRequestUtils.isOKtoUseSession(request)) {
        locale = (Locale) request.getAttribute(LOCALE_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Attempt to find locale from session resulted in " + locale);
        }

        if (locale != null) {
            request.setAttribute(LOCALE_PULLED_FROM_SESSION, Boolean.TRUE, WebRequest.SCOPE_REQUEST);
        }

    }

    // Finally, use the default
    if (locale == null) {
        locale = localeService.findDefaultLocale();
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            request.removeAttribute(BroadleafCurrencyResolverImpl.CURRENCY_VAR,
                    WebRequest.SCOPE_GLOBAL_SESSION);
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("Locale set to default locale " + locale);
        }
    }

    // Set the default locale to override Spring's cookie resolver
    request.setAttribute(LOCALE_VAR, locale, WebRequest.SCOPE_REQUEST);
    java.util.Locale javaLocale = BroadleafRequestContext.convertLocaleToJavaLocale(locale);
    request.setAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME, javaLocale,
            WebRequest.SCOPE_REQUEST);

    if (BLCRequestUtils.isOKtoUseSession(request)) {
        request.setAttribute(LOCALE_VAR, locale, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    return locale;
}