0
votes

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;
    }
1
Why is there still a slash in your error? Did you copy my code? - dur

1 Answers

0
votes

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