/** * 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, * <i>not</i> exposing it as inheritable for child threads. * @param locale the current Locale, or {@code null} to reset * the locale part of thread-bound context * @see #setTimeZone(TimeZone) * @see SimpleLocaleContext#SimpleLocaleContext(Locale) */ public static void setLocale(@Nullable Locale locale) { setLocale(locale, false); }
private void initContextHolders(HttpServletRequest request, ServletRequestAttributes requestAttributes) { LocaleContextHolder.setLocale(request.getLocale(), this.threadContextInheritable); RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable); if (logger.isTraceEnabled()) { logger.trace("Bound request context to thread: " + request); } }
@Override public void requestInitialized(ServletRequestEvent requestEvent) { if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) { throw new IllegalArgumentException( "Request is not an HttpServletRequest: " + requestEvent.getServletRequest()); } HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest(); ServletRequestAttributes attributes = new ServletRequestAttributes(request); request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes); LocaleContextHolder.setLocale(request.getLocale()); RequestContextHolder.setRequestAttributes(attributes); }
@After public void tearDown() { LocaleContextHolder.setLocale(null); }
@After public void tearDown() { LocaleContextHolder.setLocale(null); }
@Before public void setUp() { LocaleContextHolder.setLocale(Locale.US); }
@After public void tearDown() { LocaleContextHolder.setLocale(null); }
@After public void tearDown() { LocaleContextHolder.setLocale(null); }
@Test public void resolveI18nPartialLocale() throws Exception { LocaleContextHolder.setLocale(Locale.FRANCE); URL url = this.configurer.resolveTemplate(getClass().getClassLoader(), TEMPLATE_PREFIX + "i18n.tpl"); Assert.assertNotNull(url); Assert.assertThat(url.getPath(), Matchers.containsString("i18n_fr.tpl")); }
@Test public void resolveI18nDefaultLocale() throws Exception { LocaleContextHolder.setLocale(Locale.US); URL url = this.configurer.resolveTemplate(getClass().getClassLoader(), TEMPLATE_PREFIX + "i18n.tpl"); Assert.assertNotNull(url); Assert.assertThat(url.getPath(), Matchers.containsString("i18n.tpl")); }
@Test(expected = IOException.class) public void failMissingTemplate() throws Exception { LocaleContextHolder.setLocale(Locale.US); this.configurer.resolveTemplate(getClass().getClassLoader(), TEMPLATE_PREFIX + "missing.tpl"); Assert.fail(); } }
@Test public void resolveI18nFullLocale() throws Exception { LocaleContextHolder.setLocale(Locale.GERMANY); URL url = this.configurer.resolveTemplate(getClass().getClassLoader(), TEMPLATE_PREFIX + "i18n.tpl"); Assert.assertNotNull(url); Assert.assertThat(url.getPath(), Matchers.containsString("i18n_de_DE.tpl")); }
@After public void cleanup() { LocaleContextHolder.setLocale(null); DateTimeContextHolder.setDateTimeContext(null); }
@After public void cleanup() { LocaleContextHolder.setLocale(null); JodaTimeContextHolder.setJodaTimeContext(null); }
@Before public void setUp() { formattingService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(formattingService); LocaleContextHolder.setLocale(Locale.US); }
@Before public void setUp() throws Exception { TestMockServletContext servletContext = new TestMockServletContext(); appContext = new XmlWebApplicationContext(); appContext.setServletContext(servletContext); LocaleContextHolder.setLocale(Locale.US); String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; appContext.getServletContext().setAttribute(attributeName, appContext); handler = new TestController(); handlerMethod = new InvocableHandlerMethod(handler, TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class)); }
private void setup(DateFormatterRegistrar registrar) { DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); SimpleDateBean bean = new SimpleDateBean(); bean.getChildren().add(new SimpleDateBean()); binder = new DataBinder(bean); binder.setConversionService(conversionService); LocaleContextHolder.setLocale(Locale.US); }
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model, Locale locale) throws Exception { GroovyMarkupView view = createViewWithUrl(viewUrl); MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(locale); LocaleContextHolder.setLocale(locale); view.renderMergedTemplateModel(model, request, response); return response; }
private void setup(DateTimeFormatterRegistrar registrar) { conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); DateTimeBean bean = new DateTimeBean(); bean.getChildren().add(new DateTimeBean()); binder = new DataBinder(bean); binder.setConversionService(conversionService); LocaleContextHolder.setLocale(Locale.US); DateTimeContext context = new DateTimeContext(); context.setTimeZone(ZoneId.of("-05:00")); DateTimeContextHolder.setDateTimeContext(context); }
private void setup(JodaTimeFormatterRegistrar registrar) { conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); JodaTimeBean bean = new JodaTimeBean(); bean.getChildren().add(new JodaTimeBean()); binder = new DataBinder(bean); binder.setConversionService(conversionService); LocaleContextHolder.setLocale(Locale.US); JodaTimeContext context = new JodaTimeContext(); context.setTimeZone(DateTimeZone.forID("-05:00")); JodaTimeContextHolder.setJodaTimeContext(context); }