org.springframework.web.servlet.LocaleResolver Java Examples

The following examples show how to use org.springframework.web.servlet.LocaleResolver. 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: 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 #2
Source File: AbstractTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected MockPageContext createPageContext() {
	MockServletContext sc = new MockServletContext();
	SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
	wac.setServletContext(sc);
	wac.setNamespace("test");
	wac.refresh();

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	MockHttpServletResponse response = new MockHttpServletResponse();
	if (inDispatcherServlet()) {
		request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
		LocaleResolver lr = new AcceptHeaderLocaleResolver();
		request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
		ThemeResolver tr = new FixedThemeResolver();
		request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
		request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
	}
	else {
		sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	}

	return new MockPageContext(sc, request, response);
}
 
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: 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 #5
Source File: ContractControllerTest.java    From Asqatasun with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setUpLocaleResolver() {
    mockLocaleResolver = createMock(LocaleResolver.class);
    expect(mockLocaleResolver.resolveLocale(null)).andReturn(Locale.FRANCE).anyTimes();
    replay(mockLocaleResolver);

    instance.setLocaleResolver(mockLocaleResolver);
}
 
Example #6
Source File: RequestContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Change the current locale to the specified locale and time zone context,
 * storing the new locale context through the configured {@link LocaleResolver}.
 * @param locale the new locale
 * @param timeZone the new time zone
 * @see LocaleContextResolver#setLocaleContext
 * @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
 */
public void changeLocale(Locale locale, TimeZone timeZone) {
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
	if (!(localeResolver instanceof LocaleContextResolver)) {
		throw new IllegalStateException("Cannot change locale context if no LocaleContextResolver configured");
	}
	((LocaleContextResolver) localeResolver).setLocaleContext(this.request, this.response,
			new SimpleTimeZoneAwareLocaleContext(locale, timeZone));
	this.locale = locale;
	this.timeZone = timeZone;
}
 
Example #7
Source File: BaseApplicationConfig.java    From spring-boot-doma2-sample with Apache License 2.0 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    // Cookieに言語を保存する
    val resolver = new CookieLocaleResolver();
    resolver.setCookieName("lang");
    return resolver;
}
 
Example #8
Source File: WebMvcAutoConfiguration.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 国际化设置
 *
 * @return LocaleResolver
 */
@Bean
public LocaleResolver localeResolver() {
    final SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.CHINA);
    return slr;
}
 
Example #9
Source File: UserAccountController.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
public UserAccountController(
    UserAccountService userAccountService,
    RecoveryService recoveryService,
    TwoFactorAuthenticationService twoFactorAuthenticationService,
    AuthenticationSettings authenticationSettings,
    LocaleResolver localeResolver) {
  super(URI);
  this.userAccountService = requireNonNull(userAccountService);
  this.recoveryService = requireNonNull(recoveryService);
  this.twoFactorAuthenticationService = requireNonNull(twoFactorAuthenticationService);
  this.authenticationSettings = requireNonNull(authenticationSettings);
  this.localeResolver = requireNonNull(localeResolver);
}
 
Example #10
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 #11
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 #12
Source File: LocaleConfig.java    From SuperBoot with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
    return slr;
}
 
Example #13
Source File: I18nConfig.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * session区域解析器
 * @return
 */
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver resolver = new SessionLocaleResolver();
    resolver.setDefaultLocale(Locale.CHINA);

    return resolver;
}
 
Example #14
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 #15
Source File: LocaleConfig.java    From springBoot with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.US);
    return slr;
}
 
Example #16
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 #17
Source File: I18nConfig.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
 * Session方式
 *
 * @return
 */
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    //设置默认区域
    slr.setDefaultLocale(Locale.US);

    return slr;
}
 
Example #18
Source File: LocaleConfig.java    From wolf with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.CHINA);
    return slr;
}
 
Example #19
Source File: WebMvcAutoConfiguration.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
/**
 * 国际化设置
 *
 * @return LocaleResolver
 */
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.CHINA);
    return slr;
}
 
Example #20
Source File: WebMvcConfig.java    From FlyCms with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
	SessionLocaleResolver slr = new SessionLocaleResolver();
	// 默认语言
	slr.setDefaultLocale(Locale.CHINA);
	return slr;
}
 
Example #21
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 #22
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 locale and time zone context,
 * storing the new locale context through the configured {@link LocaleResolver}.
 * @param locale the new locale
 * @param timeZone the new time zone
 * @see LocaleContextResolver#setLocaleContext
 * @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
 */
public void changeLocale(Locale locale, TimeZone timeZone) {
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
	if (!(localeResolver instanceof LocaleContextResolver)) {
		throw new IllegalStateException("Cannot change locale context if no LocaleContextResolver configured");
	}
	((LocaleContextResolver) localeResolver).setLocaleContext(this.request, this.response,
			new SimpleTimeZoneAwareLocaleContext(locale, timeZone));
	this.locale = locale;
	this.timeZone = timeZone;
}
 
Example #23
Source File: WebMvcConfig.java    From spring-comparing-template-engines with Apache License 2.0 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
	SessionLocaleResolver slr = new SessionLocaleResolver();
	slr.setDefaultLocale(Locale.US);

	return slr;
}
 
Example #24
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 #25
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 #26
Source File: AppConfig.java    From spring-boot-ddd with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    Locale ptBr = new Locale("pt", "BR");
    slr.setDefaultLocale(ptBr);
    return slr;
}
 
Example #27
Source File: SpringViewResolverProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public SpringViewResolverProvider(ViewResolver viewResolver, LocaleResolver localeResolver) {
    if (viewResolver == null) {
        throw new IllegalArgumentException("Argument viewResolver is required");
    }
    if (localeResolver == null) {
        throw new IllegalArgumentException("Argument localeResolver is required");
    }
    this.viewResolver = viewResolver;
    this.localeResolver = localeResolver;
}
 
Example #28
Source File: RequestContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Change the current locale to the specified locale and time zone context,
 * storing the new locale context through the configured {@link LocaleResolver}.
 * @param locale the new locale
 * @param timeZone the new time zone
 * @see LocaleContextResolver#setLocaleContext
 * @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
 */
public void changeLocale(Locale locale, TimeZone timeZone) {
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
	if (!(localeResolver instanceof LocaleContextResolver)) {
		throw new IllegalStateException("Cannot change locale context if no LocaleContextResolver configured");
	}
	((LocaleContextResolver) localeResolver).setLocaleContext(this.request, this.response,
			new SimpleTimeZoneAwareLocaleContext(locale, timeZone));
	this.locale = locale;
	this.timeZone = timeZone;
}
 
Example #29
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 #30
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 locale and time zone context,
 * storing the new locale context through the configured {@link LocaleResolver}.
 * @param locale the new locale
 * @param timeZone the new time zone
 * @see LocaleContextResolver#setLocaleContext
 * @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
 */
public void changeLocale(Locale locale, TimeZone timeZone) {
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
	if (!(localeResolver instanceof LocaleContextResolver)) {
		throw new IllegalStateException("Cannot change locale context if no LocaleContextResolver configured");
	}
	((LocaleContextResolver) localeResolver).setLocaleContext(this.request, this.response,
			new SimpleTimeZoneAwareLocaleContext(locale, timeZone));
	this.locale = locale;
	this.timeZone = timeZone;
}