找不到基本名称为 “Warn” 的捆绑包。

huangapple go评论68阅读模式
英文:

Can't find bundle for base name Warn

问题

ResourceBundle [classpath:/org/springframework/security/messages] 未找到用于 MessageSource 的资源包: 无法找到基础名称为 classpath:/org/springframework/security/messages 的资源包,区域设置为 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;
}
英文:

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

得分: 1

以下是翻译好的内容:

基本名称是相对于类路径根目录的,无需编写,参见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>

该示例假设在类路径中定义了名为 formatexceptionswindows 的三个资源包。解析消息的任何请求都是通过ResourceBundle对象以JDK标准方式处理的。

您修改后的代码:

@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 basenames are relative to the root of the class path, you don't have to write it, see 1.15.1. Internationalization using MessageSource:

> <!--language: xml -->
>
> <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:

<!-- language: java -->

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

huangapple
  • 本文由 发表于 2020年8月19日 15:31:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63482048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定