春季国际化



我有一个springBoot 2.4.0应用程序,在控制器中有这段代码:

String defaultLocation =
messages.getMessage("home.default.location", null, LocaleContextHolder.getLocale());

在应用程序中,我看到来自属性的消息正确加载了消息,没有错误,但在日志中我看到这个错误:

14:43:41.168 [http-nio-7080-exec-13] WARN  o.s.c.s.ReloadableResourceBundleMessageSource.refreshProperties 445 - Could not parse properties file [messages_en.properties]
java.util.zip.ZipException: invalid code lengths set
at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:165)
at org.springframework.boot.loader.jar.ZipInflaterInputStream.read(ZipInflaterInputStream.java:52)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185)
at java.base/java.io.Reader.read(Reader.java:229)
at java.base/java.util.Properties$LineReader.readLine(Properties.java:500)
at java.base/java.util.Properties.load0(Properties.java:415)
at java.base/java.util.Properties.load(Properties.java:378)
at org.springframework.util.DefaultPropertiesPersister.load(DefaultPropertiesPersister.java:64)
at org.springframework.context.support.ReloadableResourceBundleMessageSource.loadProperties(ReloadableResourceBundleMessageSource.java:495)
at org.springframework.context.support.ReloadableResourceBundleMessageSource.refreshProperties(ReloadableResourceBundleMessageSource.java:440)
at org.springframework.context.support.ReloadableResourceBundleMessageSource.getProperties(ReloadableResourceBundleMessageSource.java:395)
at org.springframework.context.support.ReloadableResourceBundleMessageSource.resolveCodeWithoutArguments(ReloadableResourceBundleMessageSource.java:186)
at org.springframework.context.support.AbstractMessageSource.getMessageInternal(AbstractMessageSource.java:212)
at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:153)
at com.bonanza.controller.HomeController.home(HomeController.java:46)

这是我的配置类:

@Configuration
public class I18NConfig {
private final Environment env;
public I18NConfig(Environment env) {
this.env = env;
}
@Bean
@Qualifier("messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource resourceBundleMessageSource = new ReloadableResourceBundleMessageSource();

resourceBundleMessageSource.setBasename("classpath:i18n/messages");

resourceBundleMessageSource.setDefaultEncoding("UTF-8"); // Set the UTF-8 encoding
resourceBundleMessageSource.setCacheSeconds(1);
return resourceBundleMessageSource;
}
}

像这样更改bean定义…删除缓存秒并使用setUSeCodeAsDefaultMessage()并尝试一下。还要重新检查您的属性文件定义和命名引用。

@Bean
@Qualifier("messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource resourceBundleMessageSource = new ReloadableResourceBundleMessageSource();

resourceBundleMessageSource.setBasename("classpath:i18n/messages");

resourceBundleMessageSource.setDefaultEncoding("UTF-8"); 
resourceBundleMessageSource.setUseCodeAsDefaultMessage(true);
return resourceBundleMessageSource;
}

use

org.springframework.context.support.ReloadableResourceBundleMessageSource

不是

org.springframework.context.support.ResourceBundleMessageSource

配置如下:

<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
<property name="basename"> 
<value>classpath*:i18n/messages</value> 
</property> 
</bean>

或:

@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:i18n/messages");
messageSource.setCacheSeconds(10); //reload messages every 10 seconds
return messageSource;
}

相关内容

  • 没有找到相关文章

最新更新