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入手

相关推荐
JavaGuide10 分钟前
OPPO 后端校招面试,过于简单了!
java·后端
码割机15 分钟前
Linux服务器安装jdk和maven详解
java·linux·maven
调试人生的显微镜17 分钟前
如何查看手机使用记录?四种实用方法详解
后端
侯爵19 分钟前
rabbitmq 如何保证消息顺序消费
后端
sualpha24 分钟前
再见,StringManipulation!AI一键搞定字符串转换、JSON格式化与翻译
后端
quant_198625 分钟前
【教程】使用加密货币行情接口 - 查询比特币实时价格
开发语言·后端·python·websocket·网络协议
得物技术36 分钟前
得物管理类目配置线上化:从业务痛点到技术实现
后端·算法·数据分析
小虚竹1 小时前
Rust日志系统完全指南:从log门面库到env_logger实战
开发语言·后端·rust
今日说"法"1 小时前
Rust 日志级别与结构化日志:从调试到生产的日志策略
开发语言·后端·rust
-大头.1 小时前
Rust并发编程实战技巧
开发语言·后端·rust