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

相关推荐
东小西9 分钟前
【SAA实战】第 1 篇:ReactAgent 入门——先撸个"会调工具的助手"跑起来
java·人工智能·spring
Profile排查笔记35 分钟前
指纹浏览器哪个好?从 Profile、代理、权限和自动化能力判断是否适合
前端·人工智能·后端·自动化
用户938515635071 小时前
用 AI 结对编程从 0 搭一个"单词后台管理系统":Next.js + Supabase + Drizzle + shadcn/ui 全记录
后端·postgresql·next.js
mqiqe1 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
脉动数据行情1 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股
爱学习的小邓同学1 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
小灰灰搞电子2 小时前
Rust+Slint 实现的“DNA双螺旋”加载动画源码分享
后端·rust·slint·加载动画
面向Google编程3 小时前
向量库不再囤数据:Milvus 3.0 零拷贝直读数据湖
后端
月华路3 小时前
G1 GC 对数组与大对象(Humongous)的处理
java·jvm·算法
liangshanbo12154 小时前
虚拟列表深度面试题整理
java·开发语言·前端