NipGeihou's blog NipGeihou's blog
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档

NipGeihou

我见青山多妩媚,料青山见我应如是
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档
  • 设计模式

  • 开发规范

  • 经验分享

  • 记录

  • 快速开始

    • Spring Boot整合RabbitMQ
    • Spring Boot整合Email邮件发送
    • Spring Boot整合jasypt加密配置文件
    • Spring Boot整合单元测试
    • Spring Boot整合优雅关机
    • Spring Boot整合Redis分布式锁
    • Spring Boot整合MyBatis-plus
    • XXL-JOB快速上手
    • Spring Boot整合WebSocket(stomp协议)
    • SpringBoot整合i18n(多语言)
      • 依赖
      • i18n翻译文件
      • applicant.yml
      • 使用
        • 自定义返回
        • 方法1
        • 方法2
        • 入参校验
      • 参考
    • 第三方登录 - Google
    • 第三方登录 - Facebook
    • Spring Boot 整合Elasticsearch
  • 笔记

  • 面试题

  • 微服务

  • 踩过的坑

  • Java
  • 快速开始
NipGeihou
2023-06-06
目录

SpringBoot整合i18n(多语言)

# 依赖

无需额外引入, spring-boot-starter-web 默认支持。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

# i18n 翻译文件

IDEA 中 resources\i18n 右键 New

image-20230606161442982

添加要支持的语言

image-20230606161415146

base name 通常为: messages

image-20230607100807779

# applicant.yml

spring:
  # 国际化配置
  messages:
    basename: i18n/messages
    encoding: UTF-8
    fallbackToSystemLocale: false

# 使用

使用中需要用到两个对象: MessageSource 、 Locale

MessageSource:可通过 Spring 容器获取

Locale:可通过请求对象 HttpServletRequest 获取

# 自定义返回

# 方法 1

@RestController
public class HelloController {
    @Autowired
    MessageSource messageSource;

    @GetMapping("/")
    public String index(Locale locale) {
        return messageSource.getMessage("error.notfound", null, locale);
    }
}

# 方法 2

public class I18nUtil {
    public static String getMessage(String code) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        MessageSource messageSource = SpringUtil.getBean(MessageSource.class);
        return messageSource.getMessage(code, null, request.getLocale());
    }

}

# 入参校验

javax.validation 本身就是支持多语言的,查看源码 String message() default "{javax.validation.constraints.NotBlank.message}"; 他是通过 el 表达式来实现的,因此只需替换成我们所需的 key 即可。

值得注意的是,这里 i18n 的文件路径为 resources/ValidationMessages

image-20230607131103204

@NotBlank(message = "{password.notBlank}")
@Pattern(regexp = RegularConstants.PASSWORD, message = "{password.pattern}")
private String password;

# 参考

  • Internationalization with Spring Boot REST - HowToDoInJava (opens new window)
  • 这可能是你见过 hibernate-validator 最全国际化方案 - 掘金 (opens new window)
  • SpringBoot 使用 validation 数据校验之国际化问题怎么搞?满满的干货,值得收藏 - 第 411 篇 - 知乎 (opens new window)
上次更新: 2024/03/11, 22:37:05
Spring Boot整合WebSocket(stomp协议)
第三方登录 - Google

← Spring Boot整合WebSocket(stomp协议) 第三方登录 - Google→

最近更新
01
元器件
05-23
02
iSCSI服务搭建
05-10
03
磁盘管理与文件系统
05-02
更多文章>
Theme by Vdoing | Copyright © 2018-2025 NipGeihou | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式