Search in sources :

Example 1 with SimpleTimeZoneAwareLocaleContext

use of org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext 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 2 with SimpleTimeZoneAwareLocaleContext

use of org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext in project spring-framework by spring-projects.

the class LocaleResolverTests method doTest.

private void doTest(LocaleResolver localeResolver, boolean shouldSet) {
    // create mocks
    MockServletContext context = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(context);
    request.addPreferredLocale(Locale.UK);
    MockHttpServletResponse response = new MockHttpServletResponse();
    // check original locale
    Locale locale = localeResolver.resolveLocale(request);
    assertEquals(locale, Locale.UK);
    // set new locale
    try {
        localeResolver.setLocale(request, response, Locale.GERMANY);
        if (!shouldSet)
            fail("should not be able to set Locale");
        // check new locale
        locale = localeResolver.resolveLocale(request);
        assertEquals(locale, Locale.GERMANY);
    } catch (UnsupportedOperationException ex) {
        if (shouldSet) {
            fail("should be able to set Locale");
        }
    }
    // check LocaleContext
    if (localeResolver instanceof LocaleContextResolver) {
        LocaleContextResolver localeContextResolver = (LocaleContextResolver) localeResolver;
        LocaleContext localeContext = localeContextResolver.resolveLocaleContext(request);
        if (shouldSet) {
            assertEquals(localeContext.getLocale(), Locale.GERMANY);
        } else {
            assertEquals(localeContext.getLocale(), Locale.UK);
        }
        assertTrue(localeContext instanceof TimeZoneAwareLocaleContext);
        assertNull(((TimeZoneAwareLocaleContext) localeContext).getTimeZone());
        if (localeContextResolver instanceof AbstractLocaleContextResolver) {
            ((AbstractLocaleContextResolver) localeContextResolver).setDefaultTimeZone(TimeZone.getTimeZone("GMT+1"));
            assertEquals(((TimeZoneAwareLocaleContext) localeContext).getTimeZone(), TimeZone.getTimeZone("GMT+1"));
        }
        try {
            localeContextResolver.setLocaleContext(request, response, new SimpleLocaleContext(Locale.US));
            if (!shouldSet) {
                fail("should not be able to set Locale");
            }
            localeContext = localeContextResolver.resolveLocaleContext(request);
            assertEquals(localeContext.getLocale(), Locale.US);
            if (localeContextResolver instanceof AbstractLocaleContextResolver) {
                assertEquals(((TimeZoneAwareLocaleContext) localeContext).getTimeZone(), TimeZone.getTimeZone("GMT+1"));
            } else {
                assertNull(((TimeZoneAwareLocaleContext) localeContext).getTimeZone());
            }
            localeContextResolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(Locale.GERMANY, TimeZone.getTimeZone("GMT+2")));
            localeContext = localeContextResolver.resolveLocaleContext(request);
            assertEquals(localeContext.getLocale(), Locale.GERMANY);
            assertTrue(localeContext instanceof TimeZoneAwareLocaleContext);
            assertEquals(((TimeZoneAwareLocaleContext) localeContext).getTimeZone(), TimeZone.getTimeZone("GMT+2"));
            localeContextResolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(null, TimeZone.getTimeZone("GMT+3")));
            localeContext = localeContextResolver.resolveLocaleContext(request);
            assertEquals(localeContext.getLocale(), Locale.UK);
            assertTrue(localeContext instanceof TimeZoneAwareLocaleContext);
            assertEquals(((TimeZoneAwareLocaleContext) localeContext).getTimeZone(), TimeZone.getTimeZone("GMT+3"));
            if (localeContextResolver instanceof AbstractLocaleContextResolver) {
                ((AbstractLocaleContextResolver) localeContextResolver).setDefaultLocale(Locale.GERMANY);
                assertEquals(localeContext.getLocale(), Locale.GERMANY);
            }
        } catch (UnsupportedOperationException ex) {
            if (shouldSet) {
                fail("should be able to set Locale");
            }
        }
    }
}
Also used : Locale(java.util.Locale) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) LocaleContextResolver(org.springframework.web.servlet.LocaleContextResolver)

Example 3 with SimpleTimeZoneAwareLocaleContext

use of org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext in project spring-framework by spring-projects.

the class CookieLocaleResolverTests method testSetAndResolveLocaleContextWithTimeZone.

@Test
public void testSetAndResolveLocaleContextWithTimeZone() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(new Locale("nl", ""), TimeZone.getTimeZone("GMT+1")));
    Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
    request = new MockHttpServletRequest();
    request.setCookies(cookie);
    resolver = new CookieLocaleResolver();
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertEquals("nl", loc.getLocale().getLanguage());
    assertTrue(loc instanceof TimeZoneAwareLocaleContext);
    assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
Also used : Locale(java.util.Locale) Cookie(javax.servlet.http.Cookie) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with SimpleTimeZoneAwareLocaleContext

use of org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext in project spring-framework by spring-projects.

the class CookieLocaleResolverTests method testSetAndResolveLocaleContextWithTimeZoneOnly.

@Test
public void testSetAndResolveLocaleContextWithTimeZoneOnly() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(null, TimeZone.getTimeZone("GMT+1")));
    Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
    request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.GERMANY);
    request.setCookies(cookie);
    resolver = new CookieLocaleResolver();
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertEquals(Locale.GERMANY, loc.getLocale());
    assertTrue(loc instanceof TimeZoneAwareLocaleContext);
    assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
Also used : Cookie(javax.servlet.http.Cookie) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

SimpleTimeZoneAwareLocaleContext (org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext)4 LocaleContext (org.springframework.context.i18n.LocaleContext)3 SimpleLocaleContext (org.springframework.context.i18n.SimpleLocaleContext)3 TimeZoneAwareLocaleContext (org.springframework.context.i18n.TimeZoneAwareLocaleContext)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)3 Locale (java.util.Locale)2 Cookie (javax.servlet.http.Cookie)2 Test (org.junit.Test)2 LocaleContextResolver (org.springframework.web.servlet.LocaleContextResolver)2 MockServletContext (org.springframework.mock.web.test.MockServletContext)1 LocaleResolver (org.springframework.web.servlet.LocaleResolver)1