Spring 国际化

jaxagkaj  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(461)

我有一个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;
    }
}
guykilcj

guykilcj1#

使用

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;
  }
ws51t4hk

ws51t4hk2#

像这样更改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;
    }

相关问题