前言  

写过一篇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. Eclipse Python 开发环境搭建 pydev 插件

    已安装: python 3.6 JDK Eclispe 在 Eclipse 中安装 pydev Pydev 的下载网址 http://www.pydev.org/download.html 安装完成后 ...

  2. zeromq示例代码

    http://www.oschina.net/code/snippet_614253_55034 将zeromq代码使用vc集中到一个解决方案中 可编译可调调试 vc2015

  3. Ural 1018 Binary Apple Tree

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018 Dynamic Programming. 首先要根据input建立树形结构,然后在 ...

  4. Wordpress“固定链接”页面出现404原因及解决方法

    编辑配置文件:/etc/apache2/apache2.conf(非常靠后的位置),将里面的AllowOverride选项由None设置为All. <Directory /> Option ...

  5. ScriptOJ-unique#89

    一般做法 const unique = (arr) => { const result = arr.reduce((acc, iter) => { if(acc.indexOf(iter) ...

  6. 批量插入数据, 将DataTable里的数据批量写入数据库的方法

    大量数据导入操作, 也就是直接将DataTable里的内容写入到数据库 通用方法: 拼接Insert语句, 好土鳖 1. MS Sql Server:   使用SqlBulkCopy 2. MySql ...

  7. WPF常用样式总结

    常用控件样式: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation ...

  8. ubuntu 16.04下源码安装opencv3.4

    源码安装opencv,遇到了一些小波折,这里做个备忘吧. 首先要下载源码,路径: https://github.com/opencv/opencv 下载成功后,在opencv的根目录下执行下面操作: ...

  9. python中使用双端队列解决回文问题

    双端队列:英文名字:deque (全名double-ended queue)是一种具有队列和栈性质的抽象数据类型. 双端队列中的元素可以从两端弹出,插入和删除操作限定在队列的两边进行. 双端队列可以在 ...

  10. python中基于queue的打印机仿真算法

    使用打印机的模型是queue中最经典的应用之一,这里就回顾一下queue在这里的使用方法和 起的重要作用. 为了仿真打印状态,这里需要把真实环境中的三个物理模型要建模出来,分别是:打印者,打印 任务, ...