首页 > 解决方案 > 在配置中设置默认语言环境以供以后在集成测试中使用

问题描述

我有一个集成测试,可以加载应用程序的主要 Web 配置。

集成测试类配置有注释:

@SpringBootTest(classes = { ApplicationConfiguration.class, WebConfiguration.class })
@RunWith(SpringRunner.class)
@WebAppConfiguration

在此测试中,检索默认应用程序区域设置:

@Test
public void testRuntimeException() throws Exception {
    Locale defaultLocale = Locale.getDefault();
    // defaultLocale = LocaleContextHolder.getLocale();
    this.mockMvc.perform(
            get("/error/rte").headers(httpHeaders)
            .accept(MediaType.APPLICATION_JSON)
        )
        .andExpect(status().isInternalServerError())
        .andExpect(jsonPath("$.message").value(localizeErrorMessage("error.rte")))
        .andReturn();
}

上述两种获取语言环境的方法,都返回 fr_FR 语言环境。

我怀疑这是因为我的(Linux)操作系统用户有这个 fr_FR 有语言环境。

我希望我的应用程序忽略操作系统用户区域设置,因此我尝试在应用程序的配置中指定默认区域设置。

我尝试使用这些语言环境解析器中的 eihttps://jira.spring.io/browse/SPR-16775ther:

@Bean
public LocaleResolver localeResolver() {
    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    localeResolver.setDefaultLocale(Locale.ENGLISH);
    return localeResolver;
}

@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver localeResolver = new SessionLocaleResolver();
    localeResolver.setDefaultLocale(Locale.ENGLISH);
    return localeResolver;
}

使用它们时,测试总是返回 fr_FR 语言环境。

最重要的是,当在请求标头中设置区域设置时,控制器仍然会忽略它,并且在响应中发送默认区域设置,并进行以下测试(包含设置请求标头区域设置的两种不同方法)失败:

httpHeaders.add(HttpHeaders.ACCEPT_LANGUAGE, Locale.FRENCH.toLanguageTag());
this.mockMvc.perform(
        get("/error/npe").headers(httpHeaders).locale(Locale.FRENCH)
        .accept(MediaType.APPLICATION_JSON))
        .andDo(print()
    )
    .andExpect(status().isInternalServerError())
    .andExpect(jsonPath("$.message").value(localizeErrorMessage("error.npe", Locale.FRENCH)))
    .andReturn();

我正在使用 Spring Boot 2.0.3.RELEASE

更新:要让服务器注意到请求语言标头,我必须使用AcceptHeaderLocaleResolver解析器,因为SessionLocaleResolver解析器CookieLocaleResolver不会注意到请求语言标头。

所以我的配置看起来像:

private static final List<Locale> SUPPORTED_LOCALES = Arrays.asList(Locale.ENGLISH, Locale.FRENCH);

@Bean
public LocaleResolver localeResolver() {
    AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
    localeResolver.setSupportedLocales(SUPPORTED_LOCALES);
    localeResolver.setDefaultLocale(Locale.ENGLISH);
    return localeResolver;
}

我在配置中还有一个语言环境拦截器:

// The locale interceptor provides a way to switch the language in any page
// just by passing the lang=’en’, lang=’fr’, and so on to the url
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName(LOCALE_LANGUAGE_PARAM);
    return localeChangeInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
    registry.addInterceptor(webContentInterceptor());
}

但是使用AcceptHeaderLocaleResolver意味着lang没有注意到url参数,所以我不能在请求中使用它。上面的拦截器是没用的。目前只能使用标题。

更新:我注意到 usingLocale.getDefault()忽略了localeResolverbean 中配置的默认语言环境。要获得此默认语言环境,我必须使用:

@Autowired
private AcceptHeaderLocaleResolver localeResolver;

localeResolver.getDefaultLocale();

更新:语言环境拦截器目前似乎有一个未解决的问题。

标签: javaspring-bootlocale

解决方案


斯蒂芬妮,

我遇到了同样的问题,在寻找解决方案的过程中,我发现了以下问题: 解决在 spring boot 中设置默认语言环境的解决方案

src/main/resources/application.properties文件中插入以下代码并编辑语言环境:

#setting to default locale
spring.mvc.locale=pt_BR
#force locale resolver to spring boot reconized your settings
spring.mvc.locale-resolver=fixed

这对我有用,它的简单让我感到惊讶。我希望它对你有帮助!

此致!耶稣


推荐阅读