Example usage for org.springframework.web.servlet LocaleResolver setLocale

List of usage examples for org.springframework.web.servlet LocaleResolver setLocale

Introduction

In this page you can find the example usage for org.springframework.web.servlet LocaleResolver setLocale.

Prototype

void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale);

Source Link

Document

Set the current locale to the given one.

Usage

From source file:org.ng200.openolympus.controller.LanguageSwitchController.java

@RequestMapping(value = "/locale", method = RequestMethod.GET)
public String switchLanguage(final HttpServletRequest request, final HttpServletResponse response,
        @RequestParam("name") final String name) {
    Assertions.resourceExists(name);// w w  w  . j  a v  a  2 s .  c  o  m

    final LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    localeResolver.setLocale(request, response, StringUtils.parseLocaleString(name));
    return "redirect:/";
}

From source file:org.jamwiki.servlets.JAMWikiLocaleInterceptor.java

/**
 * Ensure that the session locale value is set.
 *//*from ww  w. j a v  a  2s  .  c  o m*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws ServletException {
    Locale locale = (Locale) request.getSession()
            .getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
    if (locale == null) {
        locale = this.setUserLocale(request, response);
    }
    if (locale != null) {
        LocaleResolver resolver = RequestContextUtils.getLocaleResolver(request);
        if (resolver != null) {
            resolver.setLocale(request, response, locale);
        }
    }
    return super.preHandle(request, response, handler);
}

From source file:org.jnap.core.mvc.i18n.CompositeLocaleResolver.java

@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    for (LocaleResolver localeResolver : this.resolvers) {
        try {//w w w .  j  a  va  2  s. c om
            configCookieScope(localeResolver, request);
            localeResolver.setLocale(request, response, locale);
            break;
        } catch (UnsupportedOperationException e) {
            if (throwExceptionIfReadOnly) {
                throw e;
            }
        }
    }
}

From source file:org.iish.visualmets.util.LocaleChangeInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws ServletException {

    String newLocale = request.getParameter(this.paramName);
    if (newLocale != null) {
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if (localeResolver == null) {
            throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
        }/*from  w ww. jav  a2s  .co m*/
        LocaleEditor localeEditor = new LocaleEditor();
        localeEditor.setAsText(newLocale);
        localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
    }

    return true;
}

From source file:eu.europeana.core.util.web.LocaleChangeInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws ServletException, EuropeanaQueryException {
    Language oldLocale = ControllerUtil.getLocale(request);
    String newLocale = request.getParameter(this.paramName);
    if (newLocale != null) {
        if (newLocale.contains("*")) {
            throw new EuropeanaQueryException(QueryProblem.UNABLE_TO_CHANGE_LANGUAGE.toString());
        }/*from www .ja v a  2s  .  c o m*/
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if (localeResolver == null) {
            throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
        }
        LocaleEditor localeEditor = new LocaleEditor();
        localeEditor.setAsText(newLocale);
        localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
        clickStreamLogger.logLanguageChange(request, oldLocale, ClickStreamLogger.UserAction.LANGUAGE_CHANGE);
    }
    // Proceed in any case.
    return true;
}

From source file:com.mtt.myapp.home.controller.HomeController.java

@ResponseStatus(HttpStatus.OK)
@RequestMapping("/set_language")
public void setLanguage(String lan, HttpServletResponse response, HttpServletRequest request) {
    LocaleResolver localeResolver = checkNotNull(RequestContextUtils.getLocaleResolver(request),
            "No LocaleResolver found!");
    LocaleEditor localeEditor = new LocaleEditor();
    String language = StringUtils.defaultIfBlank(lan, config.getDefaultLanguage());
    localeEditor.setAsText(language);/*from w  ww.  j  a  v  a2s  .c  o m*/
    localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
}

From source file:ph.fingra.statisticsweb.controller.CommonController.java

@RequestMapping(method = RequestMethod.POST, value = "/changeLocaleByAjax")
public @ResponseBody String changeLocale(@RequestParam("lang") String lang, HttpServletRequest request,
        HttpServletResponse response) {/* www.  ja  v a 2  s.co  m*/
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    Locale locale = null;
    if (lang.equals("ko")) {
        locale = Locale.KOREA;
    } else if (lang.equals("cn")) {
        locale = Locale.CHINA;
    } else if (lang.equals("hk")) {
        locale = Locale.TRADITIONAL_CHINESE;
    } else if (lang.equals("ja")) {
        locale = Locale.JAPAN;
    } else {
        locale = Locale.ENGLISH;
    }

    localeResolver.setLocale(request, response, locale);

    return null;
}

From source file:org.ngrinder.home.controller.HomeController.java

private void setLanguage(String lan, HttpServletResponse response, HttpServletRequest request) {
    LocaleResolver localeResolver = checkNotNull(RequestContextUtils.getLocaleResolver(request),
            "No LocaleResolver found!");
    LocaleEditor localeEditor = new LocaleEditor();
    String language = StringUtils.defaultIfBlank(lan,
            getConfig().getControllerProperties().getProperty(PROP_CONTROLLER_DEFAULT_LANG));
    localeEditor.setAsText(language);//from  w w w.  j av  a2  s . co m
    localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
}

From source file:alfio.config.MvcConfiguration.java

@Bean
public HandlerInterceptor getEventLocaleSetterInterceptor() {
    return new HandlerInterceptorAdapter() {
        @Override//w  ww.ja  va2 s.co m
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {

            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = ((HandlerMethod) handler);
                RequestMapping reqMapping = handlerMethod.getMethodAnnotation(RequestMapping.class);

                //check if the request mapping value has the form "/event/{something}"
                Pattern eventPattern = Pattern.compile("^/event/\\{(\\w+)}/{0,1}.*");
                if (reqMapping != null && reqMapping.value().length == 1
                        && eventPattern.matcher(reqMapping.value()[0]).matches()) {

                    Matcher m = eventPattern.matcher(reqMapping.value()[0]);
                    m.matches();

                    String pathVariableName = m.group(1);

                    //extract the parameter name
                    Arrays.stream(handlerMethod.getMethodParameters())
                            .map(methodParameter -> methodParameter.getParameterAnnotation(PathVariable.class))
                            .filter(Objects::nonNull).map(PathVariable::value).filter(pathVariableName::equals)
                            .findFirst().ifPresent((val) -> {

                                //fetch the parameter value
                                @SuppressWarnings("unchecked")
                                String eventName = Optional.ofNullable(((Map<String, Object>) request
                                        .getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE))
                                                .get(val))
                                        .orElse("").toString();

                                LocaleResolver resolver = RequestContextUtils.getLocaleResolver(request);
                                Locale locale = resolver.resolveLocale(request);
                                List<ContentLanguage> cl = i18nManager.getEventLanguages(eventName);

                                request.setAttribute("ALFIO_EVENT_NAME", eventName);

                                if (cl.stream().noneMatch(
                                        contentLanguage -> contentLanguage.getLanguage().equals(Optional
                                                .ofNullable(locale).orElse(Locale.ENGLISH).getLanguage()))) {
                                    //override the user locale if it's not in the one permitted by the event
                                    resolver.setLocale(request, response, cl.stream().findFirst()
                                            .map(ContentLanguage::getLocale).orElse(Locale.ENGLISH));
                                } else {
                                    resolver.setLocale(request, response, locale);
                                }
                            });
                }
            }
            return true;
        }
    };
}

From source file:com.krawler.spring.authHandler.authHandlerController.java

protected void setLocale(HttpServletRequest request, HttpServletResponse response, String newLocale) {
    if (newLocale != null) {
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if (localeResolver == null) {
            LOG.debug("No LocaleResolver found: not in a DispatcherServlet request?");
            return;
        }//from www. jav a 2s  . c om
        LocaleEditor localeEditor = new LocaleEditor();
        localeEditor.setAsText(newLocale);
        localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
    }
}