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

相关推荐
我爱Jack19 分钟前
时间与空间复杂度详解:算法效率的度量衡
java·开发语言·算法
米饭「」21 分钟前
C++AVL树
java·开发语言·c++
Zonda要好好学习29 分钟前
Python入门Day4
java·网络·python
开心就好202529 分钟前
WebView远程调试全景指南:实战对比主流工具优劣与适配场景
后端
用户214118326360234 分钟前
AI 一键搞定!中医药科普短视频制作全流程
后端
SimonKing42 分钟前
告别传统读写!RandomAccessFile让你的Java程序快人一步
java·后端·程序员
Little-Hu44 分钟前
QML TextEdit组件
java·服务器·数据库
Edingbrugh.南空1 小时前
Flink ClickHouse 连接器数据读取源码深度解析
java·clickhouse·flink
NE_STOP2 小时前
SpringBoot--简单入门
java·spring
hqxstudying2 小时前
Java创建型模式---原型模式
java·开发语言·设计模式·代码规范