找不到基名称的捆绑包

t3psigkw  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(384)

找不到messagesource的resourcebundle[classpath:/org/springframework/security/messages]source:找不到基名称classpath:/org/springframework/security/messages的bundle,locale ru我找不到任何在java中设置国际化的解决方案
我的代码就是这样的

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

2jcobegt1#

基名相对于类路径的根,您不必编写它,请参见1.15.1。国际化使用 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>

该示例假设您有三个名为 format , exceptions 以及 windows 在类路径中定义。解析消息的任何请求都是通过jdk解析消息的标准方式处理的 ResourceBundle 物体。
修改后的代码:

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

相关问题