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
相关推荐
泡海椒6 分钟前
JQuick-Curl 二次开发:自定义解析器、扩展点开发指南,如何把框架能力真正变成团队资产
java·开发语言·okhttp·maven
MetaLite30 分钟前
AI编程与工程底座-让AI遵守SpringBoot边界
人工智能·spring boot·ai编程
青山木36 分钟前
Hot 100 --- 数组中的第K个最大元素
java·数据结构·算法·排序算法
玛卡巴卡ldf40 分钟前
【AICoding】笔试提示词设计思路
java·算法·ai编程
IT_陈寒1 小时前
Python的多线程居然是个假把式?搞清GIL让我少熬三天夜
前端·人工智能·后端
珍珠先生1 小时前
第10章 JDK 17 新特性:语法糖与新能力
java
明月_清风1 小时前
位图与布隆过滤器:海量数据下的"存在性判断"艺术
前端·后端·算法
明月_清风1 小时前
Hash 表从入门到精通:Go 实战与工程细节
前端·后端·算法
考虑考虑1 小时前
ElasticSearch索引命令
运维·后端·elasticsearch
吴声子夜歌2 小时前
ApacheCommons——commons-text(模板替换与文本算法)
java·开发语言·算法·apache