简体   繁体   中英

Can't find bundle for base name Warn

ResourceBundle [classpath:/org/springframework/security/messages] not found for MessageSource: Can't find bundle for base name classpath:/org/springframework/security/messages, locale ru I can't find any solution for setting internationalization in Java

This is how my code looks like

public static final Locale defaultLocale = new Locale("ru");

    public LocaleConfig() {
    }

    @Bean
    public LocaleResolver localeResolver() {
        return new FixedLocaleResolver(defaultLocale);
    }
    @Bean
    @Primary
    public MessageSource messageSource() {
        ResourceBundleMessageSource rs = new ResourceBundleMessageSource();
        rs.setBasenames("classpath:i18n/messages");
        rs.setBasenames("classpath:/org/springframework/security/messages");
        rs.setDefaultEncoding("UTF-8");
        rs.setUseCodeAsDefaultMessage(true);
        return rs;
    }

    @Bean
    @Primary
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
    }

The basenames are relative to the root of the class path, you don't have to write it, see 1.15.1. Internationalization using MessageSource :

 <beans> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>format</value> <value>exceptions</value> <value>windows</value> </list> </property> </bean> </beans>

The example assumes that you have three resource bundles called format , exceptions and windows defined in your classpath. Any request to resolve a message is handled in the JDK-standard way of resolving messages through ResourceBundle objects.

Your modified code:

@Bean
@Primary
public MessageSource messageSource() {
    ResourceBundleMessageSource rs = new ResourceBundleMessageSource();
    rs.setBasenames("org/springframework/security/messages");
    rs.setDefaultEncoding("UTF-8");
    rs.setUseCodeAsDefaultMessage(true);
    return rs;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM