Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring boot国际化在英语lang OS中总是更喜欢英语_Java_Spring_Spring Boot_Internationalization - Fatal编程技术网

Java Spring boot国际化在英语lang OS中总是更喜欢英语

Java Spring boot国际化在英语lang OS中总是更喜欢英语,java,spring,spring-boot,internationalization,Java,Spring,Spring Boot,Internationalization,我有SpringBoot应用程序,其中包含国际化所需的bean @Bean public LocaleResolver localeResolver() { CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver(); cookieLocaleResolver.setCookieName(CookieNames.LANG); cookieLocaleReso

我有SpringBoot应用程序,其中包含国际化所需的bean

 @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
        cookieLocaleResolver.setCookieName(CookieNames.LANG);
        cookieLocaleResolver.setDefaultLocale(new Locale(Lang.DEFAULT_LANG.getLabel())); //label is "ru"
        cookieLocaleResolver.setCookieMaxAge(this.localeAgeCookie);
        return cookieLocaleResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:i18n/messages");
        messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
        return messageSource;
    }

    @Bean
    public MessageSourceAccessor messageSourceAccessor() {
        return new MessageSourceAccessor(messageSource());
    }
还有俄语(messages.properties)和英语(messages_en.properties)的文件消息,默认选择俄语。
通过调用以下方法,我使用POST请求而不是拦截器更改语言

public static void setLocale(HttpServletRequest request, HttpServletResponse response, Lang lang) {
    final LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    localeResolver.setLocale(request, response, Locale.forLanguageTag(lang.getLabel()));

}
问题是当我打电话的时候

 messageSourceAccessor.getMessage(key)
,, 密钥位于messages和messages_en文件中,即使我在查询中强制使用区域设置,我始终从messages_en获取消息,如:

messageSourceAccessor.getMessage(key, new Locale("ru")).
我注意到我的同事在基于俄语lang的操作系统上没有这样的问题。但在制作方面,我有英文操作系统。
如何解决此问题?

您可以尝试使用系统参数启动应用程序
user.language=ru
user.country=RU


注意:我不确定ru和ru是否是好的值,但您可能比我更清楚解决方案非常简单。这足以更改消息源的属性fallbackToSystemLocale

 messageSource.setFallbackToSystemLocale(false);

它起作用了。但我认为这是一个值得怀疑的决定。如果显式指定为ruI的上下文中的区域设置不知道应用程序的上下文是什么,那么为什么最初他会尝试从messages_en.properties获取消息。如果您提供了一些示例,那么我可以对其进行研究,但您可能设置了错误的区域设置。试着看一看。有很多选择。太好了,对我有用!我遇到了一个奇怪的情况,在thymeleaf模板中打印出区域设置显示了我所期望的,但是messageSource没有使用相同的区域设置。