Java Code Examples for org.springframework.web.servlet.support.RequestContextUtils#getLocaleResolver()

The following examples show how to use org.springframework.web.servlet.support.RequestContextUtils#getLocaleResolver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageResolver.java    From Lottery with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 获得国际化信息
 * 
 * @param request
 *            HttpServletRequest
 * @param code
 *            国际化代码
 * @param args
 *            替换参数
 * @return
 * @see org.springframework.context.MessageSource#getMessage(String,
 *      Object[], Locale)
 */
public static String getMessage(HttpServletRequest request, String code,
		Object... args) {
	WebApplicationContext messageSource = RequestContextUtils
			.getWebApplicationContext(request);
	if (messageSource == null) {
		throw new IllegalStateException("WebApplicationContext not found!");
	}
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	Locale locale;
	if (localeResolver != null) {
		locale = localeResolver.resolveLocale(request);
	} else {
		locale = request.getLocale();
	}
	return messageSource.getMessage(code, args, locale);
}
 
Example 2
Source File: FrontLocaleInterceptor.java    From Lottery with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request,
		HttpServletResponse response, Object handler)
		throws ServletException {
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	if (localeResolver == null) {
		throw new IllegalStateException(
				"No LocaleResolver found: not in a DispatcherServlet request?");
	}
	CmsSite site = CmsUtils.getSite(request);
	String newLocale = site.getLocaleFront();
	LocaleEditor localeEditor = new LocaleEditor();
	localeEditor.setAsText(newLocale);
	localeResolver.setLocale(request, response, (Locale) localeEditor
			.getValue());
	// Proceed in any case.
	return true;
}
 
Example 3
Source File: AdminLocaleInterceptor.java    From Lottery with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request,
		HttpServletResponse response, Object handler)
		throws ServletException {
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	if (localeResolver == null) {
		throw new IllegalStateException(
				"No LocaleResolver found: not in a DispatcherServlet request?");
	}
	CmsSite site = CmsUtils.getSite(request);
	String newLocale = site.getLocaleAdmin();
	LocaleEditor localeEditor = new LocaleEditor();
	localeEditor.setAsText(newLocale);
	localeResolver.setLocale(request, response, (Locale) localeEditor
			.getValue());
	// Proceed in any case.
	return true;
}
 
Example 4
Source File: WebErrors.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 通过HttpServletRequest创建WebErrors
 * 
 * @param request
 *            从request中获得MessageSource和Locale,如果存在的话。
 */
public WebErrors(HttpServletRequest request) {
	WebApplicationContext webApplicationContext = RequestContextUtils
			.getWebApplicationContext(request);
	if (webApplicationContext != null) {
		LocaleResolver localeResolver = RequestContextUtils
				.getLocaleResolver(request);
		Locale locale;
		if (localeResolver != null) {
			locale = localeResolver.resolveLocale(request);
			this.messageSource = webApplicationContext;
			this.locale = locale;
		}
	}
}
 
Example 5
Source File: LocaleChangeInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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()) {
					if (logger.isDebugEnabled()) {
						logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage());
					}
				}
				else {
					throw ex;
				}
			}
		}
	}
	// Proceed in any case.
	return true;
}
 
Example 6
Source File: FrontLocaleInterceptor.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void postHandle(HttpServletRequest request,
		HttpServletResponse response, Object handler,
		ModelAndView modelAndView) throws Exception {
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	if (localeResolver == null) {
		throw new IllegalStateException(
				"No LocaleResolver found: not in a DispatcherServlet request?");
	}
	if (modelAndView != null) {
		modelAndView.getModelMap().addAttribute(LOCALE,
				localeResolver.resolveLocale(request).toString());
	}
}
 
Example 7
Source File: AdminLocaleInterceptor.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void postHandle(HttpServletRequest request,
		HttpServletResponse response, Object handler,
		ModelAndView modelAndView) throws Exception {
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	if (localeResolver == null) {
		throw new IllegalStateException(
				"No LocaleResolver found: not in a DispatcherServlet request?");
	}
	if (modelAndView != null) {
		modelAndView.getModelMap().addAttribute(LOCALE,
				localeResolver.resolveLocale(request).toString());
	}
}
 
Example 8
Source File: LocaleUtils.java    From voj with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 根据用户语言设置Locale信息.
 * @param request - HttpRequest对象
 * @param response - HttpResponse对象
 * @param language - 语言的名称(例如zh_CN)
 */
public static void setLocale(HttpServletRequest request, HttpServletResponse response, String language) {
	Locale locale = LocaleUtils.getLocaleOfLanguage(language);
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	localeResolver.setLocale(request, response, locale);
	request.getSession().setAttribute("language", language);
}
 
Example 9
Source File: PageListWrapper.java    From Asqatasun with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This method resolves the language through the request.
 *
 * @param request
 * @return
 */
private ResourceBundle getResourceBundle(ServletRequest request) {
    ResourceBundle resourceBundle = ResourceBundle.getBundle(I18N_FILE_KEY);;
    if (request instanceof HttpServletRequest) {
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver((HttpServletRequest) request);

        if (localeResolver != null) {
            // get current locale
            Locale locale = localeResolver.resolveLocale((HttpServletRequest) request);
            resourceBundle = ResourceBundle.getBundle(I18N_FILE_KEY, locale);
        }
    }
    return resourceBundle;
}
 
Example 10
Source File: SimpleWebApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	if (!(RequestContextUtils.getWebApplicationContext(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;
}
 
Example 11
Source File: LocaleChangeInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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, StringUtils.parseLocaleString(newLocale));
			}
			catch (IllegalArgumentException ex) {
				if (isIgnoreInvalidLocale()) {
					logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage());
				}
				else {
					throw ex;
				}
			}
		}
	}
	// Proceed in any case.
	return true;
}
 
Example 12
Source File: LocaleChangeInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 13
Source File: IndexAction.java    From albert with MIT License 5 votes vote down vote up
/**
 * 国家化语言
 *
 * @param modelMap
 * @return
 * @throws IOException 
 */
@ResponseBody
@RequestMapping(value = "/changeLanguage", method = {RequestMethod.POST,RequestMethod.GET})
public Map changeLanguage( 
		@RequestParam(
				value="language", required=false) String language,
				HttpServletRequest request,
				HttpServletResponse reponse,
				ModelMap modelMap)  {
	Map<String,Object> map = new HashMap<String,Object>();
	map.put("msg","");
	map.put("res","false");
	try {
		request.getSession().setAttribute(Constants.SYS_LANGUAGE_SESSION_KEY, language);
		LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
		if (localeResolver == null) {
			log.debug("No LocaleResolver found: not in a DispatcherServlet request?");
		}
		LocaleEditor localeEditor = new LocaleEditor();
		localeEditor.setAsText(language);
		localeResolver.setLocale(request, reponse, (Locale) localeEditor.getValue());
		map.put("res", "true");
	}
	catch(Exception e){
		log.debug("修改系统语言 /changeLanguage.do", e); 
	}
	return map;  
}
 
Example 14
Source File: SimpleWebApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 15
Source File: LocaleChangeInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 16
Source File: LanguageController.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 设置语言
 * @param request
 * @param response
 * @param lang
 * @return
 */
@GetMapping("/setLang")
public FwResult getInfoByLang(HttpServletRequest request, HttpServletResponse response,
                              String lang){
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    if("zh".equals(lang)){
        localeResolver.setLocale(request, response, new Locale("zh", "CN"));
    }else if("en".equals(lang)){
        localeResolver.setLocale(request, response, new Locale("en", "US"));
    }
    return FwResult.okMsg("设置"+lang+"成功");
}
 
Example 17
Source File: I18nMessages.java    From Milkomeda with MIT License 5 votes vote down vote up
/**
 * 设置固定的Locale
 * @param locale    Locale
 */
public void setFixedLocale(Locale locale) {
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(WebContext.getRequest());
    if (localeResolver == null) {
        throw new IllegalStateException(
                "No LocaleResolver found: not in a DispatcherServlet request?");
    }
    localeResolver.setLocale(WebContext.getRequest(), WebContext.getRequestAttributes().getResponse(), locale);
}
 
Example 18
Source File: SimpleWebApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 19
Source File: ComplexWebApplicationContext.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException {
	WebApplicationContext wac = RequestContextUtils.getWebApplicationContext(request);
	if (!(wac instanceof ComplexWebApplicationContext)) {
		throw new ServletException("Incorrect WebApplicationContext");
	}
	if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) {
		throw new ServletException("Not in a MultipartHttpServletRequest");
	}
	if (request.getParameter("fail") != null) {
		throw new ModelAndViewDefiningException(new ModelAndView("failed1"));
	}
	if (request.getParameter("access") != null) {
		throw new IllegalAccessException("illegal access");
	}
	if (request.getParameter("servlet") != null) {
		throw new ServletRequestBindingException("servlet");
	}
	if (request.getParameter("exception") != null) {
		throw new RuntimeException("servlet");
	}
	if (!(RequestContextUtils.getLocaleResolver(request) instanceof SessionLocaleResolver)) {
		throw new ServletException("Incorrect LocaleResolver");
	}
	if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	if (!Locale.CANADA.equals(LocaleContextHolder.getLocale())) {
		throw new ServletException("Incorrect Locale");
	}
	if (RequestContextUtils.getTimeZone(request) != null) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!TimeZone.getDefault().equals(LocaleContextHolder.getTimeZone())) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!(RequestContextUtils.getThemeResolver(request) instanceof SessionThemeResolver)) {
		throw new ServletException("Incorrect ThemeResolver");
	}
	if (!"theme".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) {
		throw new ServletException("Incorrect theme name");
	}
	RequestContext rc = new RequestContext(request);
	rc.changeLocale(Locale.US, TimeZone.getTimeZone("GMT+1"));
	rc.changeTheme("theme2");
	if (!Locale.US.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	if (!Locale.US.equals(LocaleContextHolder.getLocale())) {
		throw new ServletException("Incorrect Locale");
	}
	if (!TimeZone.getTimeZone("GMT+1").equals(RequestContextUtils.getTimeZone(request))) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!TimeZone.getTimeZone("GMT+1").equals(LocaleContextHolder.getTimeZone())) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!"theme2".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) {
		throw new ServletException("Incorrect theme name");
	}
}
 
Example 20
Source File: ComplexWebApplicationContext.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException {
	WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request);
	if (!(wac instanceof ComplexWebApplicationContext)) {
		throw new ServletException("Incorrect WebApplicationContext");
	}
	if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) {
		throw new ServletException("Not in a MultipartHttpServletRequest");
	}
	if (request.getParameter("fail") != null) {
		throw new ModelAndViewDefiningException(new ModelAndView("failed1"));
	}
	if (request.getParameter("access") != null) {
		throw new IllegalAccessException("illegal access");
	}
	if (request.getParameter("servlet") != null) {
		throw new ServletRequestBindingException("servlet");
	}
	if (request.getParameter("exception") != null) {
		throw new RuntimeException("servlet");
	}
	if (!(RequestContextUtils.getLocaleResolver(request) instanceof SessionLocaleResolver)) {
		throw new ServletException("Incorrect LocaleResolver");
	}
	if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	if (!Locale.CANADA.equals(LocaleContextHolder.getLocale())) {
		throw new ServletException("Incorrect Locale");
	}
	if (RequestContextUtils.getTimeZone(request) != null) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!TimeZone.getDefault().equals(LocaleContextHolder.getTimeZone())) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!(RequestContextUtils.getThemeResolver(request) instanceof SessionThemeResolver)) {
		throw new ServletException("Incorrect ThemeResolver");
	}
	if (!"theme".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) {
		throw new ServletException("Incorrect theme name");
	}
	RequestContext rc = new RequestContext(request);
	rc.changeLocale(Locale.US, TimeZone.getTimeZone("GMT+1"));
	rc.changeTheme("theme2");
	if (!Locale.US.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	if (!Locale.US.equals(LocaleContextHolder.getLocale())) {
		throw new ServletException("Incorrect Locale");
	}
	if (!TimeZone.getTimeZone("GMT+1").equals(RequestContextUtils.getTimeZone(request))) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!TimeZone.getTimeZone("GMT+1").equals(LocaleContextHolder.getTimeZone())) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!"theme2".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) {
		throw new ServletException("Incorrect theme name");
	}
}