org.springframework.web.servlet.i18n

Class CookieLocaleResolver

    • Field Detail

      • DEFAULT_COOKIE_NAME

        public static final java.lang.String DEFAULT_COOKIE_NAME
        The default cookie name used if none is explicitly set.
    • Method Detail

      • setLanguageTagCompliant

        public void setLanguageTagCompliant(boolean languageTagCompliant)
        Specify whether this resolver's cookies should be compliant with BCP 47 language tags instead of Java's legacy locale specification format.

        The default is true, as of 5.1. Switch this to false for rendering Java's legacy locale specification format. For parsing, this resolver leniently accepts the legacy Locale.toString() format as well as BCP 47 language tags in any case.

        Since:
        4.3
        See Also:
        parseLocaleValue(String), toLocaleValue(Locale), Locale.forLanguageTag(String), Locale.toLanguageTag()
      • isLanguageTagCompliant

        public boolean isLanguageTagCompliant()
        Return whether this resolver's cookies should be compliant with BCP 47 language tags instead of Java's legacy locale specification format.
        Since:
        4.3
      • setDefaultLocale

        public void setDefaultLocale(@Nullable
                                     java.util.Locale defaultLocale)
        Set a fixed Locale that this resolver will return if no cookie found.
      • getDefaultLocale

        @Nullable
        protected java.util.Locale getDefaultLocale()
        Return the fixed Locale that this resolver will return if no cookie found, if any.
      • setDefaultTimeZone

        public void setDefaultTimeZone(@Nullable
                                       java.util.TimeZone defaultTimeZone)
        Set a fixed TimeZone that this resolver will return if no cookie found.
        Since:
        4.0
      • getDefaultTimeZone

        @Nullable
        protected java.util.TimeZone getDefaultTimeZone()
        Return the fixed TimeZone that this resolver will return if no cookie found, if any.
        Since:
        4.0
      • resolveLocale

        public java.util.Locale resolveLocale(HttpServletRequest request)
        Description copied from interface: LocaleResolver
        Resolve the current locale via the given request. Can return a default locale as fallback in any case.
        Specified by:
        resolveLocale in interface LocaleResolver
        Parameters:
        request - the request to resolve the locale for
        Returns:
        the current locale (never null)
      • setLocale

        public void setLocale(HttpServletRequest request,
                              @Nullable
                              HttpServletResponse response,
                              @Nullable
                              java.util.Locale locale)
        Description copied from interface: LocaleResolver
        Set the current locale to the given one.
        Specified by:
        setLocale in interface LocaleResolver
        Parameters:
        request - the request to be used for locale modification
        response - the response to be used for locale modification
        locale - the new locale, or null to clear the locale
      • parseLocaleValue

        @Nullable
        protected java.util.Locale parseLocaleValue(java.lang.String localeValue)
        Parse the given locale value coming from an incoming cookie.

        The default implementation calls StringUtils.parseLocale(String), accepting the Locale.toString() format as well as BCP 47 language tags.

        Parameters:
        localeValue - the locale value to parse
        Returns:
        the corresponding Locale instance
        Since:
        4.3
        See Also:
        StringUtils.parseLocale(String)
      • toLocaleValue

        protected java.lang.String toLocaleValue(java.util.Locale locale)
        Render the given locale as a text value for inclusion in a cookie.

        The default implementation calls Locale.toString() or JDK 7's Locale.toLanguageTag(), depending on the "languageTagCompliant" configuration property.

        Parameters:
        locale - the locale to stringify
        Returns:
        a String representation for the given locale
        Since:
        4.3
        See Also:
        isLanguageTagCompliant()
      • determineDefaultLocale

        @Nullable
        protected java.util.Locale determineDefaultLocale(HttpServletRequest request)
        Determine the default locale for the given request, Called if no locale cookie has been found.

        The default implementation returns the specified default locale, if any, else falls back to the request's accept-header locale.

        Parameters:
        request - the request to resolve the locale for
        Returns:
        the default locale (never null)
        See Also:
        setDefaultLocale(java.util.Locale), ServletRequest.getLocale()
      • determineDefaultTimeZone

        @Nullable
        protected java.util.TimeZone determineDefaultTimeZone(HttpServletRequest request)
        Determine the default time zone for the given request, Called if no TimeZone cookie has been found.

        The default implementation returns the specified default time zone, if any, or null otherwise.

        Parameters:
        request - the request to resolve the time zone for
        Returns:
        the default time zone (or null if none defined)
        See Also:
        setDefaultTimeZone(java.util.TimeZone)