Spring framework: No message found under code for locale

后端 未结 6 2163
遇见更好的自我
遇见更好的自我 2021-01-01 17:41

This is my messageResource declaration





        
相关标签:
6条回答
  • 2021-01-01 18:24

    In general such issue appears not because of non-existence locale, but because MessageBundle is configured improperly. In your case you seem to need to remove "/" in your basename.

    <bean id="messageSource"
         class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
         p:basename="/WEB-INF/messages" />
    

    Why it is so:

    If you have messages.properties and messages_en.properties bundle, then bundle name is messages. If you have them in the WEB-INF folder, then basename is /WEB-INF/messages, i.e. according to /path/to/bundle/bundlename. If you have messages.properties within /WEB-INF/messages folder, then corresponding basename is /WEB-INF/messages/messages.

    0 讨论(0)
  • 2021-01-01 18:24

    You can specify in Spring boot application.properties too

    # INTERNATIONALIZATION 
    spring.messages.basename=i18n/messages
    spring.messages.encoding=UTF-8
    
    0 讨论(0)
  • 2021-01-01 18:32

    You can add some code in application.properties:

    spring.messages.always-use-message-format=false
    spring.messages.basename=messages
    spring.messages.cache-seconds=-1
    spring.messages.encoding=UTF-8
    spring.messages.fallback-to-system-locale=true
    
    0 讨论(0)
  • 2021-01-01 18:33

    For spring boot folder ressources you need to add name of Bean:

    @Bean(name="messageSource")
    public ResourceBundleMessageSource bundleMessageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    return messageSource;
    }
    
    0 讨论(0)
  • 2021-01-01 18:37

    For spring boot you need something like this:

    @Bean
    public MessageSource messageSource() {
         ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
         messageSource.setBasename("/WEB-INF/classes/messages");
         return messageSource;
    }
    
    0 讨论(0)
  • 2021-01-01 18:39

    By default maven assumes to find such resource bundle message source under

    src/main/resources

    . By moving all the necessary folders under the location and also making sure we have the following code in the context

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename"><value>messages</value></property>
    </bean>
    
    0 讨论(0)
提交回复
热议问题