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

Spring i18n国际化

从源码MessageSource的三个实现出发实战spring·i18n国际化 - 简熵 - 博客园

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;import java.util.Locale;
import java.util.ResourceBundle;@SpringBootApplication
public class HelloWorldSpringbootApplication {// ReloadableResourceBundleMessageSource可以加载指定项目以外的国际化文件private static String zhPath ="file:D:/temp/content";public static void main(String[] args) {// SpringApplication.run(HelloWorldSpringbootApplication.class, args);getSpringContent();}@Beanpublic ReloadableResourceBundleMessageSource messageSource() {ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();messageSource.setBasenames(zhPath, "classpath:content");return messageSource;}public static void getSpringContent() {AnnotationConfigApplicationContext ctx =new AnnotationConfigApplicationContext(HelloWorldSpringbootApplication.class);MessageSource source = ctx.getBean(MessageSource.class);Locale localeZh = new Locale("zh","CN");String hello = source.getMessage("hello", null, localeZh);System.out.println("hello = " + hello);}public static void getContent() {Locale localeEn = new Locale("en","US");Locale localeZh = new Locale("zh","CN");ResourceBundle res = ResourceBundle.getBundle("content", localeZh);String hello = res.getString("hello");System.out.println("hello = " + hello);}
}

再加三个文件:

content.properties
hello=123
content_en_US.properties
hello=hello_world

content_zh_CN.properties

hello=456

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

相关文章:

  • 基于stm32的楼宇照明控制系统设计
  • ESP32移植Openharmony外设篇(3)OLED屏
  • 人工智能:未来生活与工作的变革力量
  • AI自动生成PPT哪个软件好?智能生成PPT不再熬夜做课件
  • C# OOP面试题精选 面向新手/SOLID原则/设计模式++ 长期更新
  • 安全见闻(2)——开阔眼界,不做井底之蛙
  • ProtoBuf 的含义和安装
  • C++位操作实战:掩码、提取与组装
  • PVE虚拟机强制重启
  • Adobe Acrobat DC 打印PDF文件,没有打印出注释的解决方法
  • 主机名学习
  • SpringBoot循环依赖
  • 一道面试题:为什么要使用Docker?
  • 类的创建、构造器、实例属性、实例方法
  • js读取.txt文件内容
  • 【AscendC算子开发】笔记2 算子高级开发和调试调优
  • vscode连接keil-5 开发STM32 程序
  • 数据结构深度优先搜索遍历连通图+非连通图(C语言代码+遍历+终端输入内容)
  • 信息安全工程师(55)网络安全漏洞概述
  • member access within null pointer of type ‘ListNode‘
  • UE5蓝图中整理节点的方法
  • 01,http 协议
  • 在 typescript 中,如何封装一个 class 类来接收接口的响应数据
  • 力扣周赛第420场 中等 3325.字符至少出现k次的子字符串 I
  • 【Spring框架】Spring核心思想IoC以及依赖注入DI详解
  • Java项目-基于springboot框架的智慧外贸系统项目实战(附源码+文档)
  • Python程序控制结构 if语句详解
  • 【ppq install】
  • 3DGS相关方法conda环境配置
  • python画图|曲线动态输出