SpringBoot 国际化-自定义 LocaleResolver

  1. 准备国际化文件
bash 复制代码
资源目录下创建 i18文件夹,
i18 下面创建两个文件:            预配置信息           
messages_en_US.preperties     | success=success
messages_zh_CN.properties     | success=操作成功
  1. 在application.yml中指定国际化文件的位置
yaml 复制代码
spring:
  messages:
    basename: i18/messages
  1. 创建一个自定义 local 解析器
java 复制代码
package com.shi.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@Component
public class CustomLocalResolver implements LocaleResolver {

    @Autowired
    private HttpServletRequest request;

    public Locale getLocale() {
        return resolveLocale(request);
    }

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (ObjectUtils.isEmpty(cookies)) {
            return Locale.getDefault();
        }
        List<Cookie> cookieList = Arrays.stream(request.getCookies()).filter(cookie -> cookie.getName().equals("locale")).collect(Collectors.toList());
        if (ObjectUtils.isEmpty(cookieList)) {
            return Locale.getDefault();
        }
        Locale locale = StringUtils.parseLocale(cookieList.get(0).getValue());
        if (ObjectUtils.isEmpty(locale)){
            return Locale.getDefault();
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    }

}
  1. 准备工具类。

使用Set注入解决 静态属性不能注入的问题。

java 复制代码
package com.shi.demo.utils;

import com.shi.demo.config.CustomLocalResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class I18Utils {

    private static CustomLocalResolver localResolver;

    private static ApplicationContext context;
    public static String getMessage(String code,Object[] args){
        return context.getMessage(code,args,localResolver.getLocale());
    }

    @Autowired
    public void setLocalResolver(CustomLocalResolver localResolver) {
        I18Utils.localResolver = localResolver;
    }

    @Autowired
    public void setContext(ApplicationContext context) {
        I18Utils.context = context;
    }
}
  1. 测试 代码
java 复制代码
@RestController
@RequestMapping("/users")
public class UserController {

    @GetMapping
    public String get() {
        return "get请求:"+I18Utils.getMessage("success",null);
    }
}
  1. 发起请求
    使用的是idea 自带的http client 发送的请求
java 复制代码
###
GET http://localhost:8080/users
#Cookie: locale=zh-cn  # 由于上面使用了StringUtils.parseLocale(),所以这里的值不区分大小写以及'_'和'-'
Cookie: locale=en_US
相关推荐
格砸42 分钟前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
蝎子莱莱爱打怪1 小时前
GitLab CI/CD + Docker Registry + K8s 部署完整实战指南
后端·docker·kubernetes
躺平大鹅2 小时前
Java面向对象入门(类与对象,新手秒懂)
java
哈密瓜的眉毛美2 小时前
零基础学Java|第三篇:DOS 命令、转义字符、注释与代码规范
后端
用户60572374873082 小时前
AI 编码助手的规范驱动开发 - OpenSpec 初探
前端·后端·程序员
哈密瓜的眉毛美2 小时前
零基础学Java|第二篇:Java 核心机制与第一个程序:从 JVM 到 Hello World
后端
用户8307196840822 小时前
RabbitMQ vs RocketMQ 事务大对决:一个在“裸奔”,一个在“开挂”?
后端·rabbitmq·rocketmq
初次攀爬者2 小时前
RocketMQ 集群介绍
后端·消息队列·rocketmq
初次攀爬者2 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
Leo8992 小时前
go 从零单排 之 一小时通关
后端