SpringBoot国际化配置(i8n配置)未生效(完美解决)

最近在整理springBoot国际化时,发现国际化没有生效,通过报错提示在 MessageTag -> doEndTag处打断点
最后发现messageSource并不是ResourceBundleMessageSource,而是DelegatingMessageSource代理对象,其内部代理的对象为null,可知springboot自动配置的ResourceBundleMessageSource没有生效。

springBoot启动时,会自动加载MessageSourceAutoConfiguration,同时我们需要注意的是MessageSourceAutoConfiguration上的@Conditional({MessageSourceAutoConfiguration.ResourceBundleCondition.class})注解,@Conditional注解为当满足里面所有Condition类的条件时执行,分析ResourceBundleCondition.class的getMatchOutcome方法

public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
            String basename = context.getEnvironment().getProperty("spring.messages.basename", "messages");
            ConditionOutcome outcome = (ConditionOutcome)cache.get(basename);
            if (outcome == null) {
                outcome = this.getMatchOutcomeForBasename(context, basename);
                cache.put(basename, outcome);
            }

            return outcome;
        }

        private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context, String basename) {
        //默认目录默认名
            Builder message = ConditionMessage.forCondition("ResourceBundle", new Object[0]);
            String[] var4 = StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename));
            int var5 = var4.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String name = var4[var6];
          //根据name,获取相关文件
                Resource[] var8 = this.getResources(context.getClassLoader(), name);
                int var9 = var8.length;

                for(int var10 = 0; var10 < var9; ++var10) {
                    Resource resource = var8[var10];
                    if (resource.exists()) {
                        return ConditionOutcome.match(message.found("bundle").items(new Object[]{resource}));
                    }
                }
            }

            return ConditionOutcome.noMatch(message.didNotFind("bundle with basename " + basename).atAll());
        }

 private Resource[] getResources(ClassLoader classLoader, String name) {
        String target = name.replace('.', '/');

        try {
            return (new PathMatchingResourcePatternResolver(classLoader)).getResources("classpath*:" + target + ".properties");
        } catch (Exception var5) {
            return MessageSourceAutoConfiguration.NO_RESOURCES;
        }
    }
}

从上述代码发现,他需要读取后缀为.properties才可以获取到为true的ConditionOutcome,否则返回false

参考文章:https://www.cnblogs.com/jaxlove-it/p/10613040.html

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一个开发框架,用于简化Java应用程序的配置和部署。它提供了一套用于国际化(i18n)的消息资源(MessageSource)的支持。 MessageSource是Spring框架中用来处理国际化消息的接口。它的作用是将应用程序中的文本消息放入一个外部资源文件中,这样可以使得应用程序支持多种语言的消息显示。MessageSource可以加载多个资源文件,每个资源文件对应一种语言。开发人员可以根据需要添加、修改或删除文本消息,而无需修改应用程序的源代码。 在Spring Boot应用中配置MessageSource的步骤如下: 1. 在项目的配置文件(比如application.properties或application.yml)中,设置MessageSource相关的属性,比如资源文件的路径、默认的语言等。 2. 创建一个资源文件,例如messages.properties,将需要国际化的消息以key-value的形式保存在这个文件中。 3. 根据需要,可以创建其他资源文件,如messages_en.properties、messages_zh.properties等,分别对应不同的语言。 4. 在代码中使用MessageSource来获取消息,可以通过@Autowired注解将MessageSource注入到需要使用的类中,然后使用getMessage()方法获取对应的消息内容,根据需要传入参数。 通过MessageSource,开发人员可以实现应用程序的国际化,使得应用程序能够适应不同的地区和语言环境。Spring Boot提供了简单易用的配置和工具,使得国际化变得更加便捷。开发人员只需关注文本消息的准备和配置,而无需关心具体的国际化实现细节。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值