use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.
the class CookieLocaleResolverTests method testResolveLocaleContextWithoutCookie.
@Test
public void testResolveLocaleContextWithoutCookie() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
CookieLocaleResolver resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
assertThat(loc.getLocale()).isEqualTo(request.getLocale());
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
assertThat(condition).isTrue();
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull();
}
use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.
the class CookieLocaleResolverTests method testSetAndResolveLocaleContext.
@Test
public void testSetAndResolveLocaleContext() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(request, response, new SimpleLocaleContext(new Locale("nl", "")));
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
request = new MockHttpServletRequest();
request.setCookies(cookie);
resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
assertThat(condition).isTrue();
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull();
}
use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.
the class CookieLocaleResolverTests method testSetAndResolveLocaleContextWithTimeZone.
@Test
public void testSetAndResolveLocaleContextWithTimeZone() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(new Locale("nl", ""), TimeZone.getTimeZone("GMT+1")));
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
request = new MockHttpServletRequest();
request.setCookies(cookie);
resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
assertThat(condition).isTrue();
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1"));
}
use of org.springframework.context.i18n.LocaleContext in project spring-boot by spring-projects.
the class WebFluxAutoConfigurationTests method whenAcceptHeaderLocaleContextResolverIsUsedThenAcceptLanguagesHeaderIsHonoured.
@Test
void whenAcceptHeaderLocaleContextResolverIsUsedThenAcceptLanguagesHeaderIsHonoured() {
this.contextRunner.withPropertyValues("spring.web.locale:en_UK").run((context) -> {
MockServerHttpRequest request = MockServerHttpRequest.get("/").acceptLanguageAsLocales(StringUtils.parseLocaleString("nl_NL")).build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
LocaleContextResolver localeContextResolver = context.getBean(LocaleContextResolver.class);
assertThat(localeContextResolver).isInstanceOf(AcceptHeaderLocaleContextResolver.class);
LocaleContext localeContext = localeContextResolver.resolveLocaleContext(exchange);
assertThat(localeContext.getLocale()).isEqualTo(StringUtils.parseLocaleString("nl_NL"));
});
}
use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.
the class HttpComponentsHttpInvokerRequestExecutor method createHttpPost.
/**
* Create a HttpPost for the given configuration.
* <p>The default implementation creates a standard HttpPost with
* "application/x-java-serialized-object" as "Content-Type" header.
* @param config the HTTP invoker configuration that specifies the
* target service
* @return the HttpPost instance
* @throws java.io.IOException if thrown by I/O methods
*/
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
HttpPost httpPost = new HttpPost(config.getServiceUrl());
RequestConfig requestConfig = createRequestConfig(config);
if (requestConfig != null) {
httpPost.setConfig(requestConfig);
}
LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
if (localeContext != null) {
Locale locale = localeContext.getLocale();
if (locale != null) {
httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale));
}
}
if (isAcceptGzipEncoding()) {
httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
}
return httpPost;
}
Aggregations