Search in sources :

Example 1 with LocaleResolver

use of org.springframework.web.servlet.LocaleResolver in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method useDefaultLocaleIfAcceptHeaderNoSet.

@Test
public void useDefaultLocaleIfAcceptHeaderNoSet() {
    load(AllResources.class, "spring.mvc.locale:en_UK");
    // mock request and set user preferred locale
    MockHttpServletRequest request = new MockHttpServletRequest();
    LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
    assertThat(localeResolver).isInstanceOf(AcceptHeaderLocaleResolver.class);
    Locale locale = localeResolver.resolveLocale(request);
    // test locale resolver uses default locale if no header is set
    assertThat(locale.toString()).isEqualTo("en_UK");
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) Locale(java.util.Locale) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 2 with LocaleResolver

use of org.springframework.web.servlet.LocaleResolver in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method useAcceptHeaderLocale.

@Test
public void useAcceptHeaderLocale() {
    load(AllResources.class, "spring.mvc.locale:en_UK");
    // mock request and set user preferred locale
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL"));
    request.addHeader(HttpHeaders.ACCEPT_LANGUAGE, "nl_NL");
    LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
    assertThat(localeResolver).isInstanceOf(AcceptHeaderLocaleResolver.class);
    Locale locale = localeResolver.resolveLocale(request);
    // test locale resolver uses user preferred locale
    assertThat(locale.toString()).isEqualTo("nl_NL");
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) Locale(java.util.Locale) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 3 with LocaleResolver

use of org.springframework.web.servlet.LocaleResolver in project spring-framework by spring-projects.

the class RequestContext method initContext.

/**
	 * Initialize this context with the given request, using the given model attributes for Errors retrieval.
	 * <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
	 * locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
	 * @param request current HTTP request
	 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
	 * fallback to root WebApplicationContext)
	 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
	 * for Errors retrieval)
	 * @see #getFallbackLocale
	 * @see #getFallbackTheme
	 * @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
	 * @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
	 */
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, Map<String, Object> model) {
    this.request = request;
    this.response = response;
    this.model = model;
    // Fetch WebApplicationContext, either from DispatcherServlet or the root context.
    // ServletContext needs to be specified to be able to fall back to the root context!
    this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (this.webApplicationContext == null) {
        this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
        if (this.webApplicationContext == null) {
            throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " + "request and no ContextLoaderListener registered?");
        }
    }
    // Determine locale to use for this RequestContext.
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    if (localeResolver instanceof LocaleContextResolver) {
        LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
        this.locale = localeContext.getLocale();
        if (localeContext instanceof TimeZoneAwareLocaleContext) {
            this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
        }
    } else if (localeResolver != null) {
        // Try LocaleResolver (we're within a DispatcherServlet request).
        this.locale = localeResolver.resolveLocale(request);
    }
    // Try JSTL fallbacks if necessary.
    if (this.locale == null) {
        this.locale = getFallbackLocale();
    }
    if (this.timeZone == null) {
        this.timeZone = getFallbackTimeZone();
    }
    // Determine default HTML escape setting from the "defaultHtmlEscape"
    // context-param in web.xml, if any.
    this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());
    // Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
    // context-param in web.xml, if any.
    this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());
    this.urlPathHelper = new UrlPathHelper();
    if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
        this.requestDataValueProcessor = this.webApplicationContext.getBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
    }
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) UrlPathHelper(org.springframework.web.util.UrlPathHelper) LocaleContextResolver(org.springframework.web.servlet.LocaleContextResolver)

Example 4 with LocaleResolver

use of org.springframework.web.servlet.LocaleResolver in project spring-framework by spring-projects.

the class RequestContext method changeLocale.

/**
	 * Change the current locale to the specified locale and time zone context,
	 * storing the new locale context through the configured {@link LocaleResolver}.
	 * @param locale the new locale
	 * @param timeZone the new time zone
	 * @see LocaleContextResolver#setLocaleContext
	 * @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
	 */
public void changeLocale(Locale locale, TimeZone timeZone) {
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
    if (!(localeResolver instanceof LocaleContextResolver)) {
        throw new IllegalStateException("Cannot change locale context if no LocaleContextResolver configured");
    }
    ((LocaleContextResolver) localeResolver).setLocaleContext(this.request, this.response, new SimpleTimeZoneAwareLocaleContext(locale, timeZone));
    this.locale = locale;
    this.timeZone = timeZone;
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) LocaleContextResolver(org.springframework.web.servlet.LocaleContextResolver)

Example 5 with LocaleResolver

use of org.springframework.web.servlet.LocaleResolver in project spring-framework by spring-projects.

the class RequestContext method changeLocale.

/**
	 * Change the current locale to the specified one,
	 * storing the new locale through the configured {@link LocaleResolver}.
	 * @param locale the new locale
	 * @see LocaleResolver#setLocale
	 * @see #changeLocale(java.util.Locale, java.util.TimeZone)
	 */
public void changeLocale(Locale locale) {
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
    if (localeResolver == null) {
        throw new IllegalStateException("Cannot change locale if no LocaleResolver configured");
    }
    localeResolver.setLocale(this.request, this.response, locale);
    this.locale = locale;
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver)

Aggregations

LocaleResolver (org.springframework.web.servlet.LocaleResolver)9 Locale (java.util.Locale)4 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)4 Test (org.junit.Test)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 FixedLocaleResolver (org.springframework.web.servlet.i18n.FixedLocaleResolver)3 SimpleTimeZoneAwareLocaleContext (org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext)2 LocaleContextResolver (org.springframework.web.servlet.LocaleContextResolver)2 ResourceBundle (java.util.ResourceBundle)1 Matcher (java.util.regex.Matcher)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 LocaleContext (org.springframework.context.i18n.LocaleContext)1 TimeZoneAwareLocaleContext (org.springframework.context.i18n.TimeZoneAwareLocaleContext)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)1 MockPageContext (org.springframework.mock.web.test.MockPageContext)1 MockServletContext (org.springframework.mock.web.test.MockServletContext)1 HttpSessionRequestCache (org.springframework.security.web.savedrequest.HttpSessionRequestCache)1 SavedRequest (org.springframework.security.web.savedrequest.SavedRequest)1