** 原创文章,请勿转载 **

在给spring boot 1.5.6 + thymeleaf 3进行国际化时,踩了一个坑(其实不止一个)。 现象:

看到了吧, 就是取值的key, 后面被加了_en_US 或 _zh_CN, 以及前后的问号。

先看下代码,首先两个资源文件:

messages_en_US.properties

page.content=this is a test string.

message_zh_CN.properties, 在eclipse里打开的,内容是: 这是一个测试字符串

page.content=\u8FD9\u662F\u4E00\u4E2A\u6D4B\u5B57\u7B26\u4E32\u3002

i18n.html:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>spring boot, thymeleaf 3 国际化</h1>
<hr>
<a href="/i18n?lang=en_US">English</a> | <a href="/i18n?lang=zh_CN">中文</a> <br>
<br>
<br>
<br>
from thymeleaf engine: <span th:text="#{page.content}"></span>
<br>
<br>
from controller model: <span th:text="${content}"></span>
</body>
</html>

其中, #{page.content} 来源于thymeleaf 3,  ${content} 则由controller的Model返回,代码是hardcode, 返回的都是中文

controller:

@Controller
public class I18nController {
@Autowired
private MessageSource messageSource; @GetMapping("/i18n")
public String i18n(Model model) {
String message = messageSource.getMessage("page.content", null, Locale.SIMPLIFIED_CHINESE);
System.out.println("message=" + message);
model.addAttribute("content", message);
return "/i18n";
}
}

从现象看, ${content}这个是没有问题的, 也就是controller里messageSource.getMessage() 是正常的,再也就是说,资源文件是成功加载的,可见,问题出在thymeleaf 3.

再看下thymeleaf 3的配置:

@Configuration
public class ThymeleafConfig implements ApplicationContextAware {
private static final String UTF8 = "UTF-8";
private ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} private SpringResourceTemplateResolver htmlTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.applicationContext);
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
} @Autowired
private MessageSource messageSource; // 如果显示 ??x_zh_CN??, 缺少spring-context-support
private SpringTemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
templateEngine.setEnableSpringELCompiler(false); return templateEngine;
} @Bean
public ViewResolver htmlViewResolver(SpringTemplateEngine templateEngine) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setApplicationContext(applicationContext);
resolver.setTemplateEngine(templateEngine(htmlTemplateResolver()));
resolver.setCharacterEncoding("UTF-8");
return resolver;
} }

** 临时补一句,国际化要spring-context-support包的支持。

web的配置:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter { @Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
// 默认语言
slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return slr;
} @Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 参数名
lci.setParamName("lang");
return lci;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
} // 这个MessageSource可有可无,spring boot默认是有一个的。
// 如果没有自定义messageSource, 要有一个messages.properties文件。
// 如果有这个定义, 就不需要messages.properites
// @Bean
// public ResourceBundleMessageSource messageSource() {
// ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
// messageSource.setBasename("messages");
// return messageSource;
// }
}

查找原因, 在thymeleaf.org上看到这样一段:(在线文档3.1: Using th:text and externalizing text)

thymeleaf叫外部文本,无论是在外部的文件里,或者是在数据库,当然国际化使用的是外部文件。

从这段信息看, thymeleaf使用IMessageResolver接口来加载外部文本的,而且有一个标准的实现:

org.thymeleaf.messageresolver.StandardMessageResolver,所以加载.properties文件应该是和这些信息相关。

但 thymeleaf-spring4 里是怎么用的呢,看下代码吧:

org.thymeleaf.spring4.messageresolver.SpringMessageResolver:

    private final StandardMessageResolver standardMessageResolver;
private MessageSource messageSource; public SpringMessageResolver() {
super();
this.standardMessageResolver = new StandardMessageResolver();
}

它就是使用 org.thymeleaf.messageresolver.StandardMessageResolver, 这样看来,我只要在模板引擎的设置里加上这个IMessageResolver的实现即可,新的代码是这样:

private SpringTemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
SpringMessageResolver messageResolver = new SpringMessageResolver(); //
messageResolver.setMessageSource(messageSource); // 加入这三行,即为解决方案
templateEngine.setMessageResolver(messageResolver); //
templateEngine.setTemplateResolver(templateResolver);
templateEngine.setEnableSpringELCompiler(false);
return templateEngine;
}

最终呈现:

spring boot + thymeleaf 3 国际化的更多相关文章

  1. Spring Boot Thymeleaf 实现国际化

    开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了.SpringBoot支持如下页面模板语言 Thymeleaf FreeMarker Vel ...

  2. spring boot + Thymeleaf开发web项目

    "Spring boot非常适合Web应用程序开发.您可以轻松创建自包含的HTTP应用.web服务器采用嵌入式Tomcat,或者Jetty等.大多数情况下Web应用程序将使用 spring- ...

  3. Spring boot+Thymeleaf+easyui集成:js创建组件页面报错

    开发工具:Ideal 使用场景:Demo 前提:       环境:Spring boot +Thymeleaf+easyui 引入thymeleaf模板引擎 <html lang=" ...

  4. spring boot: thymeleaf模板引擎使用

    spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...

  5. spring boot + thymeleaf 乱码问题

    spring boot + thymeleaf 乱码问题 hellotrms 发布于 2017/01/17 15:27 阅读 1K+ 收藏 0 答案 1 开发四年只会写业务代码,分布式高并发都不会还做 ...

  6. 一个简单的示例在spring boot中实现国际化

    最近在网上找了一个有关账单管理的spring boot项目,其中有一部分是涉及显示国际化信息的,即将页面上的中英文进行转换.因为在这之前这部分内容没有接触过,所以在这记录下过程. 中文效果图如下所示: ...

  7. Spring Boot Thymeleaf 使用详解

    在上篇文章Spring Boot (二):Web 综合开发中简单介绍了一下 Thymeleaf,这篇文章将更加全面详细的介绍 Thymeleaf 的使用.Thymeleaf 是新一代的模板引擎,在 S ...

  8. 细品 Spring Boot+Thymeleaf,还有这么多好玩的细节!

    @ 目录 1. Thymeleaf 简介 2. 整合 Spring Boot 2.1 基本用法 2.2 手动渲染 3. Thymeleaf 细节 3.1 标准表达式语法 3.1.1 简单表达式 3.1 ...

  9. spring boot thymeleaf 标签未关闭报错

    每天学习一点点 编程PDF电子书免费下载: http://www.shitanlife.com/code spring boot,input标签未关闭报bug,代码稍有不慎就出小问题,后来百度,goo ...

随机推荐

  1. 【JVM命令系列】jstat

    命令基本概述 Jstat是JDK自带的一个轻量级小工具.全称"Java Virtual Machine statistics monitoring tool",它位于java的bi ...

  2. 51nod 1270 数组的最大代价 思路:简单动态规划

    这题是看起来很复杂,但是换个思路就简单了的题目. 首先每个点要么取b[i],要么取1,因为取中间值毫无意义,不能增加最大代价S. 用一个二维数组做动态规划就很简单了. dp[i][0]表示第i个点取1 ...

  3. 使用wsimport和JAX-WS调用Web Service接口

    本文简单举例说明如何使用wsimport工具和JAX-WS API调用Web Service接口.此方法的优点:使用JDK自带的工具和API接口,无需依赖第三方库. JDK版本:1.8.0_141开发 ...

  4. ASP.NET没有魔法——ASP.NET MVC & 分层

    上一篇文章简要说明了MVC所代表的含义并提供了详细的项目及其控制器.视图等内容的创建步骤,最终完成了一个简单ASP.NET MVC程序. 注:MVC与ASP.NET MVC不相等,MVC是一种开发模式 ...

  5. Centos7虚拟机桥接模式

    主机ping不通虚拟机centos7系统的ip大多有以下原因: 1.主机与centos7ip段对应 (关键是网关.dns服务器要一一对应,具体操作见后文) 2.对应后仍然不通的,可关闭主机与cenos ...

  6. Java+Velocity模板引擎集成插件到Eclipse及使用例子

    一.因为我用的是当前最新的Eclipse4.5,Eclipse中安装集成VelocityEclipse插件之前需要先安装其支持插件:Eclipse 2.0 Style Plugin Support 1 ...

  7. Linux vi 退出&保存/不保存

    无论是否退出 vi,均可保存所做的工作.按 ESC 键,确定 vi 是否处于命令模式. 操作   键入 保存,但不退出vi                          :w 保存并退出vi    ...

  8. python之线程学习

    一.进程与线程简介 进程 进程是程序的一次执行,由进程段.数据段.进程控制块三部分组成.具体三个基本状态,就绪.执行.阻塞,是一个拥有资源的独立单位. 线程 属于进程的一个实体,拥有极少的资源.也具有 ...

  9. Sqlite常用sql语句

    sqlite常用sql语句 --返回UTC时间 select CURRENT_TIMESTAMP; --返回本地时间 select datetime(CURRENT_TIMESTAMP,'localt ...

  10. 【特效】hover效果之十字动画

    效果预览:http://www.gbtags.com/gb/rtreplayerpreview-standalone/3101.htm html: <div class="wrap&q ...