use of org.springframework.web.servlet.LocaleResolver in project spring-boot by spring-projects.
the class WebMvcAutoConfigurationTests method useDefaultLocaleIfAcceptHeaderNoSet.
@Test
public void useDefaultLocaleIfAcceptHeaderNoSet() {
load(AllResources.class, "spring.mvc.locale:en_UK");
// mock request and set user preferred locale
MockHttpServletRequest request = new MockHttpServletRequest();
LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
assertThat(localeResolver).isInstanceOf(AcceptHeaderLocaleResolver.class);
Locale locale = localeResolver.resolveLocale(request);
// test locale resolver uses default locale if no header is set
assertThat(locale.toString()).isEqualTo("en_UK");
}
use of org.springframework.web.servlet.LocaleResolver in project spring-boot by spring-projects.
the class WebMvcAutoConfigurationTests method useAcceptHeaderLocale.
@Test
public void useAcceptHeaderLocale() {
load(AllResources.class, "spring.mvc.locale:en_UK");
// mock request and set user preferred locale
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL"));
request.addHeader(HttpHeaders.ACCEPT_LANGUAGE, "nl_NL");
LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
assertThat(localeResolver).isInstanceOf(AcceptHeaderLocaleResolver.class);
Locale locale = localeResolver.resolveLocale(request);
// test locale resolver uses user preferred locale
assertThat(locale.toString()).isEqualTo("nl_NL");
}
use of org.springframework.web.servlet.LocaleResolver in project spring-boot by spring-projects.
the class WebMvcAutoConfigurationTests method overrideLocale.
@Test
public void overrideLocale() throws Exception {
load(AllResources.class, "spring.mvc.locale:en_UK", "spring.mvc.locale-resolver=fixed");
// mock request and set user preferred locale
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL"));
request.addHeader(HttpHeaders.ACCEPT_LANGUAGE, "nl_NL");
LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
assertThat(localeResolver).isInstanceOf(FixedLocaleResolver.class);
Locale locale = localeResolver.resolveLocale(request);
// test locale resolver uses fixed locale and not user preferred locale
assertThat(locale.toString()).isEqualTo("en_UK");
}
use of org.springframework.web.servlet.LocaleResolver in project Asqatasun by Asqatasun.
the class PageListWrapper method getResourceBundle.
/**
* 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;
}
Aggregations