前言  

写过一篇springboot+freemarker国际化过程中的细节问题,但没有写过具体的国际化实现过程。正好有人在问,我就把我实现的过程贴出来,即使自己知识的一个备份,如果对别人有点用,那是再好不过了。

1.springboot项目创建

springboot步入2.0后,已经逐步成为了java web快速开发的首选框架,项目的创建过程也是非常简单,教程非常多,我就不赘述了。下图时我创建的一个demo项目。

pom文件:

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhangs</groupId>
<artifactId>i18n-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>i18n-demo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

2. 各种配置

2.1 首先是application.properties的配置

  spring.messages.basename  指定了国际化文件的路径+文件前缀(不同语言,后缀是不一样的),如果不指定,默认是classpath下的messages_*.properties

2.2 增加一个全局配置类MainConfig,继承WebMvcConfigurationSupport

addInterceptors方法中增加了一个国际化拦截器,会拦截前端_lang参数,因为localeResolver方法中实例化了CookieLocaleResolver对象,所以_lang参数会存在cookie中,所有的页面都可以从cookie中取到_lang参数。
你也可以使用SessionLocaleResolver将参数存到session。
 @Configuration
@ComponentScan
@EnableAutoConfiguration
public class MainConfig extends WebMvcConfigurationSupport {
@Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver slr = new CookieLocaleResolver();
// 默认语言
slr.setDefaultLocale(Locale.US);
return slr;
} @Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 参数名
lci.setParamName("_lang");
return lci;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}

3. 创建国际化资源文件

资源文件/i18n/message_*.properties。*号的实际值(zh_CN,en_US等)就是上一步中_lang参数的值,决定了使用哪个资源文件

英文资源message_en_US.properties

中文资源message_zh_CN.properties

4. 测试ftl页面index.ftl

index.ftl

 <!DOCTYPE html>
<html lang="en">
<head>
<#import "spring.ftl" as spring>
<#assign arg = ["AAA","张三"]>
<title><@spring.messageArgs "title" ,arg /></title>
<meta charset="UTF-8">
</head>
<body>
<br/>
<a class="changeLang" href="javascript:void(0);">english</a><br/>
<a class="changeLang" href="javascript:void(0);">中文</a><br/>
<@spring.message code="user.loginname"/><br/>
</body>
<script src="${request.contextPath}/static/jquery.js"></script>
<script>
$(".changeLang").on("click", function () {
switch ($(this).text()) {
case "中文": {
window.location.href = "index?_lang=zh_CN";
break;
}
case "english": {
window.location.href = "index?_lang=en_US";
break;
}
}
})
</script>
</html>

5. 测试结果

ps:因为"AAA,张三"这几个字符是在ftl中写死了,所以不会变

英文:

中文:

项目地址: https://github.com/czs208112/i18n-demo

springboot + freemarker 实现国际化的更多相关文章

  1. SpringBoot起飞系列-国际化(六)

    一.前言 国际化这个功能可能我们不常用,但是在有需要的地方还是必须要上的,今天我们就来看一下怎么在我们的web开发中配置国际化,让我们的网站可以根据语言来展示不同的形式.本文接续上一篇SpringBo ...

  2. springboot+freemarker毕业设计项目错误合集

    1.springboot的主程序类必须在最外层. 换句话说,报错: This application has no explicit mapping for /error, so you are se ...

  3. SpringBoot系列——i18n国际化

    前言 国际化是项目中不可或缺的功能,本文将实现springboot + thymeleaf的HTML页面.js代码.java代码国际化过程记录下来. 代码编写 工程结构 每个文件里面的值(按工程结构循 ...

  4. springboot+freemarker

    springboot添加freemarker支持 1.application.properties中添加配置 #freemarker config spring.freemarker.allow-re ...

  5. springBoot 实现中文国际化

    一:实现效果如下: 二 SpringBoot 国际化配置 1.创建国际化配置文件(3个): messages.properties messages.user.name=用户名 messages.us ...

  6. idea+springboot+freemarker热部署(转)

    今天在学习springboot集成freemarker模板引擎修改代码时,发现每次修改一次freemarker文件时,都必须重启下应用,浏览器刷新才能显示修改后的内容,这样效率太低,每次启动一次应用都 ...

  7. springboot自动配置国际化失效分析

    最近在整理springBoot国际化时,发现国际化没有生效,通过报错提示在 MessageTag -> doEndTag处打断点 最后发现messageSource并不是ResourceBund ...

  8. SpringBoot 快速支持国际化i18n

    学习目标 快速学会如何在工程中支持国际化语言. 快速查阅 专题阅读:<SpringBoot 布道系列> 源码下载:springboot-locale-i18n — Hey Man,Don' ...

  9. 用maven配置springboot+freemarker

    1.创建项目 直接点下一步   原因: 不勾选 Create from archetype,是项目创建的骨架的时候,由于不知道什么原因就卡住了,一直在刷新 2.创建之后完成之后 添加依赖 <pa ...

随机推荐

  1. 2,postman的tests的断言写法

    tests的断言主要是分为三类 状态码,header内容和波body内容的测试,波body的不常用( 不容易控制) pm.expect(pm.response).to.have.status(&quo ...

  2. 2018上IEC计算机高级语言(C)作业 第3次作业

    2018上IEC计算机高级语言(C)作业 第3次作业 一.例程调试(20分) 调试下面2个例程,各位同学调试用自己的学号模3(即除以3取余数)加1序号及该序号乘以2的题.写明调试过程,如错误现象(如给 ...

  3. Effective C++ 笔记:条款 32 确定你的public继承塑造出正确的is-a关系

    32 : Make sure public inheritance models "is-a." 0 引言 Inheritance and Object-Oriented Desi ...

  4. (PMP)第13章-----项目相关方管理

    13.1 识别相关方 1 相关方分类的方法: 1.1 权力/利益方格,权力/影响方格,影响/作用方格(小型项目,关系简单) 权力:基于相关方的职权级别: 利益:对项目成果的关心程度 影响:对项目成果的 ...

  5. 02-jQuery的选择器

    我们以前在CSS中学习的选择器有: 今天来学习一下jQuery 选择器. jQuery选择器是jQuery强大的体现,它提供了一组方法,让我们更加方便的获取到页面中的元素. 1.jQuery 的基本选 ...

  6. 基于UML的文献管理系统建模研究

    一.基本信息 标题:基于UML的文献管理系统建模研究 时间:2016 出版源:信息与电脑(理论版) 领域分类:UML:文献管理系统:系统建模: 二.研究背景 问题定义:图书的管理与规划 难点:系统和管 ...

  7. Docker基础-使用Dockerfile创建镜像

    1.基本结构 Dockerfile由一行行命令语句组成,并支持以#开头的注释行.例如: # This dockerfile uses the ubuntu image # VERSION 2 - ED ...

  8. 人工智能必须要知道的语义分割模型:DeepLabv3+

    图像分割是计算机视觉中除了分类和检测外的另一项基本任务,它意味着要将图片根据内容分割成不同的块.相比图像分类和检测,分割是一项更精细的工作,因为需要对每个像素点分类,如下图的街景分割,由于对每个像素点 ...

  9. Android开发工程师文集-1 小时学会Widget小组件开发

    前言 大家好,给大家带来Android开发工程师文集-1 小时学会Widget小组件开发的概述,希望你们喜欢 学会用Widget (小组件) Widget小组件很方便,很快捷,可以个性化,自己定制,相 ...

  10. rocketmq搭建趟坑记

    这个坑对小白来讲可能要趟很久才能过,我就是这样~~明明很简单的配置,搞了半天 我用的是rocketmq4.1.0,配置了jvm参数,都能正常启动,且能在线上运行demo,但是线下就是连不上 在conf ...