@RequestMapping(value = "/changeLang", method = RequestMethod.GET)
 public String changeLocale(
     @RequestParam String language, HttpServletRequest req, HttpServletResponse resp) {
   LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(req);
   localeResolver.setLocale(req, resp, StringUtils.parseLocaleString(language));
   return "redirect:/";
 }
 public static java.util.Locale convertLocaleToJavaLocale(Locale broadleafLocale) {
   if (broadleafLocale != null) {
     String localeString = broadleafLocale.getLocaleCode();
     return org.springframework.util.StringUtils.parseLocaleString(localeString);
   }
   return null;
 }
  private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
      // Retrieve and parse cookie value.
      Cookie cookie = WebUtils.getCookie(request, getCookieName());
      Locale locale = null;
      TimeZone timeZone = null;
      if (cookie != null) {
        String value = cookie.getValue();

        // Remove the double quote
        value = StringUtils.replace(value, "%22", "");

        String localePart = value;
        String timeZonePart = null;
        int spaceIndex = localePart.indexOf(' ');
        if (spaceIndex != -1) {
          localePart = value.substring(0, spaceIndex);
          timeZonePart = value.substring(spaceIndex + 1);
        }
        locale =
            (!"-".equals(localePart)
                ? StringUtils.parseLocaleString(localePart.replace('-', '_'))
                : null);
        if (timeZonePart != null) {
          timeZone = StringUtils.parseTimeZoneString(timeZonePart);
        }
        if (logger.isTraceEnabled()) {
          logger.trace(
              "Parsed cookie value ["
                  + cookie.getValue()
                  + "] into locale '"
                  + locale
                  + "'"
                  + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
        }
      }
      request.setAttribute(
          LOCALE_REQUEST_ATTRIBUTE_NAME,
          (locale != null ? locale : determineDefaultLocale(request)));

      request.setAttribute(
          TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
          (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
    }
  }
 /** {@inheritDoc} */
 @Override
 public boolean preHandle(
     final HttpServletRequest request, final HttpServletResponse response, final Object handler)
     throws ServletException {
   String newLocale = request.getParameter(this.paramName);
   if (newLocale != null) {
     final LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
     if (localeResolver == null) {
       throw new IllegalStateException(
           "No LocaleResolver found: not in a DispatcherServlet request?");
     }
     if (!acceptedLocales.contains(newLocale)) {
       newLocale = getDefaultLocale();
     }
     if (!request.getLocale().getLanguage().equals(new Locale(newLocale).getLanguage()))
       LOGGER.info("Switching to new Locale: " + newLocale);
     localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
   }
   // Proceed in any case.
   return true;
 }
Example #5
0
 public SecurityUser(User user) {
   this.id = user.getId();
   this.password = user.getPassword();
   this.username = user.getUsername();
   this.firstName = user.getFirstName();
   this.lastName = user.getLastName();
   if (user.getCompany() != null) {
     this.company = user.getCompany().getName();
   }
   this.language =
       (null == user.getLanguage()
           ? Locale.US
           : StringUtils.parseLocaleString(user.getLanguage()));
   final long createAtTime = user.getCreatedAt().getTime();
   this.createTime = (createAtTime - (createAtTime % StringDefs.TIME_CORRECT));
   this.accountNonExpired = true;
   this.accountNonLocked = true;
   this.credentialsNonExpired = true;
   this.enabled = user.getActive();
   this.authorities = getAuthorities(user.getRoles());
 }
  public Locale resolveLocale(HttpServletRequest request) {
    // Check request for pre-parsed or preset locale.
    Locale locale = (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
    if (locale != null) {
      return locale;
    }

    // Retrieve and parse cookie value.
    Cookie cookie = WebUtils.getCookie(request, getCookieName());
    if (cookie != null) {
      locale = StringUtils.parseLocaleString(cookie.getValue());
      if (logger.isDebugEnabled()) {
        logger.debug(
            "Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale + "'");
      }
      if (locale != null) {
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale);
        return locale;
      }
    }

    return determineDefaultLocale(request);
  }
Example #7
0
 /**
  * Converts the given locale to a <code>xml:lang</code> string, as used in Axiom Faults.
  *
  * @param language the language string
  * @return the locale
  */
 public static Locale toLocale(String language) {
   language = language.replace('-', '_');
   return StringUtils.parseLocaleString(language);
 }
 /**
  * Override the default {@link Locale} to use for formatting. Default value used is {@link
  * Locale#getDefault()}.
  *
  * @param localeString the locale ID as a String representation
  * @since 4.1.5
  */
 public Jackson2ObjectMapperBuilder locale(String localeString) {
   this.locale = StringUtils.parseLocaleString(localeString);
   return this;
 }