/** * Change the current locale to the specified one, * storing the new locale through the configured {@link LocaleResolver}. * @param locale the new locale * @see LocaleResolver#setLocale * @see #changeLocale(java.util.Locale, java.util.TimeZone) */ public void changeLocale(Locale locale) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request); if (localeResolver == null) { throw new IllegalStateException("Cannot change locale if no LocaleResolver configured"); } localeResolver.setLocale(this.request, this.response, locale); this.locale = locale; }
/** * Retrieve the current locale from the given request, using the * LocaleResolver bound to the request by the DispatcherServlet * (if available), falling back to the request's accept-header Locale. * <p>This method serves as a straightforward alternative to the standard * Servlet {@link javax.servlet.http.HttpServletRequest#getLocale()} method, * falling back to the latter if no more specific locale has been found. * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getLocale()} * which will normally be populated with the same Locale. * @param request current HTTP request * @return the current locale for the given request, either from the * LocaleResolver or from the plain request itself * @see #getLocaleResolver * @see org.springframework.context.i18n.LocaleContextHolder#getLocale() */ public static Locale getLocale(HttpServletRequest request) { LocaleResolver localeResolver = getLocaleResolver(request); return (localeResolver != null ? localeResolver.resolveLocale(request) : request.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 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 one, * storing the new locale through the configured {@link LocaleResolver}. * @param locale the new locale * @see LocaleResolver#setLocale * @see #changeLocale(java.util.Locale, java.util.TimeZone) */ public void changeLocale(Locale locale) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request); if (localeResolver == null) { throw new IllegalStateException("Cannot change locale if no LocaleResolver configured"); } localeResolver.setLocale(this.request, this.response, locale); this.locale = locale; }
/** * Retrieve the current time zone from the given request, using the * TimeZoneAwareLocaleResolver bound to the request by the DispatcherServlet * (if available), falling back to the system's default time zone. * <p>Note: This method returns {@code null} if no specific time zone can be * resolved for the given request. This is in contrast to {@link #getLocale} * where there is always the request's accept-header locale to fall back to. * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getTimeZone()} * which will normally be populated with the same TimeZone: That method only * differs in terms of its fallback to the system time zone if the LocaleResolver * hasn't provided a specific time zone (instead of this method's {@code null}). * @param request current HTTP request * @return the current time zone for the given request, either from the * TimeZoneAwareLocaleResolver or {@code null} if none associated * @see #getLocaleResolver * @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone() */ @Nullable public static TimeZone getTimeZone(HttpServletRequest request) { LocaleResolver localeResolver = getLocaleResolver(request); if (localeResolver instanceof LocaleContextResolver) { LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request); if (localeContext instanceof TimeZoneAwareLocaleContext) { return ((TimeZoneAwareLocaleContext) localeContext).getTimeZone(); } } return null; }
/** * Retrieve the current locale from the given request, using the * LocaleResolver bound to the request by the DispatcherServlet * (if available), falling back to the request's accept-header Locale. * <p>This method serves as a straightforward alternative to the standard * Servlet {@link javax.servlet.http.HttpServletRequest#getLocale()} method, * falling back to the latter if no more specific locale has been found. * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getLocale()} * which will normally be populated with the same Locale. * @param request current HTTP request * @return the current locale for the given request, either from the * LocaleResolver or from the plain request itself * @see #getLocaleResolver * @see org.springframework.context.i18n.LocaleContextHolder#getLocale() */ public static Locale getLocale(HttpServletRequest request) { LocaleResolver localeResolver = getLocaleResolver(request); return (localeResolver != null ? localeResolver.resolveLocale(request) : request.getLocale()); }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { String newLocale = request.getParameter(getParamName()); if (newLocale != null) { if (checkHttpMethod(request.getMethod())) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (localeResolver == null) { throw new IllegalStateException( "No LocaleResolver found: not in a DispatcherServlet request?"); } try { localeResolver.setLocale(request, response, parseLocaleValue(newLocale)); } catch (IllegalArgumentException ex) { if (isIgnoreInvalidLocale()) { logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage()); } else { throw ex; } } } } // Proceed in any case. return true; }
@Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!(RequestContextUtils.findWebApplicationContext(request) instanceof SimpleWebApplicationContext)) { throw new ServletException("Incorrect WebApplicationContext"); } if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) { throw new ServletException("Incorrect LocaleResolver"); } if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) { throw new ServletException("Incorrect Locale"); } return null; }
/** * 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; }
/** * Retrieve the current time zone from the given request, using the * TimeZoneAwareLocaleResolver bound to the request by the DispatcherServlet * (if available), falling back to the system's default time zone. * <p>Note: This method returns {@code null} if no specific time zone can be * resolved for the given request. This is in contrast to {@link #getLocale} * where there is always the request's accept-header locale to fall back to. * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getTimeZone()} * which will normally be populated with the same TimeZone: That method only * differs in terms of its fallback to the system time zone if the LocaleResolver * hasn't provided a specific time zone (instead of this method's {@code null}). * @param request current HTTP request * @return the current time zone for the given request, either from the * TimeZoneAwareLocaleResolver or {@code null} if none associated * @see #getLocaleResolver * @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone() */ @Nullable public static TimeZone getTimeZone(HttpServletRequest request) { LocaleResolver localeResolver = getLocaleResolver(request); if (localeResolver instanceof LocaleContextResolver) { LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request); if (localeContext instanceof TimeZoneAwareLocaleContext) { return ((TimeZoneAwareLocaleContext) localeContext).getTimeZone(); } } return null; }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { String newLocale = request.getParameter(getParamName()); if (newLocale != null) { if (checkHttpMethod(request.getMethod())) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (localeResolver == null) { throw new IllegalStateException( "No LocaleResolver found: not in a DispatcherServlet request?"); } try { localeResolver.setLocale(request, response, parseLocaleValue(newLocale)); } catch (IllegalArgumentException ex) { if (isIgnoreInvalidLocale()) { logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage()); } else { throw ex; } } } } // Proceed in any case. return true; }
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (localeResolver instanceof LocaleContextResolver) { LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
throw new RuntimeException("servlet"); if (!(RequestContextUtils.getLocaleResolver(request) instanceof SessionLocaleResolver)) { throw new ServletException("Incorrect LocaleResolver");
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (localeResolver instanceof LocaleContextResolver) { LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
@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(); }
/** * 修改语言 * * @param lang * @param returnUrl * @param request * @param response * @return view name */ @RequestMapping("changeLocale") public String changeLocale(String lang, String returnUrl, HttpServletRequest request, HttpServletResponse response) { if (null != lang) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (null != localeResolver) { localeResolver.setLocale(request, response, StringUtils.parseLocaleString(lang)); } } if (CommonUtils.empty(returnUrl)) { return CommonConstants.TEMPLATE_DONEANDREFRESH; } else { SysSite site = getSite(request); if (isUnSafeUrl(returnUrl, site, request)) { returnUrl = site.getDynamicPath(); } return UrlBasedViewResolver.REDIRECT_URL_PREFIX + returnUrl; } } }
/** * 修改语言 * * @param lang * @param returnUrl * @param request * @param response * @return view name */ @RequestMapping("changeLocale") public String changeLocale(String lang, String returnUrl, HttpServletRequest request, HttpServletResponse response) { if (null != lang) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (null != localeResolver) { localeResolver.setLocale(request, response, StringUtils.parseLocaleString(lang)); } } if (CommonUtils.empty(returnUrl)) { return CommonConstants.TEMPLATE_DONEANDREFRESH; } else { SysSite site = getSite(request); if (isUnSafeUrl(returnUrl, site, request)) { returnUrl = site.getDynamicPath(); } return UrlBasedViewResolver.REDIRECT_URL_PREFIX + returnUrl; } } }
/** * Change the current locale to the specified one, * storing the new locale through the configured {@link LocaleResolver}. * @param locale the new locale * @see LocaleResolver#setLocale * @see #changeLocale(Locale, TimeZone) */ public void changeLocale(Locale locale) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request); if (localeResolver == null) { throw new IllegalStateException("Cannot change locale if no LocaleResolver configured"); } localeResolver.setLocale(this.request, this.response, locale); this.locale = locale; }
/** * Change the current locale to the specified one, * storing the new locale through the configured {@link LocaleResolver}. * @param locale the new locale * @see LocaleResolver#setLocale * @see #changeLocale(java.util.Locale, java.util.TimeZone) */ public void changeLocale(Locale locale) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request); if (localeResolver == null) { throw new IllegalStateException("Cannot change locale if no LocaleResolver configured"); } localeResolver.setLocale(this.request, this.response, locale); this.locale = locale; }
@RequestMapping public String index(HttpServletRequest request) { String locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request).toLanguageTag(); String greeting = new StringBuilder().append(greetingService.getGreeting(locale)). append(" ").append(nameService.getName()).toString(); LOG.info("Greeting: " + greeting); LOG.info("Locale: " + locale); return greeting; } }