/** * 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; }
/** * 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; }
/** * Associate the given Locale with the current thread, * preserving any TimeZone that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param locale the current Locale, or {@code null} to reset * the locale part of thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setTimeZone(TimeZone, boolean) * @see SimpleLocaleContext#SimpleLocaleContext(Locale) */ public static void setLocale(@Nullable Locale locale, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ? ((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given TimeZone with the current thread, * preserving any Locale that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param timeZone the current TimeZone, or {@code null} to reset * the time zone part of the thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setLocale(Locale, boolean) * @see SimpleTimeZoneAwareLocaleContext#SimpleTimeZoneAwareLocaleContext(Locale, TimeZone) */ public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); Locale locale = (localeContext != null ? localeContext.getLocale() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given TimeZone with the current thread, * preserving any Locale that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param timeZone the current TimeZone, or {@code null} to reset * the time zone part of the thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setLocale(Locale, boolean) * @see SimpleTimeZoneAwareLocaleContext#SimpleTimeZoneAwareLocaleContext(Locale, TimeZone) */ public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); Locale locale = (localeContext != null ? localeContext.getLocale() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given Locale with the current thread, * preserving any TimeZone that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param locale the current Locale, or {@code null} to reset * the locale part of thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setTimeZone(TimeZone, boolean) * @see SimpleLocaleContext#SimpleLocaleContext(Locale) */ public static void setLocale(@Nullable Locale locale, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ? ((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
@Test public void testSetTimeZoneAwareLocaleContext() { LocaleContext lc = new SimpleTimeZoneAwareLocaleContext(Locale.GERMANY, TimeZone.getTimeZone("GMT+1")); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); LocaleContextHolder.resetLocaleContext(); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
@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()); }
@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()); }
new SimpleTimeZoneAwareLocaleContext(Locale.GERMANY, TimeZone.getTimeZone("GMT+2"))); localeContext = localeContextResolver.resolveLocaleContext(request); assertEquals(Locale.GERMANY, localeContext.getLocale()); new SimpleTimeZoneAwareLocaleContext(null, TimeZone.getTimeZone("GMT+3"))); localeContext = localeContextResolver.resolveLocaleContext(request); assertEquals(Locale.UK, localeContext.getLocale());
/** * 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 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; }
/** * Associate the given TimeZone with the current thread, * preserving any Locale that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param timeZone the current TimeZone, or {@code null} to reset * the time zone part of the thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setLocale(Locale, boolean) * @see SimpleTimeZoneAwareLocaleContext#SimpleTimeZoneAwareLocaleContext(Locale, TimeZone) */ public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); Locale locale = (localeContext != null ? localeContext.getLocale() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given Locale with the current thread, * preserving any TimeZone that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param locale the current Locale, or {@code null} to reset * the locale part of thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setTimeZone(TimeZone, boolean) * @see SimpleLocaleContext#SimpleLocaleContext(Locale) */ public static void setLocale(@Nullable Locale locale, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ? ((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
new SimpleTimeZoneAwareLocaleContext(locale, timeZone)); this.locale = locale; this.timeZone = timeZone;