本文主要实现的功能:

  • 从文件夹中直接加载多个国际化文件
  • 后台设置前端页面显示国际化信息的文件

实现

国际化项目初始化,简单看下项目的目录和文件

在resource下创建国际化文件

  • messages.properties
  • messages_en_US.properties
  • messages_zh_CN.properties

配置引用国际化文件

  1. spring.messages.basename=messages

如果是多个源的话,用","相隔即可。

  1. spring.messages.basename=messagesxxx,xxxx,xxxx

创建常量类

  1. public class I18nConstant {
  2. /**
  3. * 一个例子
  4. */
  5. public static final String TEXT = "DeviceSummary.device.type.name";
  6. }

对应分别在三个国际化(默认、中文、英文)中加入DeviceSummary.device.type.name对应转换的语言

使用

  • 代码中使用
  1. @Autowired
  2. private MessageSource messageSource;
  3. String msg = messageSource.getMessage(I18nConstant.TEXT, null, locale);
  • 获取request的Locale
  1. Locale locale1 = LocaleContextHolder.getLocale(); // 当前request
  2. Locale locale2 = RequestContextUtils.getLocale(request); // 指定request
  3. locale.getLanguage() //获取当前语言
  • 默认根据浏览器的语言设置来决定显示语言。一般应用多采用会话级别的语言设置SessionLocaleResolver。
  1. @Bean
  2. public LocaleResolver localeResolver() {
  3. SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
  4. sessionLocaleResolver.setDefaultLocale(Locale.CHINA);
  5. return sessionLocaleResolver;
  6. }
  • 切换语言
  1. @Bean
  2. public LocaleChangeInterceptor localeChangeInterceptor() {
  3. LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
  4. lci.setParamName("lang");
  5. return lci;
  6. }
  7. @Override
  8. public void addInterceptors(InterceptorRegistry registry) {
  9. registry.addInterceptor(localeChangeInterceptor());
  10. }

URL中通过参数lang就可以切换到不同语言。比如:http://localhost:8080/hello.html?lang=zh

SpringBoot整合国际化I18n的更多相关文章

  1. SpringBoot整合国际化功能

    (1).编写国际化配置文件 在resources下新建i18n文件夹,并新建以下文件 ①index.properties   username=username ②index_en_US.proper ...

  2. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  3. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  4. SpringBoot 国际化配置,SpringBoot Locale 国际化

    SpringBoot 国际化配置,SpringBoot Locale 国际化 ================================ ©Copyright 蕃薯耀 2018年3月27日 ht ...

  5. SpringBoot系列十二:SpringBoot整合 Shiro

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Shiro 2.具体内容 Shiro 是现在最为流行的权限认证开发框架,与它起名的只有最初 ...

  6. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

  7. SpringBoot系列之i18n集成教程

    目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...

  8. 【Spring Cloud & Alibaba全栈开源项目实战】:SpringBoot整合ELK实现分布式登录日志收集和统计

    一. 前言 其实早前就想计划出这篇文章,但是最近主要精力在完善微服务.系统权限设计.微信小程序和管理前端的功能,不过好在有群里小伙伴的一起帮忙反馈问题,基础版的功能已经差不多,也在此谢过,希望今后大家 ...

  9. SpringBoot 整合thymeleaf

    1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...

随机推荐

  1. Python模块:os

    OS模块常用用法: os.name() #判断当前使用的系统环境,windows则返回 ‘nt’,Linux则返回‘posix’ os.getcwd() #显示当前目录 os.listdir() #以 ...

  2. codevs 3027线段覆盖2

    传送门 3027 线段覆盖 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标, ...

  3. flask logger

    Flask uses standard Python logging. All Flask-related messages are logged under the 'flask' logger n ...

  4. ubuntu16.04 跑Apollo Demo

    1.安装docker(参考网址:https://docs.docker.com/install/linux/docker-ce/ubuntu/) Uninstall old versions Olde ...

  5. mtk6737t摄像头配置文件的编译

    修改摄像头的配置文件后,一直没有编译生效,要make一遍才生效,最终查出编译配置的方法摄像头配置文件路径 vendor/mediatek/proprietary/custom/mt6735/hal/D ...

  6. CS231n 2016 通关 第三章-Softmax 作业

    在完成SVM作业的基础上,Softmax的作业相对比较轻松. 完成本作业需要熟悉与掌握的知识: cell 1 设置绘图默认参数 mport random import numpy as np from ...

  7. 重载和const形参的学习心得

    在C++ primer 5th中,第6章的练习6.39(a)中,关于重载和const形参的学习心得. 练习6.39:说明在下面的每组声明中第二条声明语句是何含义.如果有非法的声明,请指出来. (a)  ...

  8. 1-1 课程简介 & 2-1 IDEA与Eclipse的不同 & 2-3 Intellij IDEA安装

    ---恢复内容开始--- F:\教程\java-慕课\从网页搭建入门Java Web\Java web\步骤四:常用功能\1.IntelliJ IDEA开发工具入门 1-1 课程简介 2-1 IDEA ...

  9. 制作Docker镜像的两种方式

    此文已由作者朱笑天授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 一.使用docker commit命令制作docker镜像 1. pull一个centos6.6的基础镜像, ...

  10. 1107 Social Clusters (30 分)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...