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

相关推荐
沙湖遇雨3 分钟前
3.客户端向服务端建立连接
后端
沙湖遇雨3 分钟前
1.netty源码阅读-管理端Server启动
后端
玖石书5 分钟前
ASP.NET Core迁移Spring系列:运行时与 GC 篇
后端·spring·asp.net
谁来拯救笨小孩7 分钟前
Cursor 流式协议:为何不是 REST,HTTP/2 与双向流怎么选
后端
明月光8187 分钟前
在数据库里做权限控制?重新理解 PostgreSQL 的 RLS(行级安全)
后端
编程风暴8 分钟前
3类人群的数据分析实习类型选择+4条选错红线
java·前端·数据分析
用户298698530148 分钟前
PDF 转图片?三种方案,覆盖全平台与自动化场景
人工智能·后端
沙湖遇雨9 分钟前
2.启动客户端client
后端
程序员只因10 分钟前
SpringBoot实战项目目录结构+高频注解详解(黑马点评适配版)
后端
Conan在掘金11 分钟前
鸿蒙 7.0 小艺全面进化:超 200 项感知+全天候引擎+超强记忆——AI 私人助理根因
后端