当前位置: 首页 > news >正文

SpringBoot使用i18n国际化

使用的SpringBoot版本是2.3.5

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.5.RELEASE</version><relativePath/> 
</parent>

 一、简单测试

(1)、配置文件

messages:basename: i18n/messagesencoding: UTF-8

 或者使用 Bean进行代码配置


@Configuration
public class LocalMessageConfig {/*** 系统国际化文件配置* @return MessageSource*/@Bean(name = "messageSource")public MessageSource messageSource() {ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();resourceBundleMessageSource.setDefaultEncoding("UTF-8");resourceBundleMessageSource.setBasenames("i18n/messages");return resourceBundleMessageSource;}
}

(2)、在resource目录下i18n文件夹,文件夹下建三个文件,文件名与配置文件中的basename中名称一致,在这里都是messages

message.properties文件内容: 

login.fail = {0} 登陆失败

message_en_US.properties文件内容:

login.fail = {0} login fail

message_zh_CN.properties文件内容:

login.fail = {0} 登陆失败

(3)、测试

@RequestMapping("/hello")
@RestController
public class I18nController {@Autowiredprivate MessageSource messageSource;@GetMappingpublic String msg(){String stringMsg1 = messageSource.getMessage("login.fail",new Object[]{username},Locale.CHINA);String stringMsg2 = messageSource.getMessage("login.fail",new Object[]{username},Locale.US);System.out.println("stringMsg1---"+stringMsg1 +"\n" + "stringMsg2---"+stringMsg2);//Locale locale = LocaleContextHolder.getLocale();//String message = messageSource.getMessage("login.fail", new Object[]{username}, locale);}
}

stringMsg1---admin 登陆失败
stringMsg2---admin login fail

二、添加一些工具类

http://www.lryc.cn/news/150193.html

相关文章:

  • Jmeter的自动化测试实施方案
  • nginx优化相关
  • 美客多(mercadolibre)测评下单技术(养号环境搭建详解)
  • UE5- c++ websocket客户端写法
  • C语言——函数的使用
  • DVWA XSS
  • 石油化工智慧安监方案:TSINGSEE青犀视频AI智能识别安全生产风险预警平台建设
  • R语言图形绘制
  • 2023第七届蓝帽杯 初赛 web LovePHP
  • Pytorch 的基本概念和使用场景介绍
  • git 基础入门
  • openssl 生成自签名证书
  • 微服务dubbo和nexus
  • uView1.0的Upload组件上传图片
  • 【LeetCode题目详解】第九章 动态规划part03 343. 整数拆分 96.不同的二叉搜索树 (day41补)
  • 半导体制造常用软件工具总结
  • 基于Python的IOS自动化测试环境搭建
  • 技术领导力实战笔记25
  • 设计模式-职责链+反射
  • Middleware ❀ Kafka功能与使用详解
  • python3.11教程1:python基础语法、程序控制、函数
  • 【C++】关于using namepace xxx 使用命名空间和冲突
  • Linux常用命令——cupsenable命令
  • 基于Stable Diffusion的AIGC服饰穿搭实践
  • 【 ARMv9 Cluster BUS QoS 配置】
  • 简单了解网络基本概念
  • 网络安全知识库
  • 无涯教程-JavaScript - FLOOR函数
  • 【LeetCode-中等题】146. LRU 缓存
  • 表白墙程序