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

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();
    }

    @Bean
    public 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.kler.cn/news/364845.html

相关文章:

  • 【论文阅读】Tabbed Out: Subverting the Android Custom Tab Security Model
  • 线程本地变量-ThreadLocal
  • FFMPEG录屏(20)--- 枚举macOS下的窗口和屏幕列表,并获取名称缩略图等信息
  • LeetCode_509. 斐波那契数_java
  • 学习的内核,如何更好的学习呢?
  • 【Java】使用iText依赖生成PDF文件
  • logback日志脱敏后异步写入文件
  • 为什么需要MQ?MQ具有哪些作用?你用过哪些MQ产品?请结合过往的项目经验谈谈具体是怎么用的?
  • pdf编辑软件有哪些?方便好用的pdf编辑软件分享
  • 《深度学习》Dlib库 人脸应用实例 表情识别
  • 工业互联网引领制造业革命:智能化升级与创新亮点揭秘!
  • 2073. 买票需要的时间
  • 如何进行大数据治理
  • 基于RK3588/算能BM1684 AI盒子:综合视频智能AI分析系统建设方案(五)边缘盒子与AI服务器
  • leetcode动态规划(十五)-完全背包
  • idea中文国际化转码
  • 将md格式的数据展示在页面上
  • 『扩散模型』一篇文章入门DDPM
  • 学习docker第三弹------Docker镜像以及推送拉取镜像到阿里云公有仓库和私有仓库
  • 舍伍德业务安全架构(Sherwood Applied Business Security Architecture, SABSA)
  • 面试应该问什么?
  • 基于深度学习的图像修复系统设计与实现(PyQt5、CodeFormer ffhq-dataset数据集)
  • 贝锐花生壳内网穿透:无需公网IP,远程访问自建WebDAV文件共享
  • 数据分析与查询:矩量与辐角。
  • OpenCV高级图形用户界面(20)更改窗口的标题函数setWindowTitle()的使用
  • 基于LSTM-Transformer混合模型实现股票价格多变量时序预测(PyTorch版)