spring mvn 国际化配置

目录

国际化配置测试

测试

测试:

自定义一个MessageSource类型的bean

java 复制代码
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

/**
 * @Author mubi
 * @Date 2025/1/12 15:15
 */
@Configuration
public class I18nMessageSource {

    @Bean(name = "myMessageSource")
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasenames("i18n.messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

}

SpringApplicationUtil工具类

java 复制代码
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class SpringApplicationUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringApplicationUtil.applicationContext = applicationContext;
    }

    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }

    public static List<String> getAllBeanNames(){
        String[] arr = applicationContext.getBeanDefinitionNames();
        if(arr == null || arr.length == 0){
            return new ArrayList<>();
        }
        return Arrays.stream(arr).collect(Collectors.toList());
    }

}

MessageUtls工具类

java 复制代码
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

public class MessageUtls {

    public static String message(String code, Object... args) {
        MessageSource messageSource = (MessageSource) SpringApplicationUtil.getBean("myMessageSource");
        return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
    }

}

配置

resources下按照配置

java 复制代码
test.message=just test message
java 复制代码
test.message=仅测试消息

国际化原理

自定义的bean

java 复制代码
@Bean(name = "myMessageSource")
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasenames("i18n.messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

ResourceBundleMessageSource

直接自定义的ResourceBundleMessageSource入手

相关推荐
凌辰揽月4 分钟前
8分钟讲完 Tomcat架构及工作原理
java·架构·tomcat
笑醉踏歌行10 分钟前
idea应用代码配色网站
java·ide·intellij-idea
一入JAVA毁终身12 分钟前
处理Lombok的一个小BUG
java·开发语言·bug
gjh120823 分钟前
Easy-excel监听器中对批量上传的工单做错误收集
java·spring boot
红衣女妖仙27 分钟前
JXLS 库导出复杂 Excel
java·excel·jxls·java 导出 excel
绝无仅有32 分钟前
对接三方SDK开发过程中的问题排查与解决
后端·面试·架构
Hellyc34 分钟前
JAVA八股文:异常有哪些种类,可以举几个例子吗?Throwable类有哪些常见方法?
java·开发语言
西岭千秋雪_43 分钟前
Redis缓存架构实战
java·redis·笔记·学习·缓存·架构
五岳1 小时前
深入研究使用DozerMapper复制List<Ojbect>前后元素类型不一致的问题
java·爬坑
人生在勤,不索何获-白大侠1 小时前
day15——Java常用API(二):常见算法、正则表达式与异常处理详解
java·算法·正则表达式