Search in sources :

Example 6 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 7 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 8 with LocaleResolver

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

the class WebMvcAutoConfigurationTests method overrideLocale.

@Test
public void overrideLocale() throws Exception {
    load(AllResources.class, "spring.mvc.locale:en_UK", "spring.mvc.locale-resolver=fixed");
    // 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(FixedLocaleResolver.class);
    Locale locale = localeResolver.resolveLocale(request);
    // test locale resolver uses fixed locale and not user preferred locale
    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 9 with LocaleResolver

use of org.springframework.web.servlet.LocaleResolver in project Asqatasun by Asqatasun.

the class PageListWrapper method getResourceBundle.

/**
     * This method resolves the language through the request.
     *
     * @param request
     * @return
     */
private ResourceBundle getResourceBundle(ServletRequest request) {
    ResourceBundle resourceBundle = ResourceBundle.getBundle(I18N_FILE_KEY);
    ;
    if (request instanceof HttpServletRequest) {
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver((HttpServletRequest) request);
        if (localeResolver != null) {
            // get current locale
            Locale locale = localeResolver.resolveLocale((HttpServletRequest) request);
            resourceBundle = ResourceBundle.getBundle(I18N_FILE_KEY, locale);
        }
    }
    return resourceBundle;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LocaleResolver(org.springframework.web.servlet.LocaleResolver) Locale(java.util.Locale) ResourceBundle(java.util.ResourceBundle)

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