Java Code Examples for org.springframework.web.servlet.LocaleResolver#setLocale()

The following examples show how to use org.springframework.web.servlet.LocaleResolver#setLocale() . 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: 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 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: RequestContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 4
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 5
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 6
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 7
Source File: RequestContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
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 9
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 10
Source File: RequestContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
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 12
Source File: RequestContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 13
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 14
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);
}