Refine search
/** * Apply the resolved status code and reason to the response. * <p>The default implementation sends a response error using * {@link HttpServletResponse#sendError(int)} or * {@link HttpServletResponse#sendError(int, String)} if there is a reason * and then returns an empty ModelAndView. * @param statusCode the HTTP status code * @param reason the associated reason (may be {@code null} or empty) * @param response current HTTP response * @since 5.0 */ protected ModelAndView applyStatusAndReason(int statusCode, @Nullable String reason, HttpServletResponse response) throws IOException { if (!StringUtils.hasLength(reason)) { response.sendError(statusCode); } else { String resolvedReason = (this.messageSource != null ? this.messageSource.getMessage(reason, null, reason, LocaleContextHolder.getLocale()) : reason); response.sendError(statusCode, resolvedReason); } return new ModelAndView(); }
@Test public void testSetLocale() { LocaleContextHolder.setLocale(Locale.GERMAN); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); LocaleContextHolder.setLocale(Locale.GERMANY); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); LocaleContextHolder.setLocale(null); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); LocaleContextHolder.setDefaultLocale(Locale.GERMAN); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); LocaleContextHolder.setDefaultLocale(null); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); }
@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()); }
/** * Return the Locale associated with the current thread, if any, * or the system default Locale otherwise. This is effectively a * replacement for {@link java.util.Locale#getDefault()}, * able to optionally respect a user-level Locale setting. * <p>Note: This method has a fallback to the shared default Locale, * either at the framework level or at the JVM-wide system level. * If you'd like to check for the raw LocaleContext content * (which may indicate no specific locale through {@code null}, use * {@link #getLocaleContext()} and call {@link LocaleContext#getLocale()} * @return the current Locale, or the system default Locale if no * specific Locale has been associated with the current thread * @see #getLocaleContext() * @see LocaleContext#getLocale() * @see #setDefaultLocale(Locale) * @see java.util.Locale#getDefault() */ public static Locale getLocale() { return getLocale(getLocaleContext()); }
/** * Return the TimeZone associated with the current thread, if any, * or the system default TimeZone otherwise. This is effectively a * replacement for {@link java.util.TimeZone#getDefault()}, * able to optionally respect a user-level TimeZone setting. * <p>Note: This method has a fallback to the shared default TimeZone, * either at the framework level or at the JVM-wide system level. * If you'd like to check for the raw LocaleContext content * (which may indicate no specific time zone through {@code null}, use * {@link #getLocaleContext()} and call {@link TimeZoneAwareLocaleContext#getTimeZone()} * after downcasting to {@link TimeZoneAwareLocaleContext}. * @return the current TimeZone, or the system default TimeZone if no * specific TimeZone has been associated with the current thread * @see #getLocaleContext() * @see TimeZoneAwareLocaleContext#getTimeZone() * @see #setDefaultTimeZone(TimeZone) * @see java.util.TimeZone#getDefault() */ public static TimeZone getTimeZone() { return getTimeZone(getLocaleContext()); }
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Assert.state(this.target != null, "No HttpRequestHandler available"); LocaleContextHolder.setLocale(request.getLocale()); try { this.target.handleRequest(request, response); } catch (HttpRequestMethodNotSupportedException ex) { String[] supportedMethods = ex.getSupportedMethods(); if (supportedMethods != null) { response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", ")); } response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage()); } finally { LocaleContextHolder.resetLocaleContext(); } }
@RequestMapping("/first") public ModelAndView getAwesomeWebsite() { final String[] params = {service.getAwesomeWebsite()}; final String msg = messageSource.getMessage("message.mostAwesomeWebsite", params, LocaleContextHolder.getLocale()); return new ModelAndView("awesomeWebsite", "website", msg); }
/** * Prepares data for mail message FORWARD form. */ @RequestMapping(value = "/messaging/mailMessage/{msgId}/forward", method = RequestMethod.GET) public String setupForwardMessageHandler(@PathVariable("msgId") String msgId, Model model, HttpServletRequest request) { verifyUserRight(msgId); String subjectForwardPrefix = messages.getMessage(MessageConstants.MESSAGING_FORWARD_SUBJECT_PREFIX, null, LocaleContextHolder.getLocale()); String bodyPrefix = messages.getMessage(MessageConstants.MESSAGING_FORWARD_BODY_PREFIX, null, LocaleContextHolder.getLocale()); String bodyWrittenBy = messages.getMessage(MessageConstants.MESSAGING_FORWARD_SOURCE_WRITTEN_BY, null, LocaleContextHolder.getLocale()); MailboxType mBoxType = mailboxService.getMailboxDetails(mailMessageService.getMailMessageDetails(msgId).getMailboxId()).getType(); MailMessageData mailMessageData = mailMessageFactory.createQuoted(mailMessageService.getMailMessageDetails(msgId), subjectForwardPrefix, bodyPrefix, bodyWrittenBy, mBoxType, true, false); mailMessageData.setParentId(msgId); model.addAttribute(MODEL_MAILMESSAGE, mailMessageData); return ViewConstants.MESSAGING_MAILBOX_MAILMESSAGE_SEND_FORWARD; }
@RequestMapping(value = "/changeLang", method = RequestMethod.POST) @ResponseBody public String changeLang(HttpServletRequest request, HttpServletResponse response, @RequestParam String lang){ LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if ("zh".equals(lang)) { localeResolver.setLocale(request, response, Locale.SIMPLIFIED_CHINESE); } else if("en".equals(lang)){ localeResolver.setLocale(request, response, Locale.US); } return "lang:" + LocaleContextHolder.getLocale().getLanguage(); }
@ApiOperation(value = "取特定语言环境的国际化消息") @RequestMapping(path = "/i18n",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json") @ResponseBody public Map<String,String> getI18nProperties(HttpServletRequest request, HttpServletResponse response){ String language= StringKit.nvl(request.getParameter("language"),"zh"); String country= StringKit.nvl(request.getParameter("country"),"CN"); LocaleContextHolder.setLocale(new Locale(language,country)); Map<String,String> properties = resourceBundleMessage.getProperties(); return properties; } }
private void addEnergyTypesListTo(final Model model) { final String energyTypes = messageSource.getMessage(Constants.ENERGY_TYPES_KEY, null, LocaleContextHolder.getLocale()); final List<OptionDTO> energyTypesList = CatalogUtils.toOptionList(energyTypes, Constants.ENERGY_TYPES_KEY, messageSource); model.addAttribute(Constants.MODEL_ENERGY_TYPES, energyTypesList); }
@GetMapping("/**") public String init(Model model) { model.addAttribute("baseUrl", URI); model.addAttribute("lng", LocaleContextHolder.getLocale().getLanguage()); model.addAttribute("fallbackLng", appSettings.getLanguageCode()); model.addAttribute("dataExplorerBaseUrl", getBaseUrl(DataExplorerController.ID)); return VIEW_TEMPLATE; } }
Model model = result.getModel(); MethodParameter parameter = result.getReturnTypeSource(); Locale locale = LocaleContextHolder.getLocale(exchange.getLocaleContext()); model.addAllAttributes(render.modelAttributes()); Object view = render.view(); if (view == null) { model.addAllAttributes(((Model) returnValue).asMap()); viewsMono = resolveViews(getDefaultViewName(exchange), locale);
/** * Return the default locale to use if no explicit locale has been given. * <p>The default implementation returns the default locale passed into the * corresponding constructor, or LocaleContextHolder's locale as fallback. * Can be overridden in subclasses. * @see #MessageSourceAccessor(org.springframework.context.MessageSource, java.util.Locale) * @see org.springframework.context.i18n.LocaleContextHolder#getLocale() */ protected Locale getDefaultLocale() { return (this.defaultLocale != null ? this.defaultLocale : LocaleContextHolder.getLocale()); }
@Test public void testSetLocaleAndSetTimeZoneMixed() { LocaleContextHolder.setLocale(Locale.GERMANY); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+1")); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); LocaleContextHolder.setLocale(Locale.GERMAN); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); LocaleContextHolder.setTimeZone(null); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+2")); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale());
@Test public void testSetTimeZone() { LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+1")); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertNull(LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+2")); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertNull(LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); LocaleContextHolder.setTimeZone(null); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); LocaleContextHolder.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1")); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); LocaleContextHolder.setDefaultTimeZone(null); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
/** * 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); }
/** * 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); }
connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength)); LocaleContext localeContext = LocaleContextHolder.getLocaleContext(); if (localeContext != null) { Locale locale = localeContext.getLocale();
private void resetContextHolders() { LocaleContextHolder.resetLocaleContext(); RequestContextHolder.resetRequestAttributes(); }