Java Code Examples for org.springframework.web.servlet.i18n.CookieLocaleResolver

The following examples show how to use org.springframework.web.servlet.i18n.CookieLocaleResolver. These examples are extracted from open source projects. 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 Project: MaxKey   Source File: WebContext.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * get locale from Spring Resolver,if locale is null,get locale from Spring.
 * SessionLocaleResolver this is from internationalization
 * 
 * @return Locale
 */
public static Locale getLocale() {
    Locale locale = null;
    try {
        CookieLocaleResolver cookieLocaleResolver = 
                (CookieLocaleResolver) getBean("localeResolver");
        locale = cookieLocaleResolver.resolveLocale(getRequest());

    } catch (Exception e) {
        LogFactory.getLog(WebContext.class).debug("getLocale() error . ");
        e.printStackTrace();
        locale = RequestContextUtils.getLocale(getRequest());
    }

    return locale;
}
 
Example 2
Source Project: EasyReport   Source File: LocaleUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 根据当前request对象中的locale(Header的Accept属性)初始化系统国际化语言区域环境
 *
 * @param request  当前请求对象
 * @param response 当前响应对象
 */
public static void setInitLocale(final HttpServletRequest request, final HttpServletResponse response) {
    final Locale locale = request.getLocale();
    log.info("Init locale from user request,country:{},lang:{}", locale.getCountry(), locale.toLanguageTag());

    if (localeResolver instanceof CookieLocaleResolver) {
        final CookieLocaleResolver cookieLocaleResolver = (CookieLocaleResolver)localeResolver;
        final Cookie cookie = WebUtils.getCookie(request, cookieLocaleResolver.getCookieName());
        if (cookie == null) {
            setLocale(locale.toLanguageTag(), request, response);
        }
    }
    if (localeResolver instanceof SessionLocaleResolver) {
        final Locale sessionLocale = (Locale)WebUtils.getRequiredSessionAttribute(
            request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
        if (sessionLocale == null) {
            setLocale(locale.toLanguageTag(), request, response);
        }
    }
}
 
Example 3
Source Project: spring-analysis-note   Source File: CookieAssertionTests.java    License: MIT License 5 votes vote down vote up
@Before
public void setup() {
	CookieLocaleResolver localeResolver = new CookieLocaleResolver();
	localeResolver.setCookieDomain("domain");
	localeResolver.setCookieHttpOnly(true);

	this.mockMvc = standaloneSetup(new SimpleController())
			.addInterceptors(new LocaleChangeInterceptor())
			.setLocaleResolver(localeResolver)
			.defaultRequest(get("/").param("locale", "en_US"))
			.alwaysExpect(status().isOk())
			.build();
}
 
Example 4
Source Project: scaffold-cloud   Source File: WebMvcConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    CookieLocaleResolver slr = new CookieLocaleResolver();
    slr.setDefaultLocale(Locale.CHINA);
    slr.setCookieMaxAge(LOCAL_COOKIE_TIME);
    return slr;

}
 
Example 5
Source Project: java-technology-stack   Source File: CookieAssertionTests.java    License: MIT License 5 votes vote down vote up
@Before
public void setup() {
	CookieLocaleResolver localeResolver = new CookieLocaleResolver();
	localeResolver.setCookieDomain("domain");
	localeResolver.setCookieHttpOnly(true);

	this.mockMvc = standaloneSetup(new SimpleController())
			.addInterceptors(new LocaleChangeInterceptor())
			.setLocaleResolver(localeResolver)
			.defaultRequest(get("/").param("locale", "en_US"))
			.alwaysExpect(status().isOk())
			.build();
}
 
Example 6
Source Project: Spring   Source File: WebConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
CookieLocaleResolver localeResolver() {
    CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
    cookieLocaleResolver.setCookieMaxAge(3600);
    cookieLocaleResolver.setCookieName("locale");
    return cookieLocaleResolver;
}
 
Example 7
Source Project: Spring   Source File: WebConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
CookieLocaleResolver localeResolver() {
    CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
    cookieLocaleResolver.setCookieMaxAge(3600);
    cookieLocaleResolver.setCookieName("locale");
    return cookieLocaleResolver;
}
 
Example 8
Source Project: Spring   Source File: WebConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
CookieLocaleResolver localeResolver() {
    CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
    cookieLocaleResolver.setCookieMaxAge(3600);
    cookieLocaleResolver.setCookieName("locale");
    return cookieLocaleResolver;
}
 
Example 9
Source Project: Spring   Source File: WebConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Cookie configuration for the internationalization settings
 */
@Bean
CookieLocaleResolver localeResolver() {
    final CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
    cookieLocaleResolver.setCookieMaxAge(3600);
    cookieLocaleResolver.setCookieName("locale");
    return cookieLocaleResolver;
}
 
Example 10
Source Project: SA47   Source File: WebAppConfig.java    License: The Unlicense 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {

	CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
	cookieLocaleResolver.setDefaultLocale(StringUtils
			.parseLocaleString("en"));
	return cookieLocaleResolver;
}
 
Example 11
Source Project: SA47   Source File: WebAppConfig.java    License: The Unlicense 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {

	CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
	cookieLocaleResolver.setDefaultLocale(StringUtils
			.parseLocaleString("en"));
	return cookieLocaleResolver;
}
 
Example 12
Source Project: Spring-Boot-I18n-Pro   Source File: I18nApplication.java    License: MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    CookieLocaleResolver slr = new CookieLocaleResolver();
    slr.setDefaultLocale(Locale.CHINA);
    slr.setCookieMaxAge(3600);
    slr.setCookieName("Language");
    return slr;
}
 
Example 13
@Bean
public LocaleResolver localeResolver() {
    // Cookieに言語を保存する
    val resolver = new CookieLocaleResolver();
    resolver.setCookieName("lang");
    return resolver;
}
 
Example 14
@Before
public void setup() {
	CookieLocaleResolver localeResolver = new CookieLocaleResolver();
	localeResolver.setCookieDomain("domain");

	this.mockMvc = standaloneSetup(new SimpleController())
			.addInterceptors(new LocaleChangeInterceptor())
			.setLocaleResolver(localeResolver)
			.defaultRequest(get("/").param("locale", "en_US"))
			.alwaysExpect(status().isOk())
			.build();
}
 
Example 15
Source Project: stategen   Source File: RequestUtil.java    License: GNU Affero General Public License v3.0 4 votes vote down vote up
public static Locale getLocale(){
    return (Locale) getRequest().getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
}
 
Example 16
Source Project: training   Source File: MvcConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public CookieLocaleResolver localeResolver() {
    return new CookieLocaleResolver();
}
 
Example 17
Source Project: EasyEE   Source File: I18NConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
	CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
	return cookieLocaleResolver;
}
 
Example 18
Source Project: EasyEE   Source File: I18NConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
	CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
	return cookieLocaleResolver;
}
 
Example 19
Source Project: tutorials   Source File: MvcConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
    return cookieLocaleResolver;
}