最近在写一些Web的东西,技术上采用了Spring Boot + Bootstrap + jQuery + Freemarker。过程中查了大量的资料,也感受到了前端技术的分裂,每种东西都有N种实现,组合起来,每种解决方案的资料却很有限。

这篇文章记录下多语言国际化的实现,以支持中英文为例。

首先是页面内容的国际化

1.定义页面文本配置文件的路径,在application.properties里添加spring.messages.basename=i18n/messages

2.在resources/目录下创建上述目录,添加3个配置文件messages.propertiesmessages_zh.propertiesmessages_en.properties,分别对应默认,中文和英文配置,完整路径为resources/i18n/messages.properties

3.在配置文件里定义每条需要国际化的文本,比如中文 index.title=麦希工具 - 您身边的助手 ,英文Meta Tool - Your Best Assistant

4.在Freemarker文件里使用<@spring.message ""/>来输出文本,比如<title><@spring.message "index.title"/></title>

再说下验证内容的国际化

所谓验证内容,就是比如在填Form表单的时候,有些字段有格式或数值等的要求,表单提交时,应该验证数据是否符合要求。验证又分前端验证和后端验证,一般结合使用。

前端用来验证格式(必须是数字/英文/邮件)等,如:

<input type="text" name="height" id="height" class="form-control col-2" placeholder="" pattern="^[1-9][0-9]{2}$" required>

这里pattern使用正则表达式,定义了输入框输入字符的范围和数量。

后端用来验证数值(必须大于18岁)等,可以在Spring Boot的VO对象里,以添加注解的形式实现,如:

    @Min(value = 100, message = "不能低于100cm")
@Max(value = 250, message = "不能高于250cm")
private String height;

这里就出现了问题,即注解也应该采用配置文件的形式,以支持多语言切换,这就是验证内容的国际化。

1.定义验证文本配置文件的路径,在application.properties里添加spring.messages.basename=i18n/messages,i18n/ValidationMessages,前面的是第一节的页面文本,后面的是验证文本

2.在resources/目录下创建上述目录,添加3个配置文件ValidationMessages.propertiesValidationMessages_zh.propertiesValidationMessages_en.properties,分别对应默认,中文和英文配置,完整路径为resources/i18n/ValidationMessages.properties

3.在配置文件里定义每条需要国际化的文本,比如中文 vm.bmi.height.lower=不能低于100cm ,英文vm.bmi.height.lower=Can Not Lower Than 100cm

4.与页面文本相比,这里要多做一步,即在代码里覆盖默认验证器的配置,在代码根目录添加以下文件:


@Configuration
public class CustomConfiguration implements WebMvcConfigurer { @Autowired
private MessageSource messageSource; @Override
public Validator getValidator() {
return localValidatorFactoryBean();
} @Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(messageSource);
return validator;
}
}

5.在接收表达的VO对象的属性上,使用验证文本作为提示


public class BmiVo { @Min(value = 100, message = "{vm.bmi.height.lower}")
@Max(value = 250, message = "{vm.bmi.height.upper}")
private String height;

6.在表单上添加验证失败后的提示,使用<@spring.bind "" />绑定VO对象的属性,使用<@spring.showErrors ""/>显示属性验证失败的提示

<div id="div-height" class="form-group form-row align-items-center">
<@spring.bind "vo.height" />
<label for="height" class="col-form-label text-center offset-3 col-2"><@spring.message "bmi.height"/></label>
<input type="text" name="height" id="height" class="form-control col-2" placeholder="" pattern="^[1-9][0-9]{2}$" required>
<span class="text-primary text-center col-1">cm</span>
<span class="text-danger col-4"><@spring.showErrors ""/></span>
</div>

这样就做到了页面内容和验证内容的多语言国际化支持,具体示例参看Meta Tool

Spring Boot + Freemarker多语言国际化的实现的更多相关文章

  1. spring boot+freemarker+spring security标签权限判断

    spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...

  2. Spring boot Freemarker 获取ContextPath的方法

    Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...

  3. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】(转)

    需求缘起:有人在群里@我:请教群主大神一个问题,spring boot  + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spring ...

  4. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot

      需求缘起:有人在群里@我:请教群主大神一个问题,spring boot  + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spri ...

  5. spring boot 与 thymeleaf (1): 国际化

    在thymeleaf 里面有个消息表达式: #{...} , 可以借此来实现国际化. 在我使用这个功能的时候, 碰到了一个问题, 按照 JavaEE开发的颠覆者 Spring Boot实战  上面编码 ...

  6. Spring Boot FreeMarker 使用教程

    FreeMarker 跟 Thymeleaf 一样,是一种模板引擎,他可以无缝兼容 FreeMarker 在 Spring Boot 开发者中仍然有着很高的地位. 本章重点内容 编写一个最简单的 Fr ...

  7. Spring boot freemarker 配置

    spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.jdbc.Dri ...

  8. spring boot freemarker 导出word 带echarts图形报表

    创建word文件内容如下 将word导出为xml格式 将文件后缀名改为 .ftl 在springboot项目中添加freemarker依赖 <!-- 导出word文档--> <dep ...

  9. Spring Boot Thymeleaf 实现国际化

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

随机推荐

  1. Java获取当前的日期和时间

    Java获取当前的日期和时间 1.具体实现方法如下 /** * @Title:DateTime.java * @Package:com.you.model * @Description:获取当前的日期 ...

  2. 嵌入式Linux基于framebuffer的jpeg格式本地LCD屏显示

    在基于Linux的视频监控采集系统中,摄像头采集到的一帧视频图像数据一般都是经过硬件自动压缩成jpeg格式的,然后再保存到摄像头设备的缓冲区.如果要把采集到的jpeg格式显示在本地LCD屏上,由于我们 ...

  3. Flex读取txt文件中的内容报错

    Flex读取txt文件中的内容 1.具体错误如下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePat ...

  4. Error: Dynamic is undefined

    1.错误描述 Error: Dynamic is undefined @http://localhost:8080/Query/resource/global/scripts/app.js:149:1 ...

  5. C# 对象数据转换Json帮助类 JsonHelp

    C# 对象数据转换Json帮助类 JsonHelp using System; using System.Data; using System.Configuration; using System. ...

  6. css文字居中、图片居中、div居中解决方案

    一.文字居中 若文字只有一行 <!--html代码--> <div class="box"> <p class="text"> ...

  7. 【BZOJ4556】字符串(后缀数组,主席树)

    [BZOJ4556]字符串(后缀数组,主席树) 题面 BZOJ 题解 注意看题: 要求的是\([a,b]\)的子串和[c,d]的\(lcp\)的最大值 先来一下暴力吧 求出\(SA\)之后 暴力枚举\ ...

  8. 【CF235C】Cyclical Quest(后缀自动机)

    [CF235C]Cyclical Quest(后缀自动机) 题面 洛谷 题解 大致翻译: 给定一个串 然后若干组询问 每次也给定一个串 这个串可以旋转(就是把最后一位丢到最前面这样子) 问这个串以及其 ...

  9. 【BZOJ1009】GT考试(KMP算法,矩阵快速幂,动态规划)

    [BZOJ1009]GT考试(KMP算法,矩阵快速幂,动态规划) 题面 BZOJ 题解 看到这个题目 化简一下题意 长度为\(n\)的,由\(0-9\)组成的字符串中 不含串\(s\)的串的数量有几个 ...

  10. 【BZOJ1095】捉迷藏(动态点分治)

    [BZOJ1095]捉迷藏(动态点分治) 题面 BZOJ 题解 动态点分治板子题 假设,不考虑动态点分治 我们来想怎么打暴力: \(O(n)DP\)求树的最长链 一定都会.不想解释了 所以,利用上面的 ...