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

The following examples show how to use org.springframework.web.servlet.LocaleResolver#resolveLocale() . 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: StringUtils.java    From easyweb with Apache License 2.0 5 votes vote down vote up
/**
 * 获得i18n字符串
 */
public static String getMessage(String code, Object[] args) {
	LocaleResolver localLocaleResolver = (LocaleResolver) SpringContextHolder.getBean(LocaleResolver.class);
	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	Locale localLocale = localLocaleResolver.resolveLocale(request);
	return SpringContextHolder.getApplicationContext().getMessage(code, args, localLocale);
}
 
Example 3
Source File: StringUtils.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
/**
 * 获得i18n字符串
 */
public static String getMessage(String code, Object[] args) {
	LocaleResolver localLocaleResolver = (LocaleResolver) SpringContextHolder.getBean(LocaleResolver.class);
	HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
	Locale localLocale = localLocaleResolver.resolveLocale(request);
	return SpringContextHolder.getApplicationContext().getMessage(code, args, localLocale);
}
 
Example 4
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 5
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 6
Source File: SpringLocaleProvider.java    From spring-soy-view with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Locale> resolveLocale(final HttpServletRequest request) {
    final LocaleResolver localeResolver = (LocaleResolver) request.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE);
    if (localeResolver != null) {
        final Locale locale = localeResolver.resolveLocale(request);
        if (locale != null) {
            return Optional.of(locale);
        }
    }

    return Optional.fromNullable(request.getLocale());
}
 
Example 7
Source File: RequestContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
 * <p>This works with all View implementations. It will typically be used by View implementations.
 * <p>If a ServletContext is specified, the RequestContext will also work with a root
 * WebApplicationContext (outside a DispatcherServlet).
 * @param request current HTTP request
 * @param response current HTTP response
 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
 * fallback to root WebApplicationContext)
 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
 * for Errors retrieval)
 * @see org.springframework.web.context.WebApplicationContext
 * @see org.springframework.web.servlet.DispatcherServlet
 */
public RequestContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable ServletContext servletContext, @Nullable Map<String, Object> model) {

	this.request = request;
	this.response = response;
	this.model = model;

	// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
	// ServletContext needs to be specified to be able to fall back to the root context!
	WebApplicationContext wac = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (wac == null) {
		wac = RequestContextUtils.findWebApplicationContext(request, servletContext);
		if (wac == null) {
			throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
					"request and no ContextLoaderListener registered?");
		}
	}
	this.webApplicationContext = wac;

	Locale locale = null;
	TimeZone timeZone = null;

	// Determine locale to use for this RequestContext.
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver instanceof LocaleContextResolver) {
		LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	else if (localeResolver != null) {
		// Try LocaleResolver (we're within a DispatcherServlet request).
		locale = localeResolver.resolveLocale(request);
	}

	this.locale = locale;
	this.timeZone = timeZone;

	// Determine default HTML escape setting from the "defaultHtmlEscape"
	// context-param in web.xml, if any.
	this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());

	// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
	// context-param in web.xml, if any.
	this.responseEncodedHtmlEscape =
			WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());

	this.urlPathHelper = new UrlPathHelper();

	if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		this.requestDataValueProcessor = this.webApplicationContext.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
	}
}
 
Example 8
Source File: SpringUtils.java    From mogu_blog_v2 with Apache License 2.0 4 votes vote down vote up
public static String getMessage(String code, Object... args) {
    LocaleResolver localeResolver = getBean(LocaleResolver.class);
    Locale locale = localeResolver.resolveLocale(getCurrentReq());
    return applicationContext.getMessage(code, args, locale);
}
 
Example 9
Source File: RequestContext.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
 * <p>This works with all View implementations. It will typically be used by View implementations.
 * <p>If a ServletContext is specified, the RequestContext will also work with a root
 * WebApplicationContext (outside a DispatcherServlet).
 * @param request current HTTP request
 * @param response current HTTP response
 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
 * fallback to root WebApplicationContext)
 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
 * for Errors retrieval)
 * @see org.springframework.web.context.WebApplicationContext
 * @see org.springframework.web.servlet.DispatcherServlet
 */
public RequestContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable ServletContext servletContext, @Nullable Map<String, Object> model) {

	this.request = request;
	this.response = response;
	this.model = model;

	// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
	// ServletContext needs to be specified to be able to fall back to the root context!
	WebApplicationContext wac = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (wac == null) {
		wac = RequestContextUtils.findWebApplicationContext(request, servletContext);
		if (wac == null) {
			throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
					"request and no ContextLoaderListener registered?");
		}
	}
	this.webApplicationContext = wac;

	Locale locale = null;
	TimeZone timeZone = null;

	// Determine locale to use for this RequestContext.
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver instanceof LocaleContextResolver) {
		LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	else if (localeResolver != null) {
		// Try LocaleResolver (we're within a DispatcherServlet request).
		locale = localeResolver.resolveLocale(request);
	}

	this.locale = locale;
	this.timeZone = timeZone;

	// Determine default HTML escape setting from the "defaultHtmlEscape"
	// context-param in web.xml, if any.
	this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());

	// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
	// context-param in web.xml, if any.
	this.responseEncodedHtmlEscape =
			WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());

	this.urlPathHelper = new UrlPathHelper();

	if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		this.requestDataValueProcessor = this.webApplicationContext.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
	}
}
 
Example 10
Source File: RequestContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Initialize this context with the given request, using the given model attributes for Errors retrieval.
 * <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
 * locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
 * @param request current HTTP request
 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
 * fallback to root WebApplicationContext)
 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
 * for Errors retrieval)
 * @see #getFallbackLocale
 * @see #getFallbackTheme
 * @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
 * @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
 */
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext,
		Map<String, Object> model) {

	this.request = request;
	this.response = response;
	this.model = model;

	// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
	// ServletContext needs to be specified to be able to fall back to the root context!
	this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (this.webApplicationContext == null) {
		this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
		if (this.webApplicationContext == null) {
			throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
					"request and no ContextLoaderListener registered?");
		}
	}

	// Determine locale to use for this RequestContext.
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver instanceof LocaleContextResolver) {
		LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
		this.locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	else if (localeResolver != null) {
		// Try LocaleResolver (we're within a DispatcherServlet request).
		this.locale = localeResolver.resolveLocale(request);
	}

	// Try JSTL fallbacks if necessary.
	if (this.locale == null) {
		this.locale = getFallbackLocale();
	}
	if (this.timeZone == null) {
		this.timeZone = getFallbackTimeZone();
	}

	// Determine default HTML escape setting from the "defaultHtmlEscape"
	// context-param in web.xml, if any.
	this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());

	// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
	// context-param in web.xml, if any.
	this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());

	this.urlPathHelper = new UrlPathHelper();

	if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		this.requestDataValueProcessor = this.webApplicationContext.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
	}
}
 
Example 11
Source File: RequestContext.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize this context with the given request, using the given model attributes for Errors retrieval.
 * <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
 * locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
 * @param request current HTTP request
 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
 * fallback to root WebApplicationContext)
 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
 * for Errors retrieval)
 * @see #getFallbackLocale
 * @see #getFallbackTheme
 * @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
 * @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
 */
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext,
		Map<String, Object> model) {

	this.request = request;
	this.response = response;
	this.model = model;

	// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
	// ServletContext needs to be specified to be able to fall back to the root context!
	this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (this.webApplicationContext == null) {
		this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
		if (this.webApplicationContext == null) {
			throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
					"request and no ContextLoaderListener registered?");
		}
	}

	// Determine locale to use for this RequestContext.
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver instanceof LocaleContextResolver) {
		LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
		this.locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	else if (localeResolver != null) {
		// Try LocaleResolver (we're within a DispatcherServlet request).
		this.locale = localeResolver.resolveLocale(request);
	}

	// Try JSTL fallbacks if necessary.
	if (this.locale == null) {
		this.locale = getFallbackLocale();
	}
	if (this.timeZone == null) {
		this.timeZone = getFallbackTimeZone();
	}

	// Determine default HTML escape setting from the "defaultHtmlEscape"
	// context-param in web.xml, if any.
	this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());

	// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
	// context-param in web.xml, if any.
	this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());

	this.urlPathHelper = new UrlPathHelper();

	if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		this.requestDataValueProcessor = this.webApplicationContext.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
	}
}
 
Example 12
Source File: RequestContextUtils.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Retrieve the current locale from the given request, using the
 * LocaleResolver bound to the request by the DispatcherServlet
 * (if available), falling back to the request's accept-header Locale.
 * <p>This method serves as a straightforward alternative to the standard
 * Servlet {@link javax.servlet.http.HttpServletRequest#getLocale()} method,
 * falling back to the latter if no more specific locale has been found.
 * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getLocale()}
 * which will normally be populated with the same Locale.
 * @param request current HTTP request
 * @return the current locale for the given request, either from the
 * LocaleResolver or from the plain request itself
 * @see #getLocaleResolver
 * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
 */
public static Locale getLocale(HttpServletRequest request) {
	LocaleResolver localeResolver = getLocaleResolver(request);
	return (localeResolver != null ? localeResolver.resolveLocale(request) : request.getLocale());
}
 
Example 13
Source File: RequestContextUtils.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Retrieve the current locale from the given request, using the
 * LocaleResolver bound to the request by the DispatcherServlet
 * (if available), falling back to the request's accept-header Locale.
 * <p>This method serves as a straightforward alternative to the standard
 * Servlet {@link javax.servlet.http.HttpServletRequest#getLocale()} method,
 * falling back to the latter if no more specific locale has been found.
 * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getLocale()}
 * which will normally be populated with the same Locale.
 * @param request current HTTP request
 * @return the current locale for the given request, either from the
 * LocaleResolver or from the plain request itself
 * @see #getLocaleResolver
 * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
 */
public static Locale getLocale(HttpServletRequest request) {
	LocaleResolver localeResolver = getLocaleResolver(request);
	return (localeResolver != null ? localeResolver.resolveLocale(request) : request.getLocale());
}
 
Example 14
Source File: RequestContextUtils.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Retrieve the current locale from the given request, using the
 * LocaleResolver bound to the request by the DispatcherServlet
 * (if available), falling back to the request's accept-header Locale.
 * <p>This method serves as a straightforward alternative to the standard
 * Servlet {@link javax.servlet.http.HttpServletRequest#getLocale()} method,
 * falling back to the latter if no more specific locale has been found.
 * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getLocale()}
 * which will normally be populated with the same Locale.
 * @param request current HTTP request
 * @return the current locale for the given request, either from the
 * LocaleResolver or from the plain request itself
 * @see #getLocaleResolver
 * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
 */
public static Locale getLocale(HttpServletRequest request) {
	LocaleResolver localeResolver = getLocaleResolver(request);
	return (localeResolver != null ? localeResolver.resolveLocale(request) : request.getLocale());
}
 
Example 15
Source File: RequestContextUtils.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieve the current locale from the given request, using the
 * LocaleResolver bound to the request by the DispatcherServlet
 * (if available), falling back to the request's accept-header Locale.
 * <p>This method serves as a straightforward alternative to the standard
 * Servlet {@link javax.servlet.http.HttpServletRequest#getLocale()} method,
 * falling back to the latter if no more specific locale has been found.
 * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getLocale()}
 * which will normally be populated with the same Locale.
 * @param request current HTTP request
 * @return the current locale for the given request, either from the
 * LocaleResolver or from the plain request itself
 * @see #getLocaleResolver
 * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
 */
public static Locale getLocale(HttpServletRequest request) {
	LocaleResolver localeResolver = getLocaleResolver(request);
	return (localeResolver != null ? localeResolver.resolveLocale(request) : request.getLocale());
}