@After
 public void close() {
   LocaleContextHolder.resetLocaleContext();
   if (this.context != null) {
     this.context.close();
   }
 }
 protected void service(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   final Locale locale = localeResolver.resolveLocale(request);
   LocaleContextHolder.setLocale(locale);
   ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
   RequestContextHolder.setRequestAttributes(requestAttributes);
   try {
     super.service(
         new HttpServletRequestWrapper(request) {
           @Override
           public Locale getLocale() {
             return locale;
           }
         },
         response);
   } finally {
     if (!locale.equals(LocaleContextHolder.getLocale())) {
       if (logger.isDebugEnabled()) {
         logger.debug("locale changed, updating locale resolver");
       }
       localeResolver.setLocale(request, response, LocaleContextHolder.getLocale());
     }
     LocaleContextHolder.resetLocaleContext();
     RequestContextHolder.resetRequestAttributes();
   }
 }
示例#3
0
 /**
  * Set the locale of the current request (if there is one) into Spring's LocaleContextHolder.
  *
  * @param code The code to execute while the locale is set
  * @return the result of the code block
  */
 private static <T> T withRequestLocale(Supplier<T> code) {
   try {
     LocaleContextHolder.setLocale(Http.Context.current().lang().toLocale());
   } catch (Exception e) {
     // Just continue (Maybe there is no context or some internal error in LocaleContextHolder).
     // System default locale will be used.
   }
   try {
     return code.get();
   } finally {
     LocaleContextHolder.resetLocaleContext(); // Clean up ThreadLocal
   }
 }
 private void resetContextHolders() {
   LocaleContextHolder.resetLocaleContext();
   RequestContextHolder.resetRequestAttributes();
 }